aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2019-05-07 20:26:05 -0700
committerGarrett D'Amore <garrett@damore.org>2019-05-07 20:26:05 -0700
commit2b87c219ca07aebcf6e8ff02009dca88babc04c3 (patch)
treec4d727615f01748280b93f3abc99ac3a6c7eb488 /src/core
parentc036b3e4a365a966215e383c1130c66d96aa917b (diff)
downloadnng-2b87c219ca07aebcf6e8ff02009dca88babc04c3.tar.gz
nng-2b87c219ca07aebcf6e8ff02009dca88babc04c3.tar.bz2
nng-2b87c219ca07aebcf6e8ff02009dca88babc04c3.zip
Introduce nni_plat_printf()
This permits the stats dump to avoid some extra buffering, and resolves a complaint about possible format buffer overruns.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/platform.h5
-rw-r--r--src/core/stats.c35
2 files changed, 20 insertions, 20 deletions
diff --git a/src/core/platform.h b/src/core/platform.h
index e415b438..1418ba4f 100644
--- a/src/core/platform.h
+++ b/src/core/platform.h
@@ -52,6 +52,11 @@ extern void nni_plat_abort(void);
// not contain newlines, but the output will add them.
extern void nni_plat_println(const char *);
+// nni_plat_printf is like printf. It should conform to C99 standard printf,
+// but is a function to allow platform ports to redirect. It should go to
+// the same place that nni_plat_println does.
+extern void nni_plat_printf(const char *, ...);
+
// nni_plat_strerror allows the platform to use additional error messages
// for additional error codes. The err code passed in should be the
// equivalent of errno or GetLastError, without the NNG_ESYSERR component.
diff --git a/src/core/stats.c b/src/core/stats.c
index afeafff6..f3f969c3 100644
--- a/src/core/stats.c
+++ b/src/core/stats.c
@@ -1,5 +1,5 @@
//
-// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
@@ -453,7 +453,6 @@ nng_stats_dump(nng_stat *stat)
{
#ifdef NNG_ENABLE_STATS
static char buf[128]; // to minimize recursion, not thread safe
- static char line[128];
int len;
char * scope;
char * indent = " ";
@@ -472,53 +471,49 @@ nng_stats_dump(nng_stat *stat)
}
}
if (len > 0) {
- snprintf(line, sizeof(line), "\n%s:", buf);
+ nni_plat_printf("\n%s:\n", buf);
}
break;
case NNG_STAT_STRING:
- snprintf(line, sizeof(line), "%s%-32s\"%s\"", indent,
- nng_stat_name(stat), nng_stat_string(stat));
+ nni_plat_printf("%s%-32s\"%s\"\n", indent, nng_stat_name(stat),
+ nng_stat_string(stat));
break;
case NNG_STAT_BOOLEAN:
val = nng_stat_value(stat);
- snprintf(line, sizeof(line), "%s%-32s%s", indent,
- nng_stat_name(stat), val != 0 ? "true" : "false");
+ nni_plat_printf("%s%-32s%s\n", indent, nng_stat_name(stat),
+ val != 0 ? "true" : "false");
break;
case NNG_STAT_LEVEL:
case NNG_STAT_COUNTER:
val = nng_stat_value(stat);
+ nni_plat_printf(
+ "%s%-32s%llu", indent, nng_stat_name(stat), val);
switch (nng_stat_unit(stat)) {
case NNG_UNIT_BYTES:
- snprintf(line, sizeof(line), "%s%-32s%llu bytes",
- indent, nng_stat_name(stat), val);
+ nni_plat_printf(" bytes\n");
break;
case NNG_UNIT_MESSAGES:
- snprintf(line, sizeof(line), "%s%-32s%llu msgs",
- indent, nng_stat_name(stat), val);
+ nni_plat_printf(" msgs\n");
break;
case NNG_UNIT_MILLIS:
- snprintf(line, sizeof(line), "%s%-32s%llu msec",
- indent, nng_stat_name(stat), val);
+ nni_plat_printf(" msec\n");
break;
case NNG_UNIT_NONE:
case NNG_UNIT_EVENTS:
default:
- snprintf(line, sizeof(line), "%s%-32s%llu", indent,
- nng_stat_name(stat), val);
+ nni_plat_printf("\n");
break;
}
break;
case NNG_STAT_ID:
val = nng_stat_value(stat);
- snprintf(line, (sizeof line), "%s%-32s%llu", indent,
- nng_stat_name(stat), val);
+ nni_plat_printf(
+ "%s%-32s%llu\n", indent, nng_stat_name(stat), val);
break;
default:
- snprintf(line, (sizeof line), "%s%-32s<?>", indent,
- nng_stat_name(stat));
+ nni_plat_printf("%s%-32s<?>\n", indent, nng_stat_name(stat));
break;
}
- nni_plat_println(line);
NNI_LIST_FOREACH (&stat->s_children, child) {
nng_stats_dump(child);