json_print: Return number of characters printed

When outputting in normal mode, forward the return value from
color_fprintf().

Signed-off-by: Benjamin Poirier <bpoirier@cumulusnetworks.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Benjamin Poirier 2020-05-01 17:47:18 +09:00 committed by Stephen Hemminger
parent b262a9becb
commit 5a07a5df5a
2 changed files with 73 additions and 46 deletions

View File

@ -44,20 +44,24 @@ void close_json_array(enum output_type type, const char *delim);
void print_nl(void); void print_nl(void);
#define _PRINT_FUNC(type_name, type) \ #define _PRINT_FUNC(type_name, type) \
void print_color_##type_name(enum output_type t, \ int print_color_##type_name(enum output_type t, \
enum color_attr color, \ enum color_attr color, \
const char *key, \ const char *key, \
const char *fmt, \ const char *fmt, \
type value); \ type value); \
\ \
static inline void print_##type_name(enum output_type t, \ static inline int print_##type_name(enum output_type t, \
const char *key, \ const char *key, \
const char *fmt, \ const char *fmt, \
type value) \ 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(int, int)
_PRINT_FUNC(s64, int64_t) _PRINT_FUNC(s64, int64_t)
_PRINT_FUNC(bool, bool) _PRINT_FUNC(bool, bool)

View File

@ -123,20 +123,22 @@ void close_json_array(enum output_type type, const char *str)
*/ */
#define _PRINT_FUNC(type_name, type) \ #define _PRINT_FUNC(type_name, type) \
__attribute__((format(printf, 4, 0))) \ __attribute__((format(printf, 4, 0))) \
void print_color_##type_name(enum output_type t, \ int print_color_##type_name(enum output_type t, \
enum color_attr color, \ enum color_attr color, \
const char *key, \ const char *key, \
const char *fmt, \ const char *fmt, \
type value) \ type value) \
{ \ { \
int ret = 0; \
if (_IS_JSON_CONTEXT(t)) { \ if (_IS_JSON_CONTEXT(t)) { \
if (!key) \ if (!key) \
jsonw_##type_name(_jw, value); \ jsonw_##type_name(_jw, value); \
else \ else \
jsonw_##type_name##_field(_jw, key, value); \ jsonw_##type_name##_field(_jw, key, value); \
} else if (_IS_FP_CONTEXT(t)) { \ } 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(int, int);
_PRINT_FUNC(s64, int64_t); _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); _PRINT_NAME_VALUE_FUNC(string, const char*, s);
#undef _PRINT_NAME_VALUE_FUNC #undef _PRINT_NAME_VALUE_FUNC
void print_color_string(enum output_type type, int print_color_string(enum output_type type,
enum color_attr color, enum color_attr color,
const char *key, const char *key,
const char *fmt, const char *fmt,
const char *value) const char *value)
{ {
int ret = 0;
if (_IS_JSON_CONTEXT(type)) { if (_IS_JSON_CONTEXT(type)) {
if (key && !value) if (key && !value)
jsonw_name(_jw, key); jsonw_name(_jw, key);
@ -176,8 +180,10 @@ void print_color_string(enum output_type type,
else else
jsonw_string_field(_jw, key, value); jsonw_string_field(_jw, key, value);
} else if (_IS_FP_CONTEXT(type)) { } 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 * 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 * branch for json and regular output. grep -r "print_bool" for example
*/ */
void print_color_bool(enum output_type type, int print_color_bool(enum output_type type,
enum color_attr color, enum color_attr color,
const char *key, const char *key,
const char *fmt, const char *fmt,
bool value) bool value)
{ {
int ret = 0;
if (_IS_JSON_CONTEXT(type)) { if (_IS_JSON_CONTEXT(type)) {
if (key) if (key)
jsonw_bool_field(_jw, key, value); jsonw_bool_field(_jw, key, value);
else else
jsonw_bool(_jw, value); jsonw_bool(_jw, value);
} else if (_IS_FP_CONTEXT(type)) { } 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 * In JSON context uses hardcode %#x format: 42 -> 0x2a
*/ */
void print_color_0xhex(enum output_type type, int print_color_0xhex(enum output_type type,
enum color_attr color, enum color_attr color,
const char *key, const char *key,
const char *fmt, const char *fmt,
unsigned long long hex) unsigned long long hex)
{ {
int ret = 0;
if (_IS_JSON_CONTEXT(type)) { if (_IS_JSON_CONTEXT(type)) {
SPRINT_BUF(b1); SPRINT_BUF(b1);
snprintf(b1, sizeof(b1), "%#llx", hex); snprintf(b1, sizeof(b1), "%#llx", hex);
print_string(PRINT_JSON, key, NULL, b1); print_string(PRINT_JSON, key, NULL, b1);
} else if (_IS_FP_CONTEXT(type)) { } 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, int print_color_hex(enum output_type type,
enum color_attr color, enum color_attr color,
const char *key, const char *key,
const char *fmt, const char *fmt,
unsigned int hex) unsigned int hex)
{ {
int ret = 0;
if (_IS_JSON_CONTEXT(type)) { if (_IS_JSON_CONTEXT(type)) {
SPRINT_BUF(b1); SPRINT_BUF(b1);
@ -235,28 +252,34 @@ void print_color_hex(enum output_type type,
else else
jsonw_string(_jw, b1); jsonw_string(_jw, b1);
} else if (_IS_FP_CONTEXT(type)) { } 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 * In JSON context we don't use the argument "value" we simply call jsonw_null
* whereas FP context can use "value" to output anything * whereas FP context can use "value" to output anything
*/ */
void print_color_null(enum output_type type, int print_color_null(enum output_type type,
enum color_attr color, enum color_attr color,
const char *key, const char *key,
const char *fmt, const char *fmt,
const char *value) const char *value)
{ {
int ret = 0;
if (_IS_JSON_CONTEXT(type)) { if (_IS_JSON_CONTEXT(type)) {
if (key) if (key)
jsonw_null_field(_jw, key); jsonw_null_field(_jw, key);
else else
jsonw_null(_jw); jsonw_null(_jw);
} else if (_IS_FP_CONTEXT(type)) { } 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) */ /* Print line separator (if not in JSON mode) */