mirror of
https://git.proxmox.com/git/mirror_iproute2
synced 2025-10-04 15:54:54 +00:00

The functions print_rate() and sprint_rate() are useful for formatting rate-like values. The DCB tool would find these useful in the maxrate subtool. However, the current interface to these functions uses a global variable use_iec as a flag indicating whether 1024- or 1000-based powers should be used when formatting the rate value. For general use, a global variable is not a great way of passing arguments to a function. Besides, it is unlike most other printing functions in that it deals in buffers and ignores JSON. Therefore make the interface to print_rate() explicit by converting use_iec to an ordinary parameter. Since the interface changes anyway, convert it to follow the pattern of other json_print functions (except for the now-explicit use_iec parameter). Move to json_print.c. Add a wrapper to tc, so that all the call sites do not need to repeat the use_iec global variable argument, and convert all call sites. In q_cake.c, the conversion is not straightforward due to usage of a macro that is shared across numerous data types. Simply hand-roll the corresponding code, which seems better than making an extra helper for one call site. Drop sprint_rate() now that everybody just uses print_rate(). Signed-off-by: Petr Machata <me@pmachata.org> Signed-off-by: David Ahern <dsahern@gmail.com>
100 lines
2.8 KiB
C
100 lines
2.8 KiB
C
/*
|
|
* json_print.h "print regular or json output, based on json_writer".
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*
|
|
* Authors: Julien Fortin, <julien@cumulusnetworks.com>
|
|
*/
|
|
|
|
#ifndef _JSON_PRINT_H_
|
|
#define _JSON_PRINT_H_
|
|
|
|
#include "json_writer.h"
|
|
#include "color.h"
|
|
|
|
json_writer_t *get_json_writer(void);
|
|
|
|
/*
|
|
* use:
|
|
* - PRINT_ANY for context based output
|
|
* - PRINT_FP for non json specific output
|
|
* - PRINT_JSON for json specific output
|
|
*/
|
|
enum output_type {
|
|
PRINT_FP = 1,
|
|
PRINT_JSON = 2,
|
|
PRINT_ANY = 4,
|
|
};
|
|
|
|
void new_json_obj(int json);
|
|
void delete_json_obj(void);
|
|
void new_json_obj_plain(int json);
|
|
void delete_json_obj_plain(void);
|
|
|
|
bool is_json_context(void);
|
|
|
|
void open_json_object(const char *str);
|
|
void close_json_object(void);
|
|
void open_json_array(enum output_type type, const char *delim);
|
|
void close_json_array(enum output_type type, const char *delim);
|
|
|
|
void print_nl(void);
|
|
|
|
#define _PRINT_FUNC(type_name, type) \
|
|
int print_color_##type_name(enum output_type t, \
|
|
enum color_attr color, \
|
|
const char *key, \
|
|
const char *fmt, \
|
|
type value); \
|
|
\
|
|
static inline int print_##type_name(enum output_type t, \
|
|
const char *key, \
|
|
const char *fmt, \
|
|
type value) \
|
|
{ \
|
|
return print_color_##type_name(t, COLOR_NONE, key, fmt, \
|
|
value); \
|
|
}
|
|
|
|
/* These functions return 0 if printing to a JSON context, number of
|
|
* characters printed otherwise (as calculated by printf(3)).
|
|
*/
|
|
_PRINT_FUNC(int, int)
|
|
_PRINT_FUNC(s64, int64_t)
|
|
_PRINT_FUNC(bool, bool)
|
|
_PRINT_FUNC(on_off, bool)
|
|
_PRINT_FUNC(null, const char*)
|
|
_PRINT_FUNC(string, const char*)
|
|
_PRINT_FUNC(uint, unsigned int)
|
|
_PRINT_FUNC(u64, uint64_t)
|
|
_PRINT_FUNC(hhu, unsigned char)
|
|
_PRINT_FUNC(hu, unsigned short)
|
|
_PRINT_FUNC(hex, unsigned int)
|
|
_PRINT_FUNC(0xhex, unsigned long long)
|
|
_PRINT_FUNC(luint, unsigned long)
|
|
_PRINT_FUNC(lluint, unsigned long long)
|
|
_PRINT_FUNC(float, double)
|
|
#undef _PRINT_FUNC
|
|
|
|
#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \
|
|
void print_##type_name##_name_value(const char *name, type value) \
|
|
|
|
_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u);
|
|
_PRINT_NAME_VALUE_FUNC(string, const char*, s);
|
|
#undef _PRINT_NAME_VALUE_FUNC
|
|
|
|
int print_color_rate(bool use_iec, enum output_type t, enum color_attr color,
|
|
const char *key, const char *fmt, unsigned long long rate);
|
|
|
|
static inline int print_rate(bool use_iec, enum output_type t,
|
|
const char *key, const char *fmt,
|
|
unsigned long long rate)
|
|
{
|
|
return print_color_rate(use_iec, t, COLOR_NONE, key, fmt, rate);
|
|
}
|
|
|
|
#endif /* _JSON_PRINT_H_ */
|