Blame include/json_print.h

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 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 Service 3880ab
_PRINT_FUNC(null, const char*)
Packit Service 3880ab
_PRINT_FUNC(string, const char*)
Packit Service 3880ab
_PRINT_FUNC(uint, unsigned int)
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 Service 3880ab
#endif /* _JSON_PRINT_H_ */