| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* json_print.c - print regular or json output, based on json_writer |
| 4 |
* |
| 5 |
* Authors: Julien Fortin, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdarg.h> |
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
#include "utils.h" |
| 12 |
#include "json_print.h" |
| 13 |
|
| 14 |
static json_writer_t *_jw; |
| 15 |
|
| 16 |
static void __new_json_obj(int json, bool have_array) |
| 17 |
{ |
| 18 |
if (json) { |
| 19 |
_jw = jsonw_new(stdout); |
| 20 |
if (!_jw) { |
| 21 |
perror("json object"); |
| 22 |
exit(1); |
| 23 |
} |
| 24 |
if (pretty) |
| 25 |
jsonw_pretty(_jw, true); |
| 26 |
if (have_array) |
| 27 |
jsonw_start_array(_jw); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
static void __delete_json_obj(bool have_array) |
| 32 |
{ |
| 33 |
if (_jw) { |
| 34 |
if (have_array) |
| 35 |
jsonw_end_array(_jw); |
| 36 |
jsonw_destroy(&_jw); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
void new_json_obj(int json) |
| 41 |
{ |
| 42 |
__new_json_obj(json, true); |
| 43 |
} |
| 44 |
|
| 45 |
void delete_json_obj(void) |
| 46 |
{ |
| 47 |
__delete_json_obj(true); |
| 48 |
} |
| 49 |
|
| 50 |
void new_json_obj_plain(int json) |
| 51 |
{ |
| 52 |
__new_json_obj(json, false); |
| 53 |
} |
| 54 |
|
| 55 |
void delete_json_obj_plain(void) |
| 56 |
{ |
| 57 |
__delete_json_obj(false); |
| 58 |
} |
| 59 |
|
| 60 |
bool is_json_context(void) |
| 61 |
{ |
| 62 |
return _jw != NULL; |
| 63 |
} |
| 64 |
|
| 65 |
json_writer_t *get_json_writer(void) |
| 66 |
{ |
| 67 |
return _jw; |
| 68 |
} |
| 69 |
|
| 70 |
void open_json_object(const char *str) |
| 71 |
{ |
| 72 |
if (_IS_JSON_CONTEXT(PRINT_JSON)) { |
| 73 |
if (str) |
| 74 |
jsonw_name(_jw, str); |
| 75 |
jsonw_start_object(_jw); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void close_json_object(void) |
| 80 |
{ |
| 81 |
if (_IS_JSON_CONTEXT(PRINT_JSON)) |
| 82 |
jsonw_end_object(_jw); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Start json array or string array using |
| 87 |
* the provided string as json key (if not null) |
| 88 |
* or as array delimiter in non-json context. |
| 89 |
*/ |
| 90 |
void open_json_array(enum output_type type, const char *str) |
| 91 |
{ |
| 92 |
if (_IS_JSON_CONTEXT(type)) { |
| 93 |
if (str) |
| 94 |
jsonw_name(_jw, str); |
| 95 |
jsonw_start_array(_jw); |
| 96 |
} else if (_IS_FP_CONTEXT(type)) { |
| 97 |
printf("%s", str); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/* |
| 102 |
* End json array or string array |
| 103 |
*/ |
| 104 |
void close_json_array(enum output_type type, const char *str) |
| 105 |
{ |
| 106 |
if (_IS_JSON_CONTEXT(type)) { |
| 107 |
jsonw_end_array(_jw); |
| 108 |
} else if (_IS_FP_CONTEXT(type)) { |
| 109 |
printf("%s", str); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* pre-processor directive to generate similar |
| 115 |
* functions handling different types |
| 116 |
*/ |
| 117 |
#define _PRINT_FUNC(type_name, type) \ |
| 118 |
__attribute__((format(printf, 4, 0))) \ |
| 119 |
int print_color_##type_name(enum output_type t, \ |
| 120 |
enum color_attr color, \ |
| 121 |
const char *key, \ |
| 122 |
const char *fmt, \ |
| 123 |
type value) \ |
| 124 |
{ \ |
| 125 |
int ret = 0; \ |
| 126 |
if (_IS_JSON_CONTEXT(t)) { \ |
| 127 |
if (!key) \ |
| 128 |
jsonw_##type_name(_jw, value); \ |
| 129 |
else \ |
| 130 |
jsonw_##type_name##_field(_jw, key, value); \ |
| 131 |
} else if (_IS_FP_CONTEXT(t)) { \ |
| 132 |
ret = color_fprintf(stdout, color, fmt, value); \ |
| 133 |
} \ |
| 134 |
return ret; \ |
| 135 |
} |
| 136 |
_PRINT_FUNC(int, int); |
| 137 |
_PRINT_FUNC(s64, int64_t); |
| 138 |
_PRINT_FUNC(hhu, unsigned char); |
| 139 |
_PRINT_FUNC(hu, unsigned short); |
| 140 |
_PRINT_FUNC(uint, unsigned int); |
| 141 |
_PRINT_FUNC(u64, uint64_t); |
| 142 |
_PRINT_FUNC(luint, unsigned long); |
| 143 |
_PRINT_FUNC(lluint, unsigned long long); |
| 144 |
_PRINT_FUNC(float, double); |
| 145 |
#undef _PRINT_FUNC |
| 146 |
|
| 147 |
#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \ |
| 148 |
void print_##type_name##_name_value(const char *name, type value)\ |
| 149 |
{ \ |
| 150 |
SPRINT_BUF(format); \ |
| 151 |
\ |
| 152 |
snprintf(format, SPRINT_BSIZE, \ |
| 153 |
"%s %%"#format_char, name); \ |
| 154 |
print_##type_name(PRINT_ANY, name, format, value); \ |
| 155 |
} |
| 156 |
_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u); |
| 157 |
_PRINT_NAME_VALUE_FUNC(string, const char*, s); |
| 158 |
#undef _PRINT_NAME_VALUE_FUNC |
| 159 |
|
| 160 |
int print_color_string(enum output_type type, |
| 161 |
enum color_attr color, |
| 162 |
const char *key, |
| 163 |
const char *fmt, |
| 164 |
const char *value) |
| 165 |
{ |
| 166 |
int ret = 0; |
| 167 |
|
| 168 |
if (_IS_JSON_CONTEXT(type)) { |
| 169 |
if (key && !value) |
| 170 |
jsonw_name(_jw, key); |
| 171 |
else if (!key && value) |
| 172 |
jsonw_string(_jw, value); |
| 173 |
else |
| 174 |
jsonw_string_field(_jw, key, value); |
| 175 |
} else if (_IS_FP_CONTEXT(type)) { |
| 176 |
ret = color_fprintf(stdout, color, fmt, value); |
| 177 |
} |
| 178 |
|
| 179 |
return ret; |
| 180 |
} |
| 181 |
|
| 182 |
/* |
| 183 |
* value's type is bool. When using this function in FP context you can't pass |
| 184 |
* a value to it, you will need to use "is_json_context()" to have different |
| 185 |
* branch for json and regular output. grep -r "print_bool" for example |
| 186 |
*/ |
| 187 |
static int __print_color_bool(enum output_type type, |
| 188 |
enum color_attr color, |
| 189 |
const char *key, |
| 190 |
const char *fmt, |
| 191 |
bool value, |
| 192 |
const char *str) |
| 193 |
{ |
| 194 |
int ret = 0; |
| 195 |
|
| 196 |
if (_IS_JSON_CONTEXT(type)) { |
| 197 |
if (key) |
| 198 |
jsonw_bool_field(_jw, key, value); |
| 199 |
else |
| 200 |
jsonw_bool(_jw, value); |
| 201 |
} else if (_IS_FP_CONTEXT(type)) { |
| 202 |
ret = color_fprintf(stdout, color, fmt, str); |
| 203 |
} |
| 204 |
|
| 205 |
return ret; |
| 206 |
} |
| 207 |
|
| 208 |
int print_color_bool(enum output_type type, |
| 209 |
enum color_attr color, |
| 210 |
const char *key, |
| 211 |
const char *fmt, |
| 212 |
bool value) |
| 213 |
{ |
| 214 |
return __print_color_bool(type, color, key, fmt, value, |
| 215 |
value ? "true" : "false"); |
| 216 |
} |
| 217 |
|
| 218 |
/* In JSON mode, acts like print_color_bool. |
| 219 |
* Otherwise, will print key with prefix of "no" if false. |
| 220 |
* The show flag is used to suppress printing in non-JSON mode |
| 221 |
*/ |
| 222 |
int print_color_bool_opt(enum output_type type, |
| 223 |
enum color_attr color, |
| 224 |
const char *key, |
| 225 |
bool value, bool show) |
| 226 |
{ |
| 227 |
int ret = 0; |
| 228 |
|
| 229 |
if (_IS_JSON_CONTEXT(type)) |
| 230 |
jsonw_bool_field(_jw, key, value); |
| 231 |
else if (_IS_FP_CONTEXT(type) && show) |
| 232 |
ret = color_fprintf(stdout, color, "%s%s ", |
| 233 |
value ? "" : "no", key); |
| 234 |
return ret; |
| 235 |
} |
| 236 |
|
| 237 |
int print_color_on_off(enum output_type type, |
| 238 |
enum color_attr color, |
| 239 |
const char *key, |
| 240 |
const char *fmt, |
| 241 |
bool value) |
| 242 |
{ |
| 243 |
return __print_color_bool(type, color, key, fmt, value, |
| 244 |
value ? "on" : "off"); |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* In JSON context uses hardcode %#x format: 42 -> 0x2a |
| 249 |
*/ |
| 250 |
int print_color_0xhex(enum output_type type, |
| 251 |
enum color_attr color, |
| 252 |
const char *key, |
| 253 |
const char *fmt, |
| 254 |
unsigned long long hex) |
| 255 |
{ |
| 256 |
int ret = 0; |
| 257 |
|
| 258 |
if (_IS_JSON_CONTEXT(type)) { |
| 259 |
SPRINT_BUF(b1); |
| 260 |
|
| 261 |
snprintf(b1, sizeof(b1), "%#llx", hex); |
| 262 |
print_string(PRINT_JSON, key, NULL, b1); |
| 263 |
} else if (_IS_FP_CONTEXT(type)) { |
| 264 |
ret = color_fprintf(stdout, color, fmt, hex); |
| 265 |
} |
| 266 |
|
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
int print_color_hex(enum output_type type, |
| 271 |
enum color_attr color, |
| 272 |
const char *key, |
| 273 |
const char *fmt, |
| 274 |
unsigned int hex) |
| 275 |
{ |
| 276 |
int ret = 0; |
| 277 |
|
| 278 |
if (_IS_JSON_CONTEXT(type)) { |
| 279 |
SPRINT_BUF(b1); |
| 280 |
|
| 281 |
snprintf(b1, sizeof(b1), "%x", hex); |
| 282 |
if (key) |
| 283 |
jsonw_string_field(_jw, key, b1); |
| 284 |
else |
| 285 |
jsonw_string(_jw, b1); |
| 286 |
} else if (_IS_FP_CONTEXT(type)) { |
| 287 |
ret = color_fprintf(stdout, color, fmt, hex); |
| 288 |
} |
| 289 |
|
| 290 |
return ret; |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
* In JSON context we don't use the argument "value" we simply call jsonw_null |
| 295 |
* whereas FP context can use "value" to output anything |
| 296 |
*/ |
| 297 |
int print_color_null(enum output_type type, |
| 298 |
enum color_attr color, |
| 299 |
const char *key, |
| 300 |
const char *fmt, |
| 301 |
const char *value) |
| 302 |
{ |
| 303 |
int ret = 0; |
| 304 |
|
| 305 |
if (_IS_JSON_CONTEXT(type)) { |
| 306 |
if (key) |
| 307 |
jsonw_null_field(_jw, key); |
| 308 |
else |
| 309 |
jsonw_null(_jw); |
| 310 |
} else if (_IS_FP_CONTEXT(type)) { |
| 311 |
ret = color_fprintf(stdout, color, fmt, value); |
| 312 |
} |
| 313 |
|
| 314 |
return ret; |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* This function does take printf style argument but applying |
| 319 |
* format attribute to causes more warnings since the print_XXX |
| 320 |
* functions are used with NULL for format if unused. |
| 321 |
*/ |
| 322 |
#pragma GCC diagnostic push |
| 323 |
#pragma GCC diagnostic ignored "-Wformat-nonliteral" |
| 324 |
int print_color_tv(enum output_type type, |
| 325 |
enum color_attr color, |
| 326 |
const char *key, |
| 327 |
const char *fmt, |
| 328 |
const struct timeval *tv) |
| 329 |
{ |
| 330 |
double usecs = tv->tv_usec; |
| 331 |
double secs = tv->tv_sec; |
| 332 |
double time = secs + usecs / 1000000; |
| 333 |
|
| 334 |
return print_color_float(type, color, key, fmt, time); |
| 335 |
} |
| 336 |
#pragma GCC diagnostic pop |
| 337 |
|
| 338 |
/* Print line separator (if not in JSON mode) */ |
| 339 |
void print_nl(void) |
| 340 |
{ |
| 341 |
if (!_jw) |
| 342 |
printf("%s", _SL_); |
| 343 |
} |
| 344 |
|
| 345 |
int print_color_rate(bool use_iec, enum output_type type, enum color_attr color, |
| 346 |
const char *key, const char *fmt, unsigned long long rate) |
| 347 |
{ |
| 348 |
unsigned long kilo = use_iec ? 1024 : 1000; |
| 349 |
const char *str = use_iec ? "i" : ""; |
| 350 |
static char *units[5] = {"", "K", "M", "G", "T"}; |
| 351 |
char *buf; |
| 352 |
int rc; |
| 353 |
int i; |
| 354 |
|
| 355 |
if (_IS_JSON_CONTEXT(type)) |
| 356 |
return print_color_lluint(type, color, key, "%llu", rate); |
| 357 |
|
| 358 |
rate <<= 3; /* bytes/sec -> bits/sec */ |
| 359 |
|
| 360 |
for (i = 0; i < ARRAY_SIZE(units) - 1; i++) { |
| 361 |
if (rate < kilo) |
| 362 |
break; |
| 363 |
if (((rate % kilo) != 0) && rate < 1000*kilo) |
| 364 |
break; |
| 365 |
rate /= kilo; |
| 366 |
} |
| 367 |
|
| 368 |
rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i], |
| 369 |
i > 0 ? str : ""); |
| 370 |
if (rc < 0) |
| 371 |
return -1; |
| 372 |
|
| 373 |
rc = print_color_string(type, color, key, fmt, buf); |
| 374 |
free(buf); |
| 375 |
return rc; |
| 376 |
} |
| 377 |
|
| 378 |
unsigned int print_range(const char *name, __u32 start, __u32 end) |
| 379 |
{ |
| 380 |
int width; |
| 381 |
|
| 382 |
width = print_uint(PRINT_ANY, name, "%u", start); |
| 383 |
if (start != end) { |
| 384 |
char buf[64]; |
| 385 |
|
| 386 |
snprintf(buf, sizeof(buf), "%sEnd", name); |
| 387 |
width += print_uint(PRINT_ANY, buf, "-%u", end); |
| 388 |
} |
| 389 |
|
| 390 |
return width; |
| 391 |
} |