From: Benjamin Poirier <bpoirier@cumulusnetworks.com>
To: netdev@vger.kernel.org
Subject: [PATCH iproute2 5/7] json_print: Return number of characters printed
Date: Tue, 28 Apr 2020 08:50:49 +0900 [thread overview]
Message-ID: <20200427235051.250058-6-bpoirier@cumulusnetworks.com> (raw)
In-Reply-To: <20200427235051.250058-1-bpoirier@cumulusnetworks.com>
When outputting in normal mode, forward the return value from
color_fprintf().
Signed-off-by: Benjamin Poirier <bpoirier@cumulusnetworks.com>
---
include/json_print.h | 24 ++++++-----
lib/json_print.c | 95 +++++++++++++++++++++++++++-----------------
2 files changed, 73 insertions(+), 46 deletions(-)
diff --git a/include/json_print.h b/include/json_print.h
index 34444793..50e71de4 100644
--- a/include/json_print.h
+++ b/include/json_print.h
@@ -44,20 +44,24 @@ void close_json_array(enum output_type type, const char *delim);
void print_nl(void);
#define _PRINT_FUNC(type_name, type) \
- void print_color_##type_name(enum output_type t, \
- enum color_attr color, \
- const char *key, \
- const char *fmt, \
- type value); \
+ int print_color_##type_name(enum output_type t, \
+ enum color_attr color, \
+ const char *key, \
+ const char *fmt, \
+ type value); \
\
- static inline void print_##type_name(enum output_type t, \
- const char *key, \
- const char *fmt, \
- type value) \
+ static inline int print_##type_name(enum output_type t, \
+ const char *key, \
+ const char *fmt, \
+ type value) \
{ \
- print_color_##type_name(t, COLOR_NONE, key, fmt, value); \
+ return print_color_##type_name(t, COLOR_NONE, key, fmt, \
+ value); \
}
+/* These functions return 0 if printing to a JSON context, number of
+ * characters printed otherwise (as calculated by printf(3)).
+ */
_PRINT_FUNC(int, int)
_PRINT_FUNC(s64, int64_t)
_PRINT_FUNC(bool, bool)
diff --git a/lib/json_print.c b/lib/json_print.c
index 8e7f32dc..fe0705bf 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -123,20 +123,22 @@ void close_json_array(enum output_type type, const char *str)
*/
#define _PRINT_FUNC(type_name, type) \
__attribute__((format(printf, 4, 0))) \
- void print_color_##type_name(enum output_type t, \
- enum color_attr color, \
- const char *key, \
- const char *fmt, \
- type value) \
+ int print_color_##type_name(enum output_type t, \
+ enum color_attr color, \
+ const char *key, \
+ const char *fmt, \
+ type value) \
{ \
+ int ret = 0; \
if (_IS_JSON_CONTEXT(t)) { \
if (!key) \
jsonw_##type_name(_jw, value); \
else \
jsonw_##type_name##_field(_jw, key, value); \
} else if (_IS_FP_CONTEXT(t)) { \
- color_fprintf(stdout, color, fmt, value); \
+ ret = color_fprintf(stdout, color, fmt, value); \
} \
+ return ret; \
}
_PRINT_FUNC(int, int);
_PRINT_FUNC(s64, int64_t);
@@ -162,12 +164,14 @@ _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,
- const char *fmt,
- const char *value)
+int print_color_string(enum output_type type,
+ enum color_attr color,
+ const char *key,
+ const char *fmt,
+ const char *value)
{
+ int ret = 0;
+
if (_IS_JSON_CONTEXT(type)) {
if (key && !value)
jsonw_name(_jw, key);
@@ -176,8 +180,10 @@ void print_color_string(enum output_type type,
else
jsonw_string_field(_jw, key, value);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(stdout, color, fmt, value);
+ ret = color_fprintf(stdout, color, fmt, value);
}
+
+ return ret;
}
/*
@@ -185,47 +191,58 @@ void print_color_string(enum output_type type,
* a value to it, you will need to use "is_json_context()" to have different
* branch for json and regular output. grep -r "print_bool" for example
*/
-void print_color_bool(enum output_type type,
- enum color_attr color,
- const char *key,
- const char *fmt,
- bool value)
+int print_color_bool(enum output_type type,
+ enum color_attr color,
+ const char *key,
+ const char *fmt,
+ bool value)
{
+ int ret = 0;
+
if (_IS_JSON_CONTEXT(type)) {
if (key)
jsonw_bool_field(_jw, key, value);
else
jsonw_bool(_jw, value);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(stdout, color, fmt, value ? "true" : "false");
+ ret = color_fprintf(stdout, color, fmt,
+ value ? "true" : "false");
}
+
+ return ret;
}
/*
* In JSON context uses hardcode %#x format: 42 -> 0x2a
*/
-void print_color_0xhex(enum output_type type,
- enum color_attr color,
- const char *key,
- const char *fmt,
- unsigned long long hex)
+int print_color_0xhex(enum output_type type,
+ enum color_attr color,
+ const char *key,
+ const char *fmt,
+ unsigned long long hex)
{
+ int ret = 0;
+
if (_IS_JSON_CONTEXT(type)) {
SPRINT_BUF(b1);
snprintf(b1, sizeof(b1), "%#llx", hex);
print_string(PRINT_JSON, key, NULL, b1);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(stdout, color, fmt, hex);
+ ret = color_fprintf(stdout, color, fmt, hex);
}
+
+ return ret;
}
-void print_color_hex(enum output_type type,
- enum color_attr color,
- const char *key,
- const char *fmt,
- unsigned int hex)
+int print_color_hex(enum output_type type,
+ enum color_attr color,
+ const char *key,
+ const char *fmt,
+ unsigned int hex)
{
+ int ret = 0;
+
if (_IS_JSON_CONTEXT(type)) {
SPRINT_BUF(b1);
@@ -235,28 +252,34 @@ void print_color_hex(enum output_type type,
else
jsonw_string(_jw, b1);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(stdout, color, fmt, hex);
+ ret = color_fprintf(stdout, color, fmt, hex);
}
+
+ return ret;
}
/*
* In JSON context we don't use the argument "value" we simply call jsonw_null
* whereas FP context can use "value" to output anything
*/
-void print_color_null(enum output_type type,
- enum color_attr color,
- const char *key,
- const char *fmt,
- const char *value)
+int print_color_null(enum output_type type,
+ enum color_attr color,
+ const char *key,
+ const char *fmt,
+ const char *value)
{
+ int ret = 0;
+
if (_IS_JSON_CONTEXT(type)) {
if (key)
jsonw_null_field(_jw, key);
else
jsonw_null(_jw);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(stdout, color, fmt, value);
+ ret = color_fprintf(stdout, color, fmt, value);
}
+
+ return ret;
}
/* Print line separator (if not in JSON mode) */
--
2.26.0
next prev parent reply other threads:[~2020-04-27 23:51 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-27 23:50 [PATCH iproute2 0/7] bridge vlan output fixes Benjamin Poirier
2020-04-27 23:50 ` [PATCH iproute2 1/7] bridge: Use the same flag names in input and output Benjamin Poirier
2020-04-29 15:12 ` Roopa Prabhu
2020-04-30 0:22 ` Benjamin Poirier
2020-04-30 0:58 ` Nikolay Aleksandrov
2020-04-27 23:50 ` [PATCH iproute2 2/7] bridge: Use consistent column names in vlan output Benjamin Poirier
2020-04-27 23:50 ` [PATCH iproute2 3/7] bridge: Fix typo Benjamin Poirier
2020-04-27 23:50 ` [PATCH iproute2 4/7] bridge: Fix output with empty vlan lists Benjamin Poirier
2020-04-27 23:50 ` Benjamin Poirier [this message]
2020-04-27 23:50 ` [PATCH iproute2 6/7] bridge: Align output columns Benjamin Poirier
2020-04-27 23:50 ` [PATCH iproute2 7/7] Replace open-coded instances of print_nl() Benjamin Poirier
2020-04-30 5:35 ` [PATCH iproute2 0/7] bridge vlan output fixes Stephen Hemminger
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=20200427235051.250058-6-bpoirier@cumulusnetworks.com \
--to=bpoirier@cumulusnetworks.com \
--cc=netdev@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).