summaryrefslogtreecommitdiff
path: root/lib/json_print.c
blob: d7ee76b10de889dca155bbbddd3dc3600726999a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * json_print.c	 - print regular or json output, based on json_writer
 *
 * Authors:    Julien Fortin, <julien@cumulusnetworks.com>
 */

#include <stdarg.h>
#include <stdio.h>

#include "utils.h"
#include "json_print.h"

static json_writer_t *_jw;

static void __new_json_obj(int json, bool have_array)
{
	if (json) {
		_jw = jsonw_new(stdout);
		if (!_jw) {
			perror("json object");
			exit(1);
		}
		if (pretty)
			jsonw_pretty(_jw, true);
		if (have_array)
			jsonw_start_array(_jw);
	}
}

static void __delete_json_obj(bool have_array)
{
	if (_jw) {
		if (have_array)
			jsonw_end_array(_jw);
		jsonw_destroy(&_jw);
	}
}

void new_json_obj(int json)
{
	__new_json_obj(json, true);
}

void delete_json_obj(void)
{
	__delete_json_obj(true);
}

void new_json_obj_plain(int json)
{
	__new_json_obj(json, false);
}

void delete_json_obj_plain(void)
{
	__delete_json_obj(false);
}

bool is_json_context(void)
{
	return _jw != NULL;
}

json_writer_t *get_json_writer(void)
{
	return _jw;
}

void open_json_object(const char *str)
{
	if (_IS_JSON_CONTEXT(PRINT_JSON)) {
		if (str)
			jsonw_name(_jw, str);
		jsonw_start_object(_jw);
	}
}

void close_json_object(void)
{
	if (_IS_JSON_CONTEXT(PRINT_JSON))
		jsonw_end_object(_jw);
}

/*
 * Start json array or string array using
 * the provided string as json key (if not null)
 * or as array delimiter in non-json context.
 */
void open_json_array(enum output_type type, const char *str)
{
	if (_IS_JSON_CONTEXT(type)) {
		if (str)
			jsonw_name(_jw, str);
		jsonw_start_array(_jw);
	} else if (_IS_FP_CONTEXT(type)) {
		printf("%s", str);
	}
}

/*
 * End json array or string array
 */
void close_json_array(enum output_type type, const char *str)
{
	if (_IS_JSON_CONTEXT(type)) {
		jsonw_end_array(_jw);
	} else if (_IS_FP_CONTEXT(type)) {
		printf("%s", str);
	}
}

/*
 * pre-processor directive to generate similar
 * functions handling different types
 */
#define _PRINT_FUNC(type_name, type)					\
	__attribute__((format(printf, 4, 0)))				\
	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)) {				\
			ret = color_fprintf(stdout, color, fmt, value); \
		}							\
		return ret;						\
	}
_PRINT_FUNC(int, int);
_PRINT_FUNC(s64, int64_t);
_PRINT_FUNC(hhu, unsigned char);
_PRINT_FUNC(hu, unsigned short);
_PRINT_FUNC(uint, unsigned int);
_PRINT_FUNC(u64, uint64_t);
_PRINT_FUNC(luint, unsigned long);
_PRINT_FUNC(lluint, unsigned long long);
_PRINT_FUNC(float, double);
#undef _PRINT_FUNC

#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char)		 \
	void print_##type_name##_name_value(const char *name, type value)\
	{								 \
		SPRINT_BUF(format);					 \
									 \
		snprintf(format, SPRINT_BSIZE,				 \
			 "%s %%"#format_char, name);			 \
		print_##type_name(PRINT_ANY, name, format, value);	 \
	}
_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
_PRINT_NAME_VALUE_FUNC(string, const char*, s);
#undef _PRINT_NAME_VALUE_FUNC

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);
		else if (!key && value)
			jsonw_string(_jw, value);
		else
			jsonw_string_field(_jw, key, value);
	} else if (_IS_FP_CONTEXT(type)) {
		ret = color_fprintf(stdout, color, fmt, value);
	}

	return ret;
}

/*
 * value's type is bool. When using this function in FP context you can't pass
 * 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
 */
static int __print_color_bool(enum output_type type,
			      enum color_attr color,
			      const char *key,
			      const char *fmt,
			      bool value,
			      const char *str)
{
	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)) {
		ret = color_fprintf(stdout, color, fmt, str);
	}

	return ret;
}

int print_color_bool(enum output_type type,
		     enum color_attr color,
		     const char *key,
		     const char *fmt,
		     bool value)
{
	return __print_color_bool(type, color, key, fmt, value,
				  value ? "true" : "false");
}

int print_color_on_off(enum output_type type,
		       enum color_attr color,
		       const char *key,
		       const char *fmt,
		       bool value)
{
	return __print_color_bool(type, color, key, fmt, value,
				  value ? "on" : "off");
}

/*
 * In JSON context uses hardcode %#x format: 42 -> 0x2a
 */
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)) {
		ret = color_fprintf(stdout, color, fmt, hex);
	}

	return ret;
}

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);

		snprintf(b1, sizeof(b1), "%x", hex);
		if (key)
			jsonw_string_field(_jw, key, b1);
		else
			jsonw_string(_jw, b1);
	} else if (_IS_FP_CONTEXT(type)) {
		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
 */
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)) {
		ret = color_fprintf(stdout, color, fmt, value);
	}

	return ret;
}

/*
 * This function does take printf style argument but applying
 * format attribute to causes more warnings since the print_XXX
 * functions are used with NULL for format if unused.
 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
int print_color_tv(enum output_type type,
		   enum color_attr color,
		   const char *key,
		   const char *fmt,
		   const struct timeval *tv)
{
	double usecs = tv->tv_usec;
	double secs = tv->tv_sec;
	double time = secs + usecs / 1000000;

	return print_color_float(type, color, key, fmt, time);
}
#pragma GCC diagnostic pop

/* Print line separator (if not in JSON mode) */
void print_nl(void)
{
	if (!_jw)
		printf("%s", _SL_);
}

int print_color_rate(bool use_iec, enum output_type type, enum color_attr color,
		     const char *key, const char *fmt, unsigned long long rate)
{
	unsigned long kilo = use_iec ? 1024 : 1000;
	const char *str = use_iec ? "i" : "";
	static char *units[5] = {"", "K", "M", "G", "T"};
	char *buf;
	int rc;
	int i;

	if (_IS_JSON_CONTEXT(type))
		return print_color_lluint(type, color, key, "%llu", rate);

	rate <<= 3; /* bytes/sec -> bits/sec */

	for (i = 0; i < ARRAY_SIZE(units) - 1; i++)  {
		if (rate < kilo)
			break;
		if (((rate % kilo) != 0) && rate < 1000*kilo)
			break;
		rate /= kilo;
	}

	rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i],
		      i > 0 ? str : "");
	if (rc < 0)
		return -1;

	rc = print_color_string(type, color, key, fmt, buf);
	free(buf);
	return rc;
}