|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* json_print.c "print regular or json output, based on json_writer".
|
|
Packit Service |
3880ab |
*
|
|
Packit Service |
3880ab |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
3880ab |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
3880ab |
* as published by the Free Software Foundation; either version
|
|
Packit Service |
3880ab |
* 2 of the License, or (at your option) any later version.
|
|
Packit Service |
3880ab |
*
|
|
Packit Service |
3880ab |
* Authors: Julien Fortin, <julien@cumulusnetworks.com>
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#include <stdarg.h>
|
|
Packit Service |
3880ab |
#include <stdio.h>
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#include "utils.h"
|
|
Packit Service |
3880ab |
#include "json_print.h"
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
static json_writer_t *_jw;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
|
|
Packit Service |
3880ab |
#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
static void __new_json_obj(int json, bool have_array)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (json) {
|
|
Packit Service |
3880ab |
_jw = jsonw_new(stdout);
|
|
Packit Service |
3880ab |
if (!_jw) {
|
|
Packit Service |
3880ab |
perror("json object");
|
|
Packit Service |
3880ab |
exit(1);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
if (pretty)
|
|
Packit Service |
3880ab |
jsonw_pretty(_jw, true);
|
|
Packit Service |
3880ab |
if (have_array)
|
|
Packit Service |
3880ab |
jsonw_start_array(_jw);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
static void __delete_json_obj(bool have_array)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (_jw) {
|
|
Packit Service |
3880ab |
if (have_array)
|
|
Packit Service |
3880ab |
jsonw_end_array(_jw);
|
|
Packit Service |
3880ab |
jsonw_destroy(&_jw);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void new_json_obj(int json)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
__new_json_obj(json, true);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void delete_json_obj(void)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
__delete_json_obj(true);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void new_json_obj_plain(int json)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
__new_json_obj(json, false);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void delete_json_obj_plain(void)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
__delete_json_obj(false);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
bool is_json_context(void)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
return _jw != NULL;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
json_writer_t *get_json_writer(void)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
return _jw;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void open_json_object(const char *str)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(PRINT_JSON)) {
|
|
Packit Service |
3880ab |
if (str)
|
|
Packit Service |
3880ab |
jsonw_name(_jw, str);
|
|
Packit Service |
3880ab |
jsonw_start_object(_jw);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void close_json_object(void)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(PRINT_JSON))
|
|
Packit Service |
3880ab |
jsonw_end_object(_jw);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* Start json array or string array using
|
|
Packit Service |
3880ab |
* the provided string as json key (if not null)
|
|
Packit Service |
3880ab |
* or as array delimiter in non-json context.
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
void open_json_array(enum output_type type, const char *str)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
if (str)
|
|
Packit Service |
3880ab |
jsonw_name(_jw, str);
|
|
Packit Service |
3880ab |
jsonw_start_array(_jw);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
printf("%s", str);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* End json array or string array
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
void close_json_array(enum output_type type, const char *str)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
jsonw_end_array(_jw);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
printf("%s", str);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* pre-processor directive to generate similar
|
|
Packit Service |
3880ab |
* functions handling different types
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
#define _PRINT_FUNC(type_name, type) \
|
|
Packit Service |
3880ab |
__attribute__((format(printf, 4, 0))) \
|
|
Packit Service |
3880ab |
int print_color_##type_name(enum output_type t, \
|
|
Packit Service |
3880ab |
enum color_attr color, \
|
|
Packit Service |
3880ab |
const char *key, \
|
|
Packit Service |
3880ab |
const char *fmt, \
|
|
Packit Service |
3880ab |
type value) \
|
|
Packit Service |
3880ab |
{ \
|
|
Packit Service |
3880ab |
int ret = 0; \
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(t)) { \
|
|
Packit Service |
3880ab |
if (!key) \
|
|
Packit Service |
3880ab |
jsonw_##type_name(_jw, value); \
|
|
Packit Service |
3880ab |
else \
|
|
Packit Service |
3880ab |
jsonw_##type_name##_field(_jw, key, value); \
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(t)) { \
|
|
Packit Service |
3880ab |
ret = color_fprintf(stdout, color, fmt, value); \
|
|
Packit Service |
3880ab |
} \
|
|
Packit Service |
3880ab |
return ret; \
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
_PRINT_FUNC(int, int);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(s64, int64_t);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(hhu, unsigned char);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(hu, unsigned short);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(uint, unsigned int);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(u64, uint64_t);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(luint, unsigned long);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(lluint, unsigned long long);
|
|
Packit Service |
3880ab |
_PRINT_FUNC(float, double);
|
|
Packit Service |
3880ab |
#undef _PRINT_FUNC
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
|
|
Packit Service |
3880ab |
void print_##type_name##_name_value(const char *name, type value)\
|
|
Packit Service |
3880ab |
{ \
|
|
Packit Service |
3880ab |
SPRINT_BUF(format); \
|
|
Packit Service |
3880ab |
\
|
|
Packit Service |
3880ab |
snprintf(format, SPRINT_BSIZE, \
|
|
Packit Service |
3880ab |
"%s %%"#format_char, name); \
|
|
Packit Service |
3880ab |
print_##type_name(PRINT_ANY, name, format, value); \
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
|
|
Packit Service |
3880ab |
_PRINT_NAME_VALUE_FUNC(string, const char*, s);
|
|
Packit Service |
3880ab |
#undef _PRINT_NAME_VALUE_FUNC
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
int print_color_string(enum output_type type,
|
|
Packit Service |
3880ab |
enum color_attr color,
|
|
Packit Service |
3880ab |
const char *key,
|
|
Packit Service |
3880ab |
const char *fmt,
|
|
Packit Service |
3880ab |
const char *value)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
int ret = 0;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
if (key && !value)
|
|
Packit Service |
3880ab |
jsonw_name(_jw, key);
|
|
Packit Service |
3880ab |
else if (!key && value)
|
|
Packit Service |
3880ab |
jsonw_string(_jw, value);
|
|
Packit Service |
3880ab |
else
|
|
Packit Service |
3880ab |
jsonw_string_field(_jw, key, value);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
ret = color_fprintf(stdout, color, fmt, value);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
return ret;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* value's type is bool. When using this function in FP context you can't pass
|
|
Packit Service |
3880ab |
* a value to it, you will need to use "is_json_context()" to have different
|
|
Packit Service |
3880ab |
* branch for json and regular output. grep -r "print_bool" for example
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
int print_color_bool(enum output_type type,
|
|
Packit Service |
3880ab |
enum color_attr color,
|
|
Packit Service |
3880ab |
const char *key,
|
|
Packit Service |
3880ab |
const char *fmt,
|
|
Packit Service |
3880ab |
bool value)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
int ret = 0;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
if (key)
|
|
Packit Service |
3880ab |
jsonw_bool_field(_jw, key, value);
|
|
Packit Service |
3880ab |
else
|
|
Packit Service |
3880ab |
jsonw_bool(_jw, value);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
ret = color_fprintf(stdout, color, fmt,
|
|
Packit Service |
3880ab |
value ? "true" : "false");
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
return ret;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* In JSON context uses hardcode %#x format: 42 -> 0x2a
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
int print_color_0xhex(enum output_type type,
|
|
Packit Service |
3880ab |
enum color_attr color,
|
|
Packit Service |
3880ab |
const char *key,
|
|
Packit Service |
3880ab |
const char *fmt,
|
|
Packit Service |
3880ab |
unsigned long long hex)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
int ret = 0;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
SPRINT_BUF(b1);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
snprintf(b1, sizeof(b1), "%#llx", hex);
|
|
Packit Service |
3880ab |
print_string(PRINT_JSON, key, NULL, b1);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
ret = color_fprintf(stdout, color, fmt, hex);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
return ret;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
int print_color_hex(enum output_type type,
|
|
Packit Service |
3880ab |
enum color_attr color,
|
|
Packit Service |
3880ab |
const char *key,
|
|
Packit Service |
3880ab |
const char *fmt,
|
|
Packit Service |
3880ab |
unsigned int hex)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
int ret = 0;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
SPRINT_BUF(b1);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
snprintf(b1, sizeof(b1), "%x", hex);
|
|
Packit Service |
3880ab |
if (key)
|
|
Packit Service |
3880ab |
jsonw_string_field(_jw, key, b1);
|
|
Packit Service |
3880ab |
else
|
|
Packit Service |
3880ab |
jsonw_string(_jw, b1);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
ret = color_fprintf(stdout, color, fmt, hex);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
return ret;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* In JSON context we don't use the argument "value" we simply call jsonw_null
|
|
Packit Service |
3880ab |
* whereas FP context can use "value" to output anything
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
int print_color_null(enum output_type type,
|
|
Packit Service |
3880ab |
enum color_attr color,
|
|
Packit Service |
3880ab |
const char *key,
|
|
Packit Service |
3880ab |
const char *fmt,
|
|
Packit Service |
3880ab |
const char *value)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
int ret = 0;
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
if (_IS_JSON_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
if (key)
|
|
Packit Service |
3880ab |
jsonw_null_field(_jw, key);
|
|
Packit Service |
3880ab |
else
|
|
Packit Service |
3880ab |
jsonw_null(_jw);
|
|
Packit Service |
3880ab |
} else if (_IS_FP_CONTEXT(type)) {
|
|
Packit Service |
3880ab |
ret = color_fprintf(stdout, color, fmt, value);
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
return ret;
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/* Print line separator (if not in JSON mode) */
|
|
Packit Service |
3880ab |
void print_nl(void)
|
|
Packit Service |
3880ab |
{
|
|
Packit Service |
3880ab |
if (!_jw)
|
|
Packit Service |
3880ab |
printf("%s", _SL_);
|
|
Packit Service |
3880ab |
}
|