Skip to content

Commit 24dc347

Browse files
committed
silence repeated log messages
1 parent 8d3c3c8 commit 24dc347

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

includes/ziti/ziti_log.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ ZITI_FUNC extern void ziti_log_set_logger(log_writer logger);
101101
// use ZITI_LOG_DEFAULT_LEVEL to reset to default(INFO) or ZITI_LOG env var
102102
ZITI_FUNC extern void ziti_log_set_level(int level, const char *marker);
103103

104+
// set limit for repeated log messages. set to negative value to log all messages.
105+
// default is -1.
106+
ZITI_FUNC extern void ziti_log_set_max_repeat(int32_t max);
107+
104108
// don't use directly
105109
ZITI_FUNC extern int ziti_log_level(const char *module, const char *file);
106110

library/utils.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static const char *TLSUV_MODULE = "tlsuv";
115115

116116
static model_map log_levels;
117117
static int ziti_log_lvl = ZITI_LOG_DEFAULT_LEVEL;
118+
static int ziti_log_max_repeat = -1;
118119
static FILE *ziti_debug_out;
119120
static bool log_initialized = false;
120121
static uv_pid_t log_pid = 0;
@@ -242,6 +243,10 @@ void ziti_log_set_logger(log_writer log) {
242243
logger = log;
243244
}
244245

246+
void ziti_log_set_max_repeat(int32_t max) {
247+
ziti_log_max_repeat = max;
248+
}
249+
245250
static void init_uv_mbed_log() {
246251
char *lvl;
247252
if ((lvl = getenv("TLSUV_DEBUG")) != NULL) {
@@ -320,6 +325,12 @@ static const char *basename(const char *path) {
320325
return path;
321326
}
322327

328+
struct counted_mesg {
329+
uint16_t repeat;
330+
char *mesg;
331+
char *prev_mesg;
332+
};
333+
323334
void ziti_logger(int level, const char *module, const char *file, unsigned int line, const char *func, FORMAT_STRING(const char *fmt), ...) {
324335
#ifdef ZITI_DEBUG
325336
static size_t loglinelen = 32768;
@@ -330,9 +341,11 @@ void ziti_logger(int level, const char *module, const char *file, unsigned int l
330341
log_writer logfunc = logger;
331342
if (logfunc == NULL) { return; }
332343

333-
char *logbuf = (char *) uv_key_get(&logbufs);
344+
struct counted_mesg *logbuf = uv_key_get(&logbufs);
334345
if (!logbuf) {
335-
logbuf = malloc(loglinelen);
346+
logbuf = calloc(1, sizeof(struct counted_mesg));
347+
logbuf->mesg = malloc(loglinelen);
348+
logbuf->prev_mesg = calloc(loglinelen, sizeof(char));
336349
uv_key_set(&logbufs, logbuf);
337350
}
338351

@@ -370,14 +383,30 @@ void ziti_logger(int level, const char *module, const char *file, unsigned int l
370383

371384
va_list argp;
372385
va_start(argp, fmt);
373-
int len = vsnprintf(logbuf, loglinelen, fmt, argp);
386+
int len = vsnprintf(logbuf->mesg, loglinelen, fmt, argp);
374387
va_end(argp);
375388

376389
if (len > loglinelen) {
377390
len = (int) loglinelen;
378391
}
379392

380-
logfunc(level, location, logbuf, len);
393+
if (ziti_log_max_repeat > 0) {
394+
if (strcmp(logbuf->mesg, logbuf->prev_mesg) == 0) {
395+
logbuf->repeat++;
396+
if (logbuf->repeat >= ziti_log_max_repeat) return;
397+
} else {
398+
if (logbuf->repeat > ziti_log_max_repeat) {
399+
// previous message had been silenced
400+
int l = snprintf(logbuf->prev_mesg, loglinelen, "previous message repeated %u times",
401+
logbuf->repeat - ziti_log_max_repeat + 1);
402+
logfunc(level, "\b", logbuf->prev_mesg, l);
403+
}
404+
logbuf->repeat = 0;
405+
}
406+
strcpy(logbuf->prev_mesg, logbuf->mesg);
407+
}
408+
409+
logfunc(level, location, logbuf->mesg, len);
381410
}
382411

383412
static void default_log_writer(int level, const char *loc, const char *msg, size_t msglen) {

tests/util_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,22 @@ TEST_CASE("check hostname/domainname") {
9292

9393
printf("hostname = %s\n", info->hostname);
9494
printf("domain = %s\n", info->domain);
95+
}
96+
97+
static uint32_t mesgs_logged = 0;
98+
static void test_log_writer(int level, const char *loc, const char *msg, size_t msglen) {
99+
mesgs_logged++;
100+
}
101+
102+
TEST_CASE("check repeated logs are silenced") {
103+
ziti_log_init(uv_default_loop(), INFO, test_log_writer);
104+
ziti_log_set_max_repeat(5);
105+
mesgs_logged = 0;
106+
for (long i = 0; i < 10; i++) {
107+
ZITI_LOG(INFO, "test message text");
108+
}
109+
REQUIRE(mesgs_logged == 5);
110+
mesgs_logged = 0;
111+
ZITI_LOG(INFO, "something else now");
112+
REQUIRE(mesgs_logged == 2); // "message repeated" message, and "something else now"
95113
}

0 commit comments

Comments
 (0)