15 // ----------------------------------------------------------------------------
18 const char *program_name = "";
19 unsigned long long debug_flags = DEBUG;
24 FILE *stdaccess = NULL;
26 int access_log_syslog = 1;
27 int error_log_syslog = 1;
28 int output_log_syslog = 1; // debug log
30 time_t error_log_throttle_period = 1200;
31 unsigned long error_log_errors_per_period = 200;
33 int error_log_limit(int reset) {
34 static time_t start = 0;
35 static unsigned long counter = 0, prevented = 0;
37 // do not throttle if the period is 0
38 if(error_log_throttle_period == 0)
41 // prevent all logs if the errors per period is 0
42 if(error_log_errors_per_period == 0)
45 time_t now = time(NULL);
46 if(!start) start = now;
51 fprintf(stderr, "%s: Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n"
64 // detect if we log too much
67 if(now - start > error_log_throttle_period) {
70 fprintf(stderr, "%s: Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n"
74 , error_log_throttle_period
78 // restart the period accounting
87 if(counter > error_log_errors_per_period) {
90 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"
94 , error_log_errors_per_period
95 , error_log_throttle_period
97 , start + error_log_throttle_period - now);
102 // prevent logging this error
109 void log_date(FILE *out)
113 struct tm *tmp, tmbuf;
116 tmp = localtime_r(&t, &tmbuf);
118 if (tmp == NULL) return;
119 if (strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0) return;
121 fprintf(out, "%s: ", outstr);
124 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
129 va_start( args, fmt );
130 fprintf(stdout, "DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
131 vfprintf( stdout, fmt, args );
133 fprintf(stdout, "\n");
135 if(output_log_syslog) {
136 va_start( args, fmt );
137 vsyslog(LOG_ERR, fmt, args );
142 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
146 // prevent logging too much
147 if(error_log_limit(0)) return;
151 va_start( args, fmt );
152 if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
153 else fprintf(stderr, "INFO: %s: ", program_name);
154 vfprintf( stderr, fmt, args );
157 fprintf(stderr, "\n");
159 if(error_log_syslog) {
160 va_start( args, fmt );
161 vsyslog(LOG_INFO, fmt, args );
166 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
170 // prevent logging too much
171 if(error_log_limit(0)) return;
175 va_start( args, fmt );
176 if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
177 else fprintf(stderr, "%s: %s: ", prefix, program_name);
178 vfprintf( stderr, fmt, args );
183 char *s = strerror_r(errno, buf, 200);
184 fprintf(stderr, " (errno %d, %s)\n", errno, s);
187 else fprintf(stderr, "\n");
189 if(error_log_syslog) {
190 va_start( args, fmt );
191 vsyslog(LOG_ERR, fmt, args );
196 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
202 va_start( args, fmt );
203 if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
204 else fprintf(stderr, "FATAL: %s: ", program_name);
205 vfprintf( stderr, fmt, args );
209 fprintf(stderr, "\n");
211 if(error_log_syslog) {
212 va_start( args, fmt );
213 vsyslog(LOG_CRIT, fmt, args );
220 void log_access( const char *fmt, ... )
227 va_start( args, fmt );
228 vfprintf( stdaccess, fmt, args );
230 fprintf( stdaccess, "\n");
234 if(access_log_syslog) {
235 va_start( args, fmt );
236 vsyslog(LOG_INFO, fmt, args );