diff options
Diffstat (limited to 'src/log.c')
-rw-r--r--[-rwxr-xr-x] | src/log.c | 87 |
1 files changed, 85 insertions, 2 deletions
diff --git a/src/log.c b/src/log.c index 7ab3f1a51..e5e0ad2c4 100755..100644 --- a/src/log.c +++ b/src/log.c @@ -27,6 +27,85 @@ int access_log_syslog = 1; int error_log_syslog = 1; int output_log_syslog = 1; // debug log +time_t error_log_throttle_period = 1200; +unsigned long error_log_errors_per_period = 200; + +int error_log_limit(int reset) { + static time_t start = 0; + static unsigned long counter = 0, prevented = 0; + + // do not throttle if the period is 0 + if(error_log_throttle_period == 0) + return 0; + + // prevent all logs if the errors per period is 0 + if(error_log_errors_per_period == 0) + return 1; + + time_t now = time(NULL); + if(!start) start = now; + + if(reset) { + if(prevented) { + log_date(stderr); + fprintf(stderr, "%s: Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n" + , program_name + , program_name + , prevented + , now - start + ); + } + + start = now; + counter = 0; + prevented = 0; + } + + // detect if we log too much + counter++; + + if(now - start > error_log_throttle_period) { + if(prevented) { + log_date(stderr); + fprintf(stderr, "%s: Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n" + , program_name + , program_name + , prevented + , error_log_throttle_period + ); + } + + // restart the period accounting + start = now; + counter = 1; + prevented = 0; + + // log this error + return 0; + } + + if(counter > error_log_errors_per_period) { + if(!prevented) { + log_date(stderr); + fprintf(stderr, "%s: Too many logs (%lu logs in %ld seconds, threshold is set to %lu logs in %ld seconds). Preventing more logs from process '%s' for %ld seconds.\n" + , program_name + , counter + , now - start + , error_log_errors_per_period + , error_log_throttle_period + , program_name + , start + error_log_throttle_period - now); + } + + prevented++; + + // prevent logging this error + return 1; + } + + return 0; +} + void log_date(FILE *out) { char outstr[200]; @@ -64,6 +143,9 @@ void info_int( const char *file, const char *function, const unsigned long line, { va_list args; + // prevent logging too much + if(error_log_limit(0)) return; + log_date(stderr); va_start( args, fmt ); @@ -85,6 +167,9 @@ void error_int( const char *prefix, const char *file, const char *function, cons { va_list args; + // prevent logging too much + if(error_log_limit(0)) return; + log_date(stderr); va_start( args, fmt ); @@ -143,9 +228,7 @@ void log_access( const char *fmt, ... ) vfprintf( stdaccess, fmt, args ); va_end( args ); fprintf( stdaccess, "\n"); -#ifdef NETDATA_INTERNAL_CHECKS fflush( stdaccess ); -#endif } if(access_log_syslog) { |