1
2
3
4
5
6
7
8
9
10
11
12#ifndef _JSON_PRINT_H_
13#define _JSON_PRINT_H_
14
15#include "json_writer.h"
16#include "color.h"
17
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
21json_writer_t *get_json_writer(void);
22
23
24
25
26
27
28
29enum output_type {
30 PRINT_FP = 1,
31 PRINT_JSON = 2,
32 PRINT_ANY = 4,
33};
34
35void new_json_obj(int json);
36void delete_json_obj(void);
37void new_json_obj_plain(int json);
38void delete_json_obj_plain(void);
39
40bool is_json_context(void);
41
42void open_json_object(const char *str);
43void close_json_object(void);
44void open_json_array(enum output_type type, const char *delim);
45void close_json_array(enum output_type type, const char *delim);
46
47void print_nl(void);
48
49#define _PRINT_FUNC(type_name, type) \
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); \
55 \
56 static inline int print_##type_name(enum output_type t, \
57 const char *key, \
58 const char *fmt, \
59 type value) \
60 { \
61 return print_color_##type_name(t, COLOR_NONE, key, fmt, \
62 value); \
63 }
64
65
66
67
68_PRINT_FUNC(int, int)
69_PRINT_FUNC(s64, int64_t)
70_PRINT_FUNC(bool, bool)
71_PRINT_FUNC(on_off, bool)
72_PRINT_FUNC(null, const char*)
73_PRINT_FUNC(string, const char*)
74_PRINT_FUNC(uint, unsigned int)
75_PRINT_FUNC(size, __u32)
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)
84_PRINT_FUNC(tv, const struct timeval *)
85#undef _PRINT_FUNC
86
87#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
88 void print_##type_name##_name_value(const char *name, type value) \
89
90_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
91_PRINT_NAME_VALUE_FUNC(string, const char*, s);
92#undef _PRINT_NAME_VALUE_FUNC
93
94int print_color_rate(bool use_iec, enum output_type t, enum color_attr color,
95 const char *key, const char *fmt, unsigned long long rate);
96
97static inline int print_rate(bool use_iec, enum output_type t,
98 const char *key, const char *fmt,
99 unsigned long long rate)
100{
101 return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
102}
103
104
105char *sprint_size(__u32 sz, char *buf);
106
107#endif
108