@@ -115,6 +115,7 @@ static const char *TLSUV_MODULE = "tlsuv";
115
115
116
116
static model_map log_levels ;
117
117
static int ziti_log_lvl = ZITI_LOG_DEFAULT_LEVEL ;
118
+ static int ziti_log_max_repeat = -1 ;
118
119
static FILE * ziti_debug_out ;
119
120
static bool log_initialized = false;
120
121
static uv_pid_t log_pid = 0 ;
@@ -242,6 +243,10 @@ void ziti_log_set_logger(log_writer log) {
242
243
logger = log ;
243
244
}
244
245
246
+ void ziti_log_set_max_repeat (int32_t max ) {
247
+ ziti_log_max_repeat = max ;
248
+ }
249
+
245
250
static void init_uv_mbed_log () {
246
251
char * lvl ;
247
252
if ((lvl = getenv ("TLSUV_DEBUG" )) != NULL ) {
@@ -320,6 +325,12 @@ static const char *basename(const char *path) {
320
325
return path ;
321
326
}
322
327
328
+ struct counted_mesg {
329
+ uint16_t repeat ;
330
+ char * mesg ;
331
+ char * prev_mesg ;
332
+ };
333
+
323
334
void ziti_logger (int level , const char * module , const char * file , unsigned int line , const char * func , FORMAT_STRING (const char * fmt ), ...) {
324
335
#ifdef ZITI_DEBUG
325
336
static size_t loglinelen = 32768 ;
@@ -330,9 +341,11 @@ void ziti_logger(int level, const char *module, const char *file, unsigned int l
330
341
log_writer logfunc = logger ;
331
342
if (logfunc == NULL ) { return ; }
332
343
333
- char * logbuf = ( char * ) uv_key_get (& logbufs );
344
+ struct counted_mesg * logbuf = uv_key_get (& logbufs );
334
345
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 ));
336
349
uv_key_set (& logbufs , logbuf );
337
350
}
338
351
@@ -370,14 +383,30 @@ void ziti_logger(int level, const char *module, const char *file, unsigned int l
370
383
371
384
va_list argp ;
372
385
va_start (argp , fmt );
373
- int len = vsnprintf (logbuf , loglinelen , fmt , argp );
386
+ int len = vsnprintf (logbuf -> mesg , loglinelen , fmt , argp );
374
387
va_end (argp );
375
388
376
389
if (len > loglinelen ) {
377
390
len = (int ) loglinelen ;
378
391
}
379
392
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 );
381
410
}
382
411
383
412
static void default_log_writer (int level , const char * loc , const char * msg , size_t msglen ) {
0 commit comments