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);
79 *fp = fdopen(fd, "a");
81 error("Cannot fdopen() fd %d ('%s')", fd, filename);
83 if (setvbuf(*fp, NULL, _IOLBF, 0) != 0)
84 error("Cannot set line buffering on fd %d ('%s')", fd, filename);
91 void reopen_all_log_files() {
93 open_log_file(STDOUT_FILENO, &stdout, stdout_filename, &output_log_syslog);
96 open_log_file(STDERR_FILENO, &stderr, stderr_filename, &error_log_syslog);
98 if(stdaccess_filename)
99 stdaccess_fd = open_log_file(stdaccess_fd, &stdaccess, stdaccess_filename, &access_log_syslog);
102 void open_all_log_files() {
104 open_log_file(STDIN_FILENO, &stdin, "/dev/null", NULL);
106 open_log_file(STDOUT_FILENO, &stdout, stdout_filename, &output_log_syslog);
107 open_log_file(STDERR_FILENO, &stderr, stderr_filename, &error_log_syslog);
108 stdaccess_fd = open_log_file(stdaccess_fd, &stdaccess, stdaccess_filename, &access_log_syslog);
111 // ----------------------------------------------------------------------------
112 // error log throttling
114 time_t error_log_throttle_period = 1200;
115 unsigned long error_log_errors_per_period = 200;
117 int error_log_limit(int reset) {
118 static time_t start = 0;
119 static unsigned long counter = 0, prevented = 0;
121 // do not throttle if the period is 0
122 if(error_log_throttle_period == 0)
125 // prevent all logs if the errors per period is 0
126 if(error_log_errors_per_period == 0)
129 time_t now = time(NULL);
130 if(!start) start = now;
135 fprintf(stderr, "%s: Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n"
148 // detect if we log too much
151 if(now - start > error_log_throttle_period) {
154 fprintf(stderr, "%s: Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n"
158 , error_log_throttle_period
162 // restart the period accounting
171 if(counter > error_log_errors_per_period) {
174 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"
178 , error_log_errors_per_period
179 , error_log_throttle_period
181 , start + error_log_throttle_period - now);
186 // prevent logging this error
193 // ----------------------------------------------------------------------------
197 // this should print the date in a buffer the way it
198 // is now, logs from multiple threads may be multiplexed
200 void log_date(FILE *out)
204 struct tm *tmp, tmbuf;
207 tmp = localtime_r(&t, &tmbuf);
209 if (tmp == NULL) return;
210 if (unlikely(strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0)) return;
212 fprintf(out, "%s: ", outstr);
215 // ----------------------------------------------------------------------------
218 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
223 va_start( args, fmt );
224 printf("DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
229 if(output_log_syslog) {
230 va_start( args, fmt );
231 vsyslog(LOG_ERR, fmt, args );
238 // ----------------------------------------------------------------------------
241 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
245 // prevent logging too much
246 if(error_log_limit(0)) return;
250 va_start( args, fmt );
251 if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
252 else fprintf(stderr, "INFO: %s: ", program_name);
253 vfprintf( stderr, fmt, args );
258 if(error_log_syslog) {
259 va_start( args, fmt );
260 vsyslog(LOG_INFO, fmt, args );
265 // ----------------------------------------------------------------------------
268 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
272 // prevent logging too much
273 if(error_log_limit(0)) return;
277 va_start( args, fmt );
278 if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
279 else fprintf(stderr, "%s: %s: ", prefix, program_name);
280 vfprintf( stderr, fmt, args );
285 fprintf(stderr, " (errno %d, %s)\n", errno, strerror_r(errno, buf, 1023));
291 if(error_log_syslog) {
292 va_start( args, fmt );
293 vsyslog(LOG_ERR, fmt, args );
298 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
304 va_start( args, fmt );
305 if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
306 else fprintf(stderr, "FATAL: %s: ", program_name);
307 vfprintf( stderr, fmt, args );
313 if(error_log_syslog) {
314 va_start( args, fmt );
315 vsyslog(LOG_CRIT, fmt, args );
319 netdata_cleanup_and_exit(1);
322 // ----------------------------------------------------------------------------
325 void log_access( const char *fmt, ... )
332 va_start( args, fmt );
333 vfprintf( stdaccess, fmt, args );
335 fputc('\n', stdaccess);
338 if(access_log_syslog) {
339 va_start( args, fmt );
340 vsyslog(LOG_INFO, fmt, args );