]> arthur.barton.de Git - netdata.git/blob - src/log.c
fix for gcc < 4.9
[netdata.git] / src / log.c
1 #include "common.h"
2
3 const char *program_name = "";
4 unsigned long long debug_flags = DEBUG;
5
6 int access_log_syslog = 1;
7 int error_log_syslog = 1;
8 int output_log_syslog = 1;  // debug log
9
10 int stdaccess_fd = -1;
11 FILE *stdaccess = NULL;
12
13 const char *stdaccess_filename = NULL;
14 const char *stderr_filename = NULL;
15 const char *stdout_filename = NULL;
16
17 void syslog_init(void) {
18     static int i = 0;
19
20     if(!i) {
21         openlog(program_name, LOG_PID, LOG_DAEMON);
22         i = 1;
23     }
24 }
25
26 int open_log_file(int fd, FILE **fp, const char *filename, int *enabled_syslog) {
27     int f, t;
28
29     if(!filename || !*filename || !strcmp(filename, "none"))
30         filename = "/dev/null";
31
32     if(!strcmp(filename, "syslog")) {
33         filename = "/dev/null";
34         syslog_init();
35         if(enabled_syslog) *enabled_syslog = 1;
36     }
37     else if(enabled_syslog) *enabled_syslog = 0;
38
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;
43         filename = "stdout";
44     }
45
46     if(!strcmp(filename, "stdout"))
47         f = STDOUT_FILENO;
48
49     else if(!strcmp(filename, "stderr"))
50         f = STDERR_FILENO;
51
52     else {
53         f = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0664);
54         if(f == -1) {
55             error("Cannot open file '%s'. Leaving %d to its default.", filename, fd);
56             return fd;
57         }
58     }
59
60     // if there is a level-2 file pointer
61     // flush it before switching the level-1 fds
62     if(fp && *fp)
63         fflush(*fp);
64
65     if(fd != f && fd != -1) {
66         // it automatically closes
67         t = dup2(f, fd);
68         if (t == -1) {
69             error("Cannot dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
70             close(f);
71             return fd;
72         }
73         // info("dup2() new fd %d to old fd %d for '%s'", f, fd, filename);
74         close(f);
75     }
76     else fd = f;
77
78     if(fp && !*fp) {
79         *fp = fdopen(fd, "a");
80         if (!*fp)
81             error("Cannot fdopen() fd %d ('%s')", fd, filename);
82         else {
83             if (setvbuf(*fp, NULL, _IOLBF, 0) != 0)
84                 error("Cannot set line buffering on fd %d ('%s')", fd, filename);
85         }
86     }
87
88     return fd;
89 }
90
91 void reopen_all_log_files() {
92     if(stdout_filename)
93         open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog);
94
95     if(stderr_filename)
96         open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog);
97
98     if(stdaccess_filename)
99         stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog);
100 }
101
102 void open_all_log_files() {
103     // disable stdin
104     open_log_file(STDIN_FILENO, (FILE **)&stdin, "/dev/null", NULL);
105
106     open_log_file(STDOUT_FILENO, (FILE **)&stdout, stdout_filename, &output_log_syslog);
107     open_log_file(STDERR_FILENO, (FILE **)&stderr, stderr_filename, &error_log_syslog);
108     stdaccess_fd = open_log_file(stdaccess_fd, (FILE **)&stdaccess, stdaccess_filename, &access_log_syslog);
109 }
110
111 // ----------------------------------------------------------------------------
112 // error log throttling
113
114 time_t error_log_throttle_period_backup = 0;
115 time_t error_log_throttle_period = 1200;
116 unsigned long error_log_errors_per_period = 200;
117
118 int error_log_limit(int reset) {
119     static time_t start = 0;
120     static unsigned long counter = 0, prevented = 0;
121
122     // do not throttle if the period is 0
123     if(error_log_throttle_period == 0)
124         return 0;
125
126     // prevent all logs if the errors per period is 0
127     if(error_log_errors_per_period == 0)
128         return 1;
129
130     time_t now = time(NULL);
131     if(!start) start = now;
132
133     if(reset) {
134         if(prevented) {
135             log_date(stderr);
136             fprintf(stderr, "%s: Resetting logging for process '%s' (prevented %lu logs in the last %ld seconds).\n"
137                     , program_name
138                     , program_name
139                     , prevented
140                     , now - start
141             );
142         }
143
144         start = now;
145         counter = 0;
146         prevented = 0;
147     }
148
149     // detect if we log too much
150     counter++;
151
152     if(now - start > error_log_throttle_period) {
153         if(prevented) {
154             log_date(stderr);
155             fprintf(stderr, "%s: Resuming logging from process '%s' (prevented %lu logs in the last %ld seconds).\n"
156                     , program_name
157                     , program_name
158                     , prevented
159                     , error_log_throttle_period
160             );
161         }
162
163         // restart the period accounting
164         start = now;
165         counter = 1;
166         prevented = 0;
167
168         // log this error
169         return 0;
170     }
171
172     if(counter > error_log_errors_per_period) {
173         if(!prevented) {
174             log_date(stderr);
175             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"
176                     , program_name
177                     , counter
178                     , now - start
179                     , error_log_errors_per_period
180                     , error_log_throttle_period
181                     , program_name
182                     , start + error_log_throttle_period - now);
183         }
184
185         prevented++;
186
187         // prevent logging this error
188         return 1;
189     }
190
191     return 0;
192 }
193
194 // ----------------------------------------------------------------------------
195 // print the date
196
197 // FIXME
198 // this should print the date in a buffer the way it
199 // is now, logs from multiple threads may be multiplexed
200
201 void log_date(FILE *out)
202 {
203         char outstr[24];
204         time_t t;
205         struct tm *tmp, tmbuf;
206
207         t = time(NULL);
208         tmp = localtime_r(&t, &tmbuf);
209
210         if (tmp == NULL) return;
211         if (unlikely(strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0)) return;
212
213         fprintf(out, "%s: ", outstr);
214 }
215
216 // ----------------------------------------------------------------------------
217 // debug log
218
219 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
220 {
221     va_list args;
222
223     log_date(stdout);
224     va_start( args, fmt );
225     printf("DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
226     vprintf(fmt, args);
227     va_end( args );
228     putchar('\n');
229
230     if(output_log_syslog) {
231         va_start( args, fmt );
232         vsyslog(LOG_ERR,  fmt, args );
233         va_end( args );
234     }
235
236     fflush(stdout);
237 }
238
239 // ----------------------------------------------------------------------------
240 // info log
241
242 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
243 {
244     va_list args;
245
246     // prevent logging too much
247     if(error_log_limit(0)) return;
248
249     log_date(stderr);
250
251     va_start( args, fmt );
252     if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
253     else            fprintf(stderr, "INFO: %s: ", program_name);
254     vfprintf( stderr, fmt, args );
255     va_end( args );
256
257     fputc('\n', stderr);
258
259     if(error_log_syslog) {
260         va_start( args, fmt );
261         vsyslog(LOG_INFO,  fmt, args );
262         va_end( args );
263     }
264 }
265
266 // ----------------------------------------------------------------------------
267 // error log
268
269 #ifdef WITHOUT_C11_GENERIC
270
271 #ifdef STRERROR_R_POSIX
272 // POSIX version of strerror_r
273 static const char *strerror_result(int a, const char *b) { (void)a; return b; }
274 #else
275 // GLIBC version of strerror_r
276 static const char *strerror_result(const char *a, const char *b) { (void)b; return a; }
277 #endif
278
279 #else /* ! WITHOUT_C11_GENERIC */
280
281 // what a trick!
282 // http://stackoverflow.com/questions/479207/function-overloading-in-c
283 static const char *strerror_result_int(int a, const char *b) { (void)a; return b; }
284 static const char *strerror_result_string(const char *a, const char *b) { (void)b; return a; }
285
286 #define strerror_result(a, b) _Generic((a), \
287     int: strerror_result_int, \
288     char *: strerror_result_string \
289     )(a, b)
290
291 #endif /* ! WITHOUT_C11_GENERIC */
292
293 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
294 {
295     va_list args;
296
297     // prevent logging too much
298     if(error_log_limit(0)) return;
299
300     log_date(stderr);
301
302     va_start( args, fmt );
303     if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
304     else            fprintf(stderr, "%s: %s: ", prefix, program_name);
305     vfprintf( stderr, fmt, args );
306     va_end( args );
307
308     if(errno) {
309         char buf[1024];
310         fprintf(stderr, " (errno %d, %s)\n", errno, strerror_result(strerror_r(errno, buf, 1023), buf));
311         errno = 0;
312     }
313     else
314         fputc('\n', stderr);
315
316     if(error_log_syslog) {
317         va_start( args, fmt );
318         vsyslog(LOG_ERR,  fmt, args );
319         va_end( args );
320     }
321 }
322
323 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
324 {
325     va_list args;
326
327     log_date(stderr);
328
329     va_start( args, fmt );
330     if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
331     else            fprintf(stderr, "FATAL: %s: ", program_name);
332     vfprintf( stderr, fmt, args );
333     va_end( args );
334
335     perror(" # ");
336     fputc('\n', stderr);
337
338     if(error_log_syslog) {
339         va_start( args, fmt );
340         vsyslog(LOG_CRIT,  fmt, args );
341         va_end( args );
342     }
343
344     netdata_cleanup_and_exit(1);
345 }
346
347 // ----------------------------------------------------------------------------
348 // access log
349
350 void log_access( const char *fmt, ... )
351 {
352     va_list args;
353
354     if(stdaccess) {
355         log_date(stdaccess);
356
357         va_start( args, fmt );
358         vfprintf( stdaccess, fmt, args );
359         va_end( args );
360         fputc('\n', stdaccess);
361     }
362
363     if(access_log_syslog) {
364         va_start( args, fmt );
365         vsyslog(LOG_INFO,  fmt, args );
366         va_end( args );
367     }
368 }
369