summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/core/platform.h5
-rw-r--r--src/core/stats.c35
-rw-r--r--src/platform/posix/posix_debug.c12
-rw-r--r--src/platform/windows/win_debug.c11
-rw-r--r--src/supplemental/websocket/websocket.c1
5 files changed, 42 insertions, 22 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);
diff --git a/src/platform/posix/posix_debug.c b/src/platform/posix/posix_debug.c
index 7619e8e8..80f37225 100644
--- a/src/platform/posix/posix_debug.c
+++ b/src/platform/posix/posix_debug.c
@@ -1,5 +1,5 @@
//
-// Copyright 2017 Garrett D'Amore <garrett@damore.org>
+// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -12,6 +12,7 @@
#ifdef NNG_PLATFORM_POSIX
#include <errno.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -23,6 +24,15 @@ nni_plat_abort(void)
}
void
+nni_plat_printf(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, ap);
+ (void) vprintf(fmt, ap);
+ va_end(ap);
+}
+
+void
nni_plat_println(const char *message)
{
fputs(message, stderr);
diff --git a/src/platform/windows/win_debug.c b/src/platform/windows/win_debug.c
index a47e7b41..a37411cf 100644
--- a/src/platform/windows/win_debug.c
+++ b/src/platform/windows/win_debug.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
@@ -24,6 +24,15 @@ nni_plat_abort(void)
}
void
+nni_plat_printf(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ (void) vprintf(fmt, ap);
+ va_end(ap);
+}
+
+void
nni_plat_println(const char *message)
{
(void) fprintf(stderr, "%s\n", message);
diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c
index fcd2eeb3..91affc62 100644
--- a/src/supplemental/websocket/websocket.c
+++ b/src/supplemental/websocket/websocket.c
@@ -1443,6 +1443,7 @@ ws_listener_free(void *arg)
if (l->handler != NULL) {
nni_http_handler_fini(l->handler);
+ l->handler = NULL;
}
if (l->server != NULL) {
nni_http_server_fini(l->server);