iproute2/include/json_print.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/*
   3 * json_print.h         print regular or json output, based on json_writer.
   4 *
   5 * Authors:    Julien Fortin, <julien@cumulusnetworks.com>
   6 */
   7
   8#ifndef _JSON_PRINT_H_
   9#define _JSON_PRINT_H_
  10
  11#include "json_writer.h"
  12#include "color.h"
  13
  14#define _IS_JSON_CONTEXT(type) (is_json_context() && (type & PRINT_JSON || type & PRINT_ANY))
  15#define _IS_FP_CONTEXT(type)   (!is_json_context() && (type & PRINT_FP || type & PRINT_ANY))
  16
  17json_writer_t *get_json_writer(void);
  18
  19/*
  20 * use:
  21 *      - PRINT_ANY for context based output
  22 *      - PRINT_FP for non json specific output
  23 *      - PRINT_JSON for json specific output
  24 */
  25enum output_type {
  26        PRINT_FP = 1,
  27        PRINT_JSON = 2,
  28        PRINT_ANY = 4,
  29};
  30
  31void new_json_obj(int json);
  32void delete_json_obj(void);
  33void new_json_obj_plain(int json);
  34void delete_json_obj_plain(void);
  35
  36bool is_json_context(void);
  37
  38void open_json_object(const char *str);
  39void close_json_object(void);
  40void open_json_array(enum output_type type, const char *delim);
  41void close_json_array(enum output_type type, const char *delim);
  42
  43void print_nl(void);
  44
  45#define _PRINT_FUNC(type_name, type)                                    \
  46        int print_color_##type_name(enum output_type t,                 \
  47                                    enum color_attr color,              \
  48                                    const char *key,                    \
  49                                    const char *fmt,                    \
  50                                    type value);                        \
  51                                                                        \
  52        static inline int print_##type_name(enum output_type t,         \
  53                                            const char *key,            \
  54                                            const char *fmt,            \
  55                                            type value)                 \
  56        {                                                               \
  57                return print_color_##type_name(t, COLOR_NONE, key, fmt, \
  58                                               value);                  \
  59        }
  60
  61/* These functions return 0 if printing to a JSON context, number of
  62 * characters printed otherwise (as calculated by printf(3)).
  63 */
  64_PRINT_FUNC(int, int)
  65_PRINT_FUNC(s64, int64_t)
  66_PRINT_FUNC(bool, bool)
  67_PRINT_FUNC(on_off, bool)
  68_PRINT_FUNC(null, const char*)
  69_PRINT_FUNC(string, const char*)
  70_PRINT_FUNC(uint, unsigned int)
  71_PRINT_FUNC(size, __u32)
  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)
  80_PRINT_FUNC(tv, const struct timeval *)
  81#undef _PRINT_FUNC
  82
  83#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char)              \
  84        void print_##type_name##_name_value(const char *name, type value) \
  85
  86_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
  87_PRINT_NAME_VALUE_FUNC(string, const char*, s);
  88#undef _PRINT_NAME_VALUE_FUNC
  89
  90int print_color_rate(bool use_iec, enum output_type t, enum color_attr color,
  91                     const char *key, const char *fmt, unsigned long long rate);
  92
  93static inline int print_rate(bool use_iec, enum output_type t,
  94                             const char *key, const char *fmt,
  95                             unsigned long long rate)
  96{
  97        return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
  98}
  99
 100unsigned int print_range(const char *name, __u32 start, __u32 end);
 101
 102int print_color_bool_opt(enum output_type type, enum color_attr color,
 103                         const char *key, bool value, bool show);
 104
 105static inline int print_bool_opt(enum output_type type,
 106                                 const char *key, bool value, bool show)
 107{
 108        return print_color_bool_opt(type, COLOR_NONE, key, value, show);
 109}
 110
 111/* A backdoor to the size formatter. Please use print_size() instead. */
 112char *sprint_size(__u32 sz, char *buf);
 113
 114#endif /* _JSON_PRINT_H_ */
 115