[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20251114081409.302dd29c@phoenix>
Date: Fri, 14 Nov 2025 08:14:09 -0800
From: Stephen Hemminger <stephen@...workplumber.org>
To: Petr Oros <poros@...hat.com>
Cc: netdev@...r.kernel.org, dsahern@...nel.org, ivecera@...hat.com,
jiri@...nulli.us, Jiri Pirko <jiri@...dia.com>
Subject: Re: [PATCH iproute2-next v3 3/3] dpll: Add dpll command
On Fri, 14 Nov 2025 13:05:55 +0100
Petr Oros <poros@...hat.com> wrote:
> +#define DPLL_PR_SINT_FMT(tb, attr_id, name, format_str) \
> + do { \
> + if (tb[attr_id]) \
> + print_s64(PRINT_ANY, name, format_str, \
> + mnl_attr_get_sint(tb[attr_id])); \
> + } while (0)
> +
> +#define DPLL_PR_SINT(tb, attr_id, name) \
> + DPLL_PR_SINT_FMT(tb, attr_id, name, " " name ": %" PRId64 "\n")
> +
> +#define DPLL_PR_STR(tb, attr_id, name) \
> + DPLL_PR_STR_FMT(tb, attr_id, name, " " name ": %s\n")
> +
> +/* Temperature macro - JSON prints raw millidegrees, human prints formatted */
> +#define DPLL_PR_TEMP(tb, attr_id) \
> + do { \
> + if (tb[attr_id]) { \
> + __s32 temp = mnl_attr_get_u32(tb[attr_id]); \
> + if (is_json_context()) { \
> + print_int(PRINT_JSON, "temp", NULL, temp); \
> + } else { \
> + div_t d = div(temp, 1000); \
> + pr_out(" temp: %d.%03d C\n", d.quot, d.rem); \
> + } \
> + } \
> + } while (0)
> +
> +/* Generic version with custom format */
> +#define DPLL_PR_ENUM_STR_FMT(tb, attr_id, name, format_str, name_func) \
> + do { \
> + if (tb[attr_id]) \
> + print_string( \
> + PRINT_ANY, name, format_str, \
> + name_func(mnl_attr_get_u32(tb[attr_id]))); \
> + } while (0)
> +
> +/* Simple version with auto-generated format */
> +#define DPLL_PR_ENUM_STR(tb, attr_id, name, name_func) \
> + DPLL_PR_ENUM_STR_FMT(tb, attr_id, name, " " name ": %s\n", name_func)
> +
> +/* Multi-attr enum printer - handles multiple occurrences of same attribute */
> +#define DPLL_PR_MULTI_ENUM_STR(nlh, attr_id, name, name_func) \
> + do { \
> + struct nlattr *__attr; \
> + bool __first = true; \
> + \
> + if (!nlh) \
> + break; \
> + \
> + mnl_attr_for_each(__attr, nlh, sizeof(struct genlmsghdr)) \
> + { \
> + if (mnl_attr_get_type(__attr) == (attr_id)) { \
> + __u32 __val = mnl_attr_get_u32(__attr); \
> + if (__first) { \
> + if (is_json_context()) { \
> + open_json_array(PRINT_JSON, \
> + name); \
> + } else { \
> + pr_out(" " name ":"); \
> + } \
> + __first = false; \
> + } \
> + print_string(PRINT_ANY, NULL, " %s", \
> + name_func(__val)); \
> + } \
> + } \
> + if (__first) \
> + break; \
> + if (is_json_context()) { \
> + close_json_array(PRINT_JSON, NULL); \
> + } else { \
> + pr_out("\n"); \
> + } \
> + } while (0)
> +
Please don't write large macros. Why are these not functions.
Don't use pr_out, instead use print_nl() which allows for handling one line mode.
As much as possible do not split json code (is_json_context)
Powered by blists - more mailing lists