]> git.proxmox.com Git - mirror_iproute2.git/blame - include/json_print.h
lib: Move print_rate() from tc here; modernize
[mirror_iproute2.git] / include / json_print.h
CommitLineData
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
18json_writer_t *get_json_writer(void);
19
20/*
21 * use:
22 * - PRINT_ANY for context based output
23 * - PRINT_FP for non json specific output
24 * - PRINT_JSON for json specific output
25 */
26enum output_type {
27 PRINT_FP = 1,
28 PRINT_JSON = 2,
29 PRINT_ANY = 4,
30};
31
429f314e 32void new_json_obj(int json);
0b4b35e1 33void delete_json_obj(void);
98e48e7d
RD
34void new_json_obj_plain(int json);
35void delete_json_obj_plain(void);
0b4b35e1
DB
36
37bool is_json_context(void);
38
0b4b35e1
DB
39void open_json_object(const char *str);
40void close_json_object(void);
41void open_json_array(enum output_type type, const char *delim);
42void close_json_array(enum output_type type, const char *delim);
43
b85076cd
SH
44void print_nl(void);
45
0b4b35e1 46#define _PRINT_FUNC(type_name, type) \
5a07a5df
BP
47 int print_color_##type_name(enum output_type t, \
48 enum color_attr color, \
49 const char *key, \
50 const char *fmt, \
51 type value); \
0b4b35e1 52 \
5a07a5df
BP
53 static inline int print_##type_name(enum output_type t, \
54 const char *key, \
55 const char *fmt, \
56 type value) \
0b4b35e1 57 { \
5a07a5df
BP
58 return print_color_##type_name(t, COLOR_NONE, key, fmt, \
59 value); \
0b4b35e1 60 }
067925e2 61
5a07a5df
BP
62/* These functions return 0 if printing to a JSON context, number of
63 * characters printed otherwise (as calculated by printf(3)).
64 */
067925e2
SH
65_PRINT_FUNC(int, int)
66_PRINT_FUNC(s64, int64_t)
67_PRINT_FUNC(bool, bool)
9091ff02 68_PRINT_FUNC(on_off, bool)
067925e2
SH
69_PRINT_FUNC(null, const char*)
70_PRINT_FUNC(string, const char*)
71_PRINT_FUNC(uint, unsigned int)
72_PRINT_FUNC(u64, uint64_t)
73_PRINT_FUNC(hhu, unsigned char)
74_PRINT_FUNC(hu, unsigned short)
75_PRINT_FUNC(hex, unsigned int)
76_PRINT_FUNC(0xhex, unsigned long long)
77_PRINT_FUNC(luint, unsigned long)
78_PRINT_FUNC(lluint, unsigned long long)
79_PRINT_FUNC(float, double)
0b4b35e1
DB
80#undef _PRINT_FUNC
81
31ca29b2
RD
82#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
83 void print_##type_name##_name_value(const char *name, type value) \
84
85_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
86_PRINT_NAME_VALUE_FUNC(string, const char*, s);
87#undef _PRINT_NAME_VALUE_FUNC
88
60265cc2
PM
89int print_color_rate(bool use_iec, enum output_type t, enum color_attr color,
90 const char *key, const char *fmt, unsigned long long rate);
91
92static inline int print_rate(bool use_iec, enum output_type t,
93 const char *key, const char *fmt,
94 unsigned long long rate)
95{
96 return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
97}
98
0b4b35e1 99#endif /* _JSON_PRINT_H_ */