nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 1edf1d1f7f86301f5c37857cd2c521e0e1522e6e
parent a89b692e370e65b0fb087a2f843faf9ce1d5a6ae
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat,  2 Nov 2019 11:58:25 -0400

nserver: stats: Update Stats_dump.

* nserver/src/stats.c (Stats_dump): Update function.
* nserver/src/stats.h (Stats_dump): Return (char *).

Diffstat:
nserver/src/stats.c | 25+++++++++++++++++++------
nserver/src/stats.h | 2+-
2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/nserver/src/stats.c b/nserver/src/stats.c @@ -44,11 +44,24 @@ void Stats_sample(Stats *st, double s) st->n += 1; } -void Stats_dump(Stats *st) +char *Stats_dump(Stats *st) { - fprintf(stderr, - "sum: %f, sumsq: %f, n: %ld, " - "min: %f, max: %f, mean: %f, stddev: %f", - st->sum, st->sumsq, st->n, st->min, st->max, Stats_mean(st), - Stats_stddev(st)); + size_t char_sz = sizeof(char); + size_t dstr_len = 280 * char_sz; + + // allocate space for dump string. + char *dstr = calloc(dstr_len, char_sz); + check_mem(dstr); + + // dump into dump str. + int rc = snprintf(dstr, dstr_len, + "sum: %f, sumsq: %f, n: %ld, " + "min: %f, max: %f, mean: %f, stddev: %f", + st->sum, st->sumsq, st->n, st->min, st->max, + Stats_mean(st), Stats_stddev(st)); + check(rc > 0, "stats dump failed"); + + return dstr; + error: + return NULL; } diff --git a/nserver/src/stats.h b/nserver/src/stats.h @@ -17,7 +17,7 @@ Stats *Stats_create(); void Stats_sample(Stats *st, double s); -void Stats_dump(Stats *st); +char *Stats_dump(Stats *st); static inline double Stats_mean(Stats *st) {