diff mbox series

[iproute2,1/6] json_print: Introduce print_#type_name_value

Message ID 1579775551-22659-2-git-send-email-rondi@mellanox.com
State Accepted
Delegated to: stephen hemminger
Headers show
Series devlink: Replace devlink print helper functions with common library functions | expand

Commit Message

Ron Diskin Jan. 23, 2020, 10:32 a.m. UTC
Until now print_#type functions supported printing constant names and
unknown (variable) values only.
Add functions to allow printing when the name is also sent to the
function as a variable.

Signed-off-by: Ron Diskin <rondi@mellanox.com>
Reviewed-by: Moshe Shemesh <moshe@mellanox.com>
Reviewed-by: Jiri Pirko <jiri@mellanox.com>
---
 include/json_print.h |  7 +++++++
 lib/json_print.c     | 13 +++++++++++++
 2 files changed, 20 insertions(+)

Comments

Stephen Hemminger Jan. 27, 2020, 1:52 p.m. UTC | #1
On Thu, 23 Jan 2020 12:32:26 +0200
Ron Diskin <rondi@mellanox.com> wrote:

> Until now print_#type functions supported printing constant names and
> unknown (variable) values only.
> Add functions to allow printing when the name is also sent to the
> function as a variable.
> 
> Signed-off-by: Ron Diskin <rondi@mellanox.com>
> Reviewed-by: Moshe Shemesh <moshe@mellanox.com>
> Reviewed-by: Jiri Pirko <jiri@mellanox.com>

Series applied.
I fixed one style complaint from checkpatch by removing semi-colon
in macro.

WARNING: macros should not use a trailing semicolon
#80: FILE: include/json_print.h:75:
+#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char)		  \
+	void print_##type_name##_name_value(const char *name, type value); \
+
diff mbox series

Patch

diff --git a/include/json_print.h b/include/json_print.h
index 6695654f..fd76cf75 100644
--- a/include/json_print.h
+++ b/include/json_print.h
@@ -72,4 +72,11 @@  _PRINT_FUNC(lluint, unsigned long long)
 _PRINT_FUNC(float, double)
 #undef _PRINT_FUNC
 
+#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char)		  \
+	void print_##type_name##_name_value(const char *name, type value); \
+
+_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u)
+_PRINT_NAME_VALUE_FUNC(string, const char*, s)
+#undef _PRINT_NAME_VALUE_FUNC
+
 #endif /* _JSON_PRINT_H_ */
diff --git a/lib/json_print.c b/lib/json_print.c
index 43ea69bb..fb5f0e5d 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -127,6 +127,19 @@  _PRINT_FUNC(lluint, unsigned long long);
 _PRINT_FUNC(float, double);
 #undef _PRINT_FUNC
 
+#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char)		 \
+	void print_##type_name##_name_value(const char *name, type value)\
+	{								 \
+		SPRINT_BUF(format);					 \
+									 \
+		snprintf(format, SPRINT_BSIZE,				 \
+			 "%s %%"#format_char, name);			 \
+		print_##type_name(PRINT_ANY, name, format, value);	 \
+	}
+_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
+_PRINT_NAME_VALUE_FUNC(string, const char*, s);
+#undef _PRINT_NAME_VALUE_FUNC
+
 void print_color_string(enum output_type type,
 			enum color_attr color,
 			const char *key,