Skip to content

Commit

Permalink
change metric_print to accept customized format
Browse files Browse the repository at this point in the history
  • Loading branch information
Yao Yue committed Feb 22, 2016
1 parent 1139bb3 commit 0e12282
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/cc_metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ struct metric {
};

void metric_reset(struct metric sarr[], unsigned int nmetric);
size_t metric_print(char *buf, size_t nbuf, struct metric *m);
size_t metric_print(char *buf, size_t nbuf, char *fmt, struct metric *m);

#ifdef __cplusplus
}
Expand Down
15 changes: 10 additions & 5 deletions src/stats/cc_metric.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <stdbool.h>

#define VALUE_PRINT_LEN 30

char *metric_type_str[] = {"counter", "gauge", "floating point"};

void
Expand Down Expand Up @@ -55,8 +57,10 @@ metric_reset(struct metric sarr[], unsigned int n)
}

size_t
metric_print(char *buf, size_t nbuf, struct metric *m)
metric_print(char *buf, size_t nbuf, char *fmt, struct metric *m)
{
char val_buf[VALUE_PRINT_LEN];

if (m == NULL) {
return 0;
}
Expand All @@ -68,18 +72,19 @@ metric_print(char *buf, size_t nbuf, struct metric *m)
* and negatively impact readability, and since this function should not
* be called often enough to make it absolutely performance critical.
*/
return cc_scnprintf(buf, nbuf, "%s %llu", m->name, __atomic_load_n(
cc_scnprintf(val_buf, VALUE_PRINT_LEN, "%llu", __atomic_load_n(
&m->counter, __ATOMIC_RELAXED));

case METRIC_GAUGE:
return cc_scnprintf(buf, nbuf, "%s %lld", m->name, __atomic_load_n(
cc_scnprintf(val_buf, VALUE_PRINT_LEN, "%lld", __atomic_load_n(
&m->gauge, __ATOMIC_RELAXED));

case METRIC_FPN:
return cc_scnprintf(buf, nbuf, "%s %f", m->name, m->fpn);
cc_scnprintf(val_buf, VALUE_PRINT_LEN, "%f", m->fpn);

default:
NOT_REACHED();
return 0;
}

return cc_scnprintf(buf, nbuf, fmt, m->name, val_buf);
}

0 comments on commit 0e12282

Please sign in to comment.