3 // ----------------------------------------------------------------------------
6 const char *program_name = "";
7 unsigned long long debug_flags = DEBUG;
12 FILE *stdaccess = NULL;
14 int access_log_syslog = 1;
15 int error_log_syslog = 1;
16 int output_log_syslog = 1; // debug log
18 time_t error_log_throttle_period = 1200;
19 unsigned long error_log_errors_per_period = 200;
21 int error_log_limit(int reset) {
22 static time_t start = 0;
23 static unsigned long counter = 0, prevented = 0;
25 // do not throttle if the period is 0
26 if(error_log_throttle_period == 0)
29 // prevent all logs if the errors per period is 0
30 if(error_log_errors_per_period == 0)
33 time_t now = time(NULL);
34 if(!start) start = now;
39 fprintf(stderr, "%s: Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n"
52 // detect if we log too much
55 if(now - start > error_log_throttle_period) {
58 fprintf(stderr, "%s: Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n"
62 , error_log_throttle_period
66 // restart the period accounting
75 if(counter > error_log_errors_per_period) {
78 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"
82 , error_log_errors_per_period
83 , error_log_throttle_period
85 , start + error_log_throttle_period - now);
90 // prevent logging this error
97 void log_date(FILE *out)
101 struct tm *tmp, tmbuf;
104 tmp = localtime_r(&t, &tmbuf);
106 if (tmp == NULL) return;
107 if (unlikely(strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0)) return;
109 fprintf(out, "%s: ", outstr);
112 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
117 va_start( args, fmt );
118 printf("DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
123 if(output_log_syslog) {
124 va_start( args, fmt );
125 vsyslog(LOG_ERR, fmt, args );
132 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
136 // prevent logging too much
137 if(error_log_limit(0)) return;
141 va_start( args, fmt );
142 if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
143 else fprintf(stderr, "INFO: %s: ", program_name);
144 vfprintf( stderr, fmt, args );
149 if(error_log_syslog) {
150 va_start( args, fmt );
151 vsyslog(LOG_INFO, fmt, args );
156 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
160 // prevent logging too much
161 if(error_log_limit(0)) return;
165 va_start( args, fmt );
166 if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
167 else fprintf(stderr, "%s: %s: ", prefix, program_name);
168 vfprintf( stderr, fmt, args );
173 fprintf(stderr, " (errno %d, %s)\n", errno, strerror_r(errno, buf, 1023));
179 if(error_log_syslog) {
180 va_start( args, fmt );
181 vsyslog(LOG_ERR, fmt, args );
186 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
192 va_start( args, fmt );
193 if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
194 else fprintf(stderr, "FATAL: %s: ", program_name);
195 vfprintf( stderr, fmt, args );
201 if(error_log_syslog) {
202 va_start( args, fmt );
203 vsyslog(LOG_CRIT, fmt, args );
207 netdata_cleanup_and_exit(1);
210 void log_access( const char *fmt, ... )
217 va_start( args, fmt );
218 vfprintf( stdaccess, fmt, args );
220 fputc('\n', stdaccess);
223 if(access_log_syslog) {
224 va_start( args, fmt );
225 vsyslog(LOG_INFO, fmt, args );