|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* json_print.h "print regular or json output, based on json_writer".
|
|
Packit Service |
3880ab |
*
|
|
Packit Service |
3880ab |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
3880ab |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
3880ab |
* as published by the Free Software Foundation; either version
|
|
Packit Service |
3880ab |
* 2 of the License, or (at your option) any later version.
|
|
Packit Service |
3880ab |
*
|
|
Packit Service |
3880ab |
* Authors: Julien Fortin, <julien@cumulusnetworks.com>
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#ifndef _JSON_PRINT_H_
|
|
Packit Service |
3880ab |
#define _JSON_PRINT_H_
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#include "json_writer.h"
|
|
Packit Service |
3880ab |
#include "color.h"
|
|
Packit Service |
3880ab |
|
|
Packit Bot |
867fae |
#define _IS_JSON_CONTEXT(type) (is_json_context() && (type & PRINT_JSON || type & PRINT_ANY))
|
|
Packit Bot |
867fae |
#define _IS_FP_CONTEXT(type) (!is_json_context() && (type & PRINT_FP || type & PRINT_ANY))
|
|
Packit Bot |
867fae |
|
|
Packit Service |
3880ab |
json_writer_t *get_json_writer(void);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/*
|
|
Packit Service |
3880ab |
* use:
|
|
Packit Service |
3880ab |
* - PRINT_ANY for context based output
|
|
Packit Service |
3880ab |
* - PRINT_FP for non json specific output
|
|
Packit Service |
3880ab |
* - PRINT_JSON for json specific output
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
enum output_type {
|
|
Packit Service |
3880ab |
PRINT_FP = 1,
|
|
Packit Service |
3880ab |
PRINT_JSON = 2,
|
|
Packit Service |
3880ab |
PRINT_ANY = 4,
|
|
Packit Service |
3880ab |
};
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void new_json_obj(int json);
|
|
Packit Service |
3880ab |
void delete_json_obj(void);
|
|
Packit Service |
3880ab |
void new_json_obj_plain(int json);
|
|
Packit Service |
3880ab |
void delete_json_obj_plain(void);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
bool is_json_context(void);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void open_json_object(const char *str);
|
|
Packit Service |
3880ab |
void close_json_object(void);
|
|
Packit Service |
3880ab |
void open_json_array(enum output_type type, const char *delim);
|
|
Packit Service |
3880ab |
void close_json_array(enum output_type type, const char *delim);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
void print_nl(void);
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#define _PRINT_FUNC(type_name, type) \
|
|
Packit Service |
3880ab |
int print_color_##type_name(enum output_type t, \
|
|
Packit Service |
3880ab |
enum color_attr color, \
|
|
Packit Service |
3880ab |
const char *key, \
|
|
Packit Service |
3880ab |
const char *fmt, \
|
|
Packit Service |
3880ab |
type value); \
|
|
Packit Service |
3880ab |
\
|
|
Packit Service |
3880ab |
static inline int print_##type_name(enum output_type t, \
|
|
Packit Service |
3880ab |
const char *key, \
|
|
Packit Service |
3880ab |
const char *fmt, \
|
|
Packit Service |
3880ab |
type value) \
|
|
Packit Service |
3880ab |
{ \
|
|
Packit Service |
3880ab |
return print_color_##type_name(t, COLOR_NONE, key, fmt, \
|
|
Packit Service |
3880ab |
value); \
|
|
Packit Service |
3880ab |
}
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
/* These functions return 0 if printing to a JSON context, number of
|
|
Packit Service |
3880ab |
* characters printed otherwise (as calculated by printf(3)).
|
|
Packit Service |
3880ab |
*/
|
|
Packit Service |
3880ab |
_PRINT_FUNC(int, int)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(s64, int64_t)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(bool, bool)
|
|
Packit Bot |
867fae |
_PRINT_FUNC(on_off, bool)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(null, const char*)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(string, const char*)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(uint, unsigned int)
|
|
Packit Bot |
867fae |
_PRINT_FUNC(size, __u32)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(u64, uint64_t)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(hhu, unsigned char)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(hu, unsigned short)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(hex, unsigned int)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(0xhex, unsigned long long)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(luint, unsigned long)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(lluint, unsigned long long)
|
|
Packit Service |
3880ab |
_PRINT_FUNC(float, double)
|
|
Packit Service |
3880ab |
#undef _PRINT_FUNC
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
|
|
Packit Service |
3880ab |
void print_##type_name##_name_value(const char *name, type value) \
|
|
Packit Service |
3880ab |
|
|
Packit Service |
3880ab |
_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
|
|
Packit Service |
3880ab |
_PRINT_NAME_VALUE_FUNC(string, const char*, s);
|
|
Packit Service |
3880ab |
#undef _PRINT_NAME_VALUE_FUNC
|
|
Packit Service |
3880ab |
|
|
Packit Bot |
867fae |
int print_color_rate(bool use_iec, enum output_type t, enum color_attr color,
|
|
Packit Bot |
867fae |
const char *key, const char *fmt, unsigned long long rate);
|
|
Packit Bot |
867fae |
|
|
Packit Bot |
867fae |
static inline int print_rate(bool use_iec, enum output_type t,
|
|
Packit Bot |
867fae |
const char *key, const char *fmt,
|
|
Packit Bot |
867fae |
unsigned long long rate)
|
|
Packit Bot |
867fae |
{
|
|
Packit Bot |
867fae |
return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
|
|
Packit Bot |
867fae |
}
|
|
Packit Bot |
867fae |
|
|
Packit Bot |
867fae |
/* A backdoor to the size formatter. Please use print_size() instead. */
|
|
Packit Bot |
867fae |
char *sprint_size(__u32 sz, char *buf);
|
|
Packit Bot |
867fae |
|
|
Packit Service |
3880ab |
#endif /* _JSON_PRINT_H_ */
|