3 const char *program_name = "";
4 unsigned long long debug_flags = DEBUG;
6 int access_log_syslog = 1;
7 int error_log_syslog = 1;
8 int output_log_syslog = 1; // debug log
10 int stdaccess_fd = -1;
11 FILE *stdaccess = NULL;
13 const char *stdaccess_filename = NULL;
14 const char *stderr_filename = NULL;
15 const char *stdout_filename = NULL;
17 void syslog_init(void) {
21 openlog(program_name, LOG_PID, LOG_DAEMON);
26 int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) {
29 if(!filename || !*filename || !strcmp(filename, "none"))
30 filename = "/dev/null";
32 if(!strcmp(filename, "syslog")) {
33 filename = "/dev/null";
35 if(enabled_syslog) *enabled_syslog = 1;
37 else if(enabled_syslog) *enabled_syslog = 0;
39 // don't do anything if the user is willing
40 // to have the standard one
41 if(!strcmp(filename, "system")) {
42 if(fd != -1) return fd;
46 if(!strcmp(filename, "stdout"))
49 else if(!strcmp(filename, "stderr"))
53 f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
55 error("Cannot open file '%s'. Leaving %d to its default.", filename, fd);
60 // if there is a level-2 file pointer
61 // flush it before switching the level-1 fds
65 if(fd != f && fd != -1) {
66 // it automatically closes
69 error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
73 // info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
78 if(fp && *fp == NULL) {
79 // info("fdopen(%d) on filename '%s'", fd, filename);
81 FILE *n = fdopen(fd, "a");
83 error("Cannot fdopen() fd %d ('%s')", fd, filename);
86 if (setvbuf(n, NULL, _IOLBF, 0) != 0)
87 error("Cannot set line buffering on fd %d ('%s')", fd, filename);
102 void reopen_all_log_files() {
104 open_log_file(STDOUT_FILENO, &stdout, stdout_filename, &output_log_syslog);
107 open_log_file(STDERR_FILENO, &stderr, stderr_filename, &error_log_syslog);
109 if(stdaccess_filename)
110 stdaccess_fd = open_log_file(stdaccess_fd, &stdaccess, stdaccess_filename, &access_log_syslog);
113 void open_all_log_files() {
115 open_log_file(STDIN_FILENO, &stdin, "/dev/null", NULL);
117 open_log_file(STDOUT_FILENO, &stdout, stdout_filename, &output_log_syslog);
118 open_log_file(STDERR_FILENO, &stderr, stderr_filename, &error_log_syslog);
119 stdaccess_fd = open_log_file(stdaccess_fd, &stdaccess, stdaccess_filename, &access_log_syslog);
122 // ----------------------------------------------------------------------------
123 // error log throttling
125 time_t error_log_throttle_period = 1200;
126 unsigned long error_log_errors_per_period = 200;
128 int error_log_limit(int reset) {
129 static time_t start = 0;
130 static unsigned long counter = 0, prevented = 0;
132 // do not throttle if the period is 0
133 if(error_log_throttle_period == 0)
136 // prevent all logs if the errors per period is 0
137 if(error_log_errors_per_period == 0)
140 time_t now = time(NULL);
141 if(!start) start = now;
146 fprintf(stderr, "%s: Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n"
159 // detect if we log too much
162 if(now - start > error_log_throttle_period) {
165 fprintf(stderr, "%s: Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n"
169 , error_log_throttle_period
173 // restart the period accounting
182 if(counter > error_log_errors_per_period) {
185 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"
189 , error_log_errors_per_period
190 , error_log_throttle_period
192 , start + error_log_throttle_period - now);
197 // prevent logging this error
204 // ----------------------------------------------------------------------------
208 // this should print the date in a buffer the way it
209 // is now, logs from multiple threads may be multiplexed
211 void log_date(FILE *out)
215 struct tm *tmp, tmbuf;
218 tmp = localtime_r(&t, &tmbuf);
220 if (tmp == NULL) return;
221 if (unlikely(strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0)) return;
223 fprintf(out, "%s: ", outstr);
226 // ----------------------------------------------------------------------------
229 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
234 va_start( args, fmt );
235 printf("DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
240 if(output_log_syslog) {
241 va_start( args, fmt );
242 vsyslog(LOG_ERR, fmt, args );
249 // ----------------------------------------------------------------------------
252 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
256 // prevent logging too much
257 if(error_log_limit(0)) return;
261 va_start( args, fmt );
262 if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
263 else fprintf(stderr, "INFO: %s: ", program_name);
264 vfprintf( stderr, fmt, args );
269 if(error_log_syslog) {
270 va_start( args, fmt );
271 vsyslog(LOG_INFO, fmt, args );
276 // ----------------------------------------------------------------------------
279 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
283 // prevent logging too much
284 if(error_log_limit(0)) return;
288 va_start( args, fmt );
289 if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
290 else fprintf(stderr, "%s: %s: ", prefix, program_name);
291 vfprintf( stderr, fmt, args );
296 fprintf(stderr, " (errno %d, %s)\n", errno, strerror_r(errno, buf, 1023));
302 if(error_log_syslog) {
303 va_start( args, fmt );
304 vsyslog(LOG_ERR, fmt, args );
309 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
315 va_start( args, fmt );
316 if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
317 else fprintf(stderr, "FATAL: %s: ", program_name);
318 vfprintf( stderr, fmt, args );
324 if(error_log_syslog) {
325 va_start( args, fmt );
326 vsyslog(LOG_CRIT, fmt, args );
330 netdata_cleanup_and_exit(1);
333 // ----------------------------------------------------------------------------
336 void log_access( const char *fmt, ... )
343 va_start( args, fmt );
344 vfprintf( stdaccess, fmt, args );
346 fputc('\n', stdaccess);
349 if(access_log_syslog) {
350 va_start( args, fmt );
351 vsyslog(LOG_INFO, fmt, args );