diff mbox series

[V2,iproute2,1/3] devlink: Add helper for left justification print

Message ID 1570026916-27592-2-git-send-email-tariqt@mellanox.com
State Accepted
Delegated to: stephen hemminger
Headers show
Series Devlink health FMSG fixes and enhancements | expand

Commit Message

Tariq Toukan Oct. 2, 2019, 2:35 p.m. UTC
From: Aya Levin <ayal@mellanox.com>

Introduce a helper function which wraps code that adds a left hand side
space separator unless it follows a newline.

Fixes: e3d0f0c0e3d8 ("devlink: add option to generate JSON output")
Signed-off-by: Aya Levin <ayal@mellanox.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: Tariq Toukan <tariqt@mellanox.com>
---
 devlink/devlink.c | 45 ++++++++++++++++++++-------------------------
 1 file changed, 20 insertions(+), 25 deletions(-)

Comments

Stephen Hemminger Oct. 2, 2019, 2:48 p.m. UTC | #1
On Wed,  2 Oct 2019 17:35:14 +0300
Tariq Toukan <tariqt@mellanox.com> wrote:

>  static void pr_out_str(struct dl *dl, const char *name, const char *val)
>  {
> -	if (dl->json_output) {
> +	__pr_out_indent_newline(dl);
> +	if (dl->json_output)
>  		jsonw_string_field(dl->jw, name, val);
> -	} else {
> -		if (g_indent_newline)
> -			pr_out("%s %s", name, val);
> -		else
> -			pr_out(" %s %s", name, val);
> -	}
> +	else
> +		pr_out("%s %s", name, val)

Overall this looks like an improvement.

Why doesn't devlink already use existing json_print infrastructure?
Jiri Pirko Oct. 2, 2019, 5:14 p.m. UTC | #2
Wed, Oct 02, 2019 at 04:48:04PM CEST, stephen@networkplumber.org wrote:
>On Wed,  2 Oct 2019 17:35:14 +0300
>Tariq Toukan <tariqt@mellanox.com> wrote:
>
>>  static void pr_out_str(struct dl *dl, const char *name, const char *val)
>>  {
>> -	if (dl->json_output) {
>> +	__pr_out_indent_newline(dl);
>> +	if (dl->json_output)
>>  		jsonw_string_field(dl->jw, name, val);
>> -	} else {
>> -		if (g_indent_newline)
>> -			pr_out("%s %s", name, val);
>> -		else
>> -			pr_out(" %s %s", name, val);
>> -	}
>> +	else
>> +		pr_out("%s %s", name, val)
>
>Overall this looks like an improvement.
>
>Why doesn't devlink already use existing json_print infrastructure?

It will happen soon hopefully. I have it on the todo list and hopefully
also a person to do it :)
diff mbox series

Patch

diff --git a/devlink/devlink.c b/devlink/devlink.c
index 5e762e37540c..d654dc7bc637 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -379,6 +379,12 @@  static bool dl_no_arg(struct dl *dl)
 	return dl_argc(dl) == 0;
 }
 
+static void __pr_out_indent_newline(struct dl *dl)
+{
+	if (!g_indent_newline && !dl->json_output)
+		pr_out(" ");
+}
+
 static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
 	[DEVLINK_ATTR_BUS_NAME] = MNL_TYPE_NUL_STRING,
 	[DEVLINK_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING,
@@ -1828,14 +1834,11 @@  static void pr_out_port_handle_end(struct dl *dl)
 
 static void pr_out_str(struct dl *dl, const char *name, const char *val)
 {
-	if (dl->json_output) {
+	__pr_out_indent_newline(dl);
+	if (dl->json_output)
 		jsonw_string_field(dl->jw, name, val);
-	} else {
-		if (g_indent_newline)
-			pr_out("%s %s", name, val);
-		else
-			pr_out(" %s %s", name, val);
-	}
+	else
+		pr_out("%s %s", name, val);
 }
 
 static void pr_out_bool(struct dl *dl, const char *name, bool val)
@@ -1848,29 +1851,23 @@  static void pr_out_bool(struct dl *dl, const char *name, bool val)
 
 static void pr_out_uint(struct dl *dl, const char *name, unsigned int val)
 {
-	if (dl->json_output) {
+	__pr_out_indent_newline(dl);
+	if (dl->json_output)
 		jsonw_uint_field(dl->jw, name, val);
-	} else {
-		if (g_indent_newline)
-			pr_out("%s %u", name, val);
-		else
-			pr_out(" %s %u", name, val);
-	}
+	else
+		pr_out("%s %u", name, val);
 }
 
 static void pr_out_u64(struct dl *dl, const char *name, uint64_t val)
 {
+	__pr_out_indent_newline(dl);
 	if (val == (uint64_t) -1)
 		return pr_out_str(dl, name, "unlimited");
 
-	if (dl->json_output) {
+	if (dl->json_output)
 		jsonw_u64_field(dl->jw, name, val);
-	} else {
-		if (g_indent_newline)
-			pr_out("%s %"PRIu64, name, val);
-		else
-			pr_out(" %s %"PRIu64, name, val);
-	}
+	else
+		pr_out("%s %"PRIu64, name, val);
 }
 
 static void pr_out_bool_value(struct dl *dl, bool value)
@@ -6118,14 +6115,12 @@  static void pr_out_region_handle_end(struct dl *dl)
 
 static void pr_out_region_snapshots_start(struct dl *dl, bool array)
 {
+	__pr_out_indent_newline(dl);
 	if (dl->json_output) {
 		jsonw_name(dl->jw, "snapshot");
 		jsonw_start_array(dl->jw);
 	} else {
-		if (g_indent_newline)
-			pr_out("snapshot %s", array ? "[" : "");
-		else
-			pr_out(" snapshot %s", array ? "[" : "");
+		pr_out("snapshot %s", array ? "[" : "");
 	}
 }