All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Petr Oros <poros@redhat.com>
Cc: netdev@vger.kernel.org, dsahern@kernel.org, ivecera@redhat.com,
	jiri@resnulli.us, Jiri Pirko <jiri@nvidia.com>
Subject: Re: [PATCH iproute2-next v3 3/3] dpll: Add dpll command
Date: Fri, 14 Nov 2025 08:14:09 -0800	[thread overview]
Message-ID: <20251114081409.302dd29c@phoenix> (raw)
In-Reply-To: <20251114120555.2430520-4-poros@redhat.com>

On Fri, 14 Nov 2025 13:05:55 +0100
Petr Oros <poros@redhat.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)

      reply	other threads:[~2025-11-14 16:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-14 12:05 [PATCH iproute2-next v3 0/3] Add DPLL subsystem management tool Petr Oros
2025-11-14 12:05 ` [PATCH iproute2-next v3 1/3] lib: Move mnlg to lib for shared use Petr Oros
2025-11-14 12:05 ` [PATCH iproute2-next v3 2/3] lib: Add str_to_bool helper function Petr Oros
2025-11-14 12:05 ` [PATCH iproute2-next v3 3/3] dpll: Add dpll command Petr Oros
2025-11-14 16:14   ` Stephen Hemminger [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251114081409.302dd29c@phoenix \
    --to=stephen@networkplumber.org \
    --cc=dsahern@kernel.org \
    --cc=ivecera@redhat.com \
    --cc=jiri@nvidia.com \
    --cc=jiri@resnulli.us \
    --cc=netdev@vger.kernel.org \
    --cc=poros@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.