]>
Commit | Line | Data |
---|---|---|
0b4b35e1 DB |
1 | /* |
2 | * json_print.h "print regular or json output, based on json_writer". | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Julien Fortin, <julien@cumulusnetworks.com> | |
10 | */ | |
11 | ||
12 | #ifndef _JSON_PRINT_H_ | |
13 | #define _JSON_PRINT_H_ | |
14 | ||
15 | #include "json_writer.h" | |
16 | #include "color.h" | |
17 | ||
1a22ad27 RD |
18 | #define _IS_JSON_CONTEXT(type) (is_json_context() && (type & PRINT_JSON || type & PRINT_ANY)) |
19 | #define _IS_FP_CONTEXT(type) (!is_json_context() && (type & PRINT_FP || type & PRINT_ANY)) | |
20 | ||
0b4b35e1 DB |
21 | json_writer_t *get_json_writer(void); |
22 | ||
23 | /* | |
24 | * use: | |
25 | * - PRINT_ANY for context based output | |
26 | * - PRINT_FP for non json specific output | |
27 | * - PRINT_JSON for json specific output | |
28 | */ | |
29 | enum output_type { | |
30 | PRINT_FP = 1, | |
31 | PRINT_JSON = 2, | |
32 | PRINT_ANY = 4, | |
33 | }; | |
34 | ||
429f314e | 35 | void new_json_obj(int json); |
0b4b35e1 | 36 | void delete_json_obj(void); |
98e48e7d RD |
37 | void new_json_obj_plain(int json); |
38 | void delete_json_obj_plain(void); | |
0b4b35e1 DB |
39 | |
40 | bool is_json_context(void); | |
41 | ||
0b4b35e1 DB |
42 | void open_json_object(const char *str); |
43 | void close_json_object(void); | |
44 | void open_json_array(enum output_type type, const char *delim); | |
45 | void close_json_array(enum output_type type, const char *delim); | |
46 | ||
b85076cd SH |
47 | void print_nl(void); |
48 | ||
0b4b35e1 | 49 | #define _PRINT_FUNC(type_name, type) \ |
5a07a5df BP |
50 | int print_color_##type_name(enum output_type t, \ |
51 | enum color_attr color, \ | |
52 | const char *key, \ | |
53 | const char *fmt, \ | |
54 | type value); \ | |
0b4b35e1 | 55 | \ |
5a07a5df BP |
56 | static inline int print_##type_name(enum output_type t, \ |
57 | const char *key, \ | |
58 | const char *fmt, \ | |
59 | type value) \ | |
0b4b35e1 | 60 | { \ |
5a07a5df BP |
61 | return print_color_##type_name(t, COLOR_NONE, key, fmt, \ |
62 | value); \ | |
0b4b35e1 | 63 | } |
067925e2 | 64 | |
5a07a5df BP |
65 | /* These functions return 0 if printing to a JSON context, number of |
66 | * characters printed otherwise (as calculated by printf(3)). | |
67 | */ | |
067925e2 SH |
68 | _PRINT_FUNC(int, int) |
69 | _PRINT_FUNC(s64, int64_t) | |
70 | _PRINT_FUNC(bool, bool) | |
9091ff02 | 71 | _PRINT_FUNC(on_off, bool) |
067925e2 SH |
72 | _PRINT_FUNC(null, const char*) |
73 | _PRINT_FUNC(string, const char*) | |
74 | _PRINT_FUNC(uint, unsigned int) | |
adbe5de9 | 75 | _PRINT_FUNC(size, __u32) |
067925e2 SH |
76 | _PRINT_FUNC(u64, uint64_t) |
77 | _PRINT_FUNC(hhu, unsigned char) | |
78 | _PRINT_FUNC(hu, unsigned short) | |
79 | _PRINT_FUNC(hex, unsigned int) | |
80 | _PRINT_FUNC(0xhex, unsigned long long) | |
81 | _PRINT_FUNC(luint, unsigned long) | |
82 | _PRINT_FUNC(lluint, unsigned long long) | |
83 | _PRINT_FUNC(float, double) | |
0b4b35e1 DB |
84 | #undef _PRINT_FUNC |
85 | ||
31ca29b2 RD |
86 | #define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \ |
87 | void print_##type_name##_name_value(const char *name, type value) \ | |
88 | ||
89 | _PRINT_NAME_VALUE_FUNC(uint, unsigned int, u); | |
90 | _PRINT_NAME_VALUE_FUNC(string, const char*, s); | |
91 | #undef _PRINT_NAME_VALUE_FUNC | |
92 | ||
60265cc2 PM |
93 | int print_color_rate(bool use_iec, enum output_type t, enum color_attr color, |
94 | const char *key, const char *fmt, unsigned long long rate); | |
95 | ||
96 | static inline int print_rate(bool use_iec, enum output_type t, | |
97 | const char *key, const char *fmt, | |
98 | unsigned long long rate) | |
99 | { | |
100 | return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate); | |
101 | } | |
102 | ||
adbe5de9 PM |
103 | /* A backdoor to the size formatter. Please use print_size() instead. */ |
104 | char *sprint_size(__u32 sz, char *buf); | |
105 | ||
0b4b35e1 | 106 | #endif /* _JSON_PRINT_H_ */ |