MT#55283 refactor stats macros

Use proper functions instead of macro madness.

Change-Id: Ia557cc88d890c38f726a0a02cb0185952710d8ee
pull/1611/head
Richard Fuchs 2 years ago
parent 2cfdf05f10
commit a0743f24ea

@ -1,4 +1,5 @@
#include <math.h> #include <math.h>
#include <stdarg.h>
#include "call.h" #include "call.h"
#include "statistics.h" #include "statistics.h"
#include "graphite.h" #include "graphite.h"
@ -182,145 +183,113 @@ found:;
} }
#pragma GCC diagnostic ignored "-Wformat-zero-length"
INLINE void prom_metric(GQueue *ret, const char *name, const char *type) {
#define SM_PUSH(ret, m) \ struct stats_metric *last = g_queue_peek_tail(ret);
do { \ last->prom_name = name;
struct stats_metric *last = NULL; \ last->prom_type = type;
for (GList *l_last = ret->tail; l_last; l_last = l_last->prev) { \ }
last = l_last->data; \ static void prom_label(GQueue *ret, const char *fmt, ...) {
if (last->label) \ va_list ap;
break; \ va_start(ap, fmt);
last = NULL; \ struct stats_metric *last = g_queue_peek_tail(ret);
} \ last->prom_label = g_strdup_vprintf(fmt, ap);
if (!m->is_bracket && last) { \ va_end(ap);
if (!last->is_bracket || last->is_close_bracket) \ }
m->is_follow_up = 1; \ #define PROM(name, type) prom_metric(ret, name, type)
} \ #define PROMLAB(fmt, ...) prom_label(ret, fmt, ##__VA_ARGS__)
else if (m->is_bracket && !m->is_close_bracket && last && last->is_close_bracket) \
m->is_follow_up = 1; \ INLINE void metric_push(GQueue *ret, struct stats_metric *m) {
g_queue_push_tail(ret, m); \ struct stats_metric *last = NULL;
} while (0) for (GList *l_last = ret->tail; l_last; l_last = l_last->prev) {
last = l_last->data;
#define PROM(name, type) \ if (last->label)
do { \ break;
struct stats_metric *last = g_queue_peek_tail(ret); \ last = NULL;
last->prom_name = name; \ }
last->prom_type = type; \ if (!m->is_bracket && last) {
} while (0) if (!last->is_bracket || last->is_close_bracket)
#define PROMLAB(fmt, ...) \ m->is_follow_up = 1;
do { \ }
struct stats_metric *last = g_queue_peek_tail(ret); \ else if (m->is_bracket && !m->is_close_bracket && last && last->is_close_bracket)
last->prom_label = g_strdup_printf(fmt, ## __VA_ARGS__); \ m->is_follow_up = 1;
} while (0) g_queue_push_tail(ret, m);
}
#define METRICva(lb, dsc, fmt1, fmt2, ...) \ static void add_metric(GQueue *ret, const char *label, const char *desc, const char *fmt1, const char *fmt2, ...) {
do { \ va_list ap;
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \
m->label = g_strdup(lb); \ struct stats_metric *m = g_slice_alloc0(sizeof(*m));
m->descr = g_strdup(dsc); \ if (label)
if (fmt1) \ m->label = g_strdup(label);
m->value_short = g_strdup_printf(fmt1, ## __VA_ARGS__); \ if (desc)
if (fmt2) \ m->descr = g_strdup(desc);
m->value_long = g_strdup_printf(fmt2, ## __VA_ARGS__); \ if (fmt1) {
SM_PUSH(ret, m); \ va_start(ap, fmt2);
} while (0) m->value_short = g_strdup_vprintf(fmt1, ap);
va_end(ap);
#define METRIC(lb, dsc, fmt1, fmt2, arg) \ }
do { \ if (fmt2) {
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \ va_start(ap, fmt2);
m->label = g_strdup(lb); \ m->value_long = g_strdup_vprintf(fmt2, ap);
m->descr = g_strdup(dsc); \ va_end(ap);
if (fmt1) \ }
m->value_short = g_strdup_printf(fmt1, arg); \ if (fmt1 && fmt1[0] == '%' && (!fmt2 || !strcmp(fmt1, fmt2))) {
if (fmt2) \ va_start(ap, fmt2);
m->value_long = g_strdup_printf(fmt2, arg); \ if (!strcmp(fmt1, "%u") || !strcmp(fmt1, "%i") || !strcmp(fmt1, "%d")) {
if (fmt1 && fmt2 && !strcmp(fmt1, fmt2)) { \ m->is_int = 1;
if (!strcmp(fmt1, "%u") || \ m->int_value = va_arg(ap, int);
!strcmp(fmt1, "%lu") || \ }
!strcmp(fmt1, "%llu") || \ else if (!strcmp(fmt1, "%lu") || !strcmp(fmt1, "%li") || !strcmp(fmt1, "%ld")) {
!strcmp(fmt1, "%i") || \ m->is_int = 1;
!strcmp(fmt1, "%li") || \ m->int_value = va_arg(ap, long);
!strcmp(fmt1, "%lli") || \ }
!strcmp(fmt1, "%d") || \ else if ( !strcmp(fmt1, "%llu") || !strcmp(fmt1, "%lli") || !strcmp(fmt1, "%lld")) {
!strcmp(fmt1, "%ld") || \ m->is_int = 1;
!strcmp(fmt1, "%lld")) \ m->int_value = va_arg(ap, long long);
{ \ }
m->is_int = 1; \ va_end(ap);
m->int_value = arg; \ }
} \ metric_push(ret, m);
} \ }
SM_PUSH(ret, m); \ static void add_header(GQueue *ret, const char *fmt1, const char *fmt2, ...) {
} while (0) va_list ap;
#define METRICl(dsc, fmt2, ...) \ struct stats_metric *m = g_slice_alloc0(sizeof(*m));
do { \ if (fmt1) {
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \ va_start(ap, fmt2);
m->descr = g_strdup(dsc); \ m->label = g_strdup_vprintf(fmt1, ap);
m->value_long = g_strdup_printf(fmt2, ## __VA_ARGS__); \ va_end(ap);
SM_PUSH(ret, m); \ }
} while (0) if (fmt2) {
va_start(ap, fmt2);
#define METRICsva(lb, fmt1, ...) \ m->descr = g_strdup_vprintf(fmt2, ap);
do { \ va_end(ap);
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \ }
m->label = g_strdup(lb); \ if (m->label && (
m->value_short = g_strdup_printf(fmt1, ## __VA_ARGS__); \ m->label[0] == '['
SM_PUSH(ret, m); \ || m->label[0] == '{'
} while (0) || m->label[0] == '}'
|| m->label[0] == ']')
#define METRICs(lb, fmt1, arg) \ && m->label[1] == 0)
do { \ {
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \ m->is_bracket = 1;
m->label = g_strdup(lb); \ if (m->label[0] == '}' || m->label[0] == ']')
m->value_short = g_strdup_printf(fmt1, arg); \ m->is_close_bracket = 1;
if (fmt1) { \ if (m->label[0] == '{' || m->label[0] == '}')
if (!strcmp(fmt1, "%u") || \ m->is_brace = 1;
!strcmp(fmt1, "%lu") || \ }
!strcmp(fmt1, "%llu") || \ metric_push(ret, m);
!strcmp(fmt1, "%i") || \ }
!strcmp(fmt1, "%li") || \
!strcmp(fmt1, "%lli") || \ #define METRIC(lb, dsc, fmt1, fmt2, ...) add_metric(ret, lb, dsc, fmt1, fmt2, ## __VA_ARGS__)
!strcmp(fmt1, "%d") || \ #define METRICva METRIC
!strcmp(fmt1, "%ld") || \ #define METRICl(dsc, fmt2, ...) add_metric(ret, NULL, dsc, NULL, fmt2, ##__VA_ARGS__)
!strcmp(fmt1, "%lld")) \ #define METRICsva(lb, fmt1, ...) add_metric(ret, lb, NULL, fmt1, NULL, ##__VA_ARGS__)
{ \ #define METRICs(lb, fmt1, arg) add_metric(ret, lb, NULL, fmt1, NULL, arg)
m->is_int = 1; \
m->int_value = arg; \ #define HEADER(fmt1, fmt2, ...) add_header(ret, fmt1, fmt2, ##__VA_ARGS__)
} \ #define HEADERl(fmt2, ...) add_header(ret, NULL, fmt2, ##__VA_ARGS__)
} \
SM_PUSH(ret, m); \
} while (0)
#define HEADER(fmt1, fmt2, ...) \
do { \
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \
if (fmt1) \
m->label = g_strdup_printf(fmt1, ## __VA_ARGS__); \
if (fmt2) \
m->descr = g_strdup_printf(fmt2, ## __VA_ARGS__); \
if (m->label && ( \
m->label[0] == '[' \
|| m->label[0] == '{' \
|| m->label[0] == '}' \
|| m->label[0] == ']') \
&& m->label[1] == 0) \
{ \
m->is_bracket = 1; \
if (m->label[0] == '}' || m->label[0] == ']') \
m->is_close_bracket = 1; \
if (m->label[0] == '{' || m->label[0] == '}') \
m->is_brace = 1; \
} \
SM_PUSH(ret, m); \
} while (0)
#define HEADERl(fmt2, ...) \
do { \
struct stats_metric *m = g_slice_alloc0(sizeof(*m)); \
m->descr = g_strdup_printf(fmt2, ## __VA_ARGS__); \
SM_PUSH(ret, m); \
} while (0)
GQueue *statistics_gather_metrics(void) { GQueue *statistics_gather_metrics(void) {
GQueue *ret = g_queue_new(); GQueue *ret = g_queue_new();

Loading…
Cancel
Save