]> arthur.barton.de Git - netdata.git/blob - src/log.c
removed thread unsafe functions
[netdata.git] / src / log.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <time.h>
5 #include <syslog.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10
11 #include "log.h"
12 #include "common.h"
13
14
15 // ----------------------------------------------------------------------------
16 // LOG
17
18 const char *program_name = "";
19 unsigned long long debug_flags = DEBUG;
20
21 int silent = 0;
22
23 int access_fd = -1;
24 FILE *stdaccess = NULL;
25
26 int access_log_syslog = 1;
27 int error_log_syslog = 1;
28 int output_log_syslog = 1;      // debug log
29
30
31 void log_date(FILE *out)
32 {
33                 char outstr[200];
34                 time_t t;
35                 struct tm *tmp, tmbuf;
36
37                 t = time(NULL);
38                 tmp = localtime_r(&t, &tmbuf);
39
40                 if (tmp == NULL) return;
41                 if (strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0) return;
42
43                 fprintf(out, "%s: ", outstr);
44 }
45
46 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
47 {
48         va_list args;
49
50         log_date(stdout);
51         va_start( args, fmt );
52         fprintf(stdout, "DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
53         vfprintf( stdout, fmt, args );
54         va_end( args );
55         fprintf(stdout, "\n");
56
57         if(output_log_syslog) {
58                 va_start( args, fmt );
59                 vsyslog(LOG_ERR,  fmt, args );
60                 va_end( args );
61         }
62 }
63
64 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
65 {
66         va_list args;
67
68         log_date(stderr);
69
70         va_start( args, fmt );
71         if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
72         else            fprintf(stderr, "INFO: %s: ", program_name);
73         vfprintf( stderr, fmt, args );
74         va_end( args );
75
76         fprintf(stderr, "\n");
77
78         if(error_log_syslog) {
79                 va_start( args, fmt );
80                 vsyslog(LOG_INFO,  fmt, args );
81                 va_end( args );
82         }
83 }
84
85 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
86 {
87         va_list args;
88
89         log_date(stderr);
90
91         va_start( args, fmt );
92         if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
93         else            fprintf(stderr, "%s: %s: ", prefix, program_name);
94         vfprintf( stderr, fmt, args );
95         va_end( args );
96
97         if(errno) {
98                 char buf[200];
99                 char *s = strerror_r(errno, buf, 200);
100                 fprintf(stderr, " (errno %d, %s)\n", errno, s);
101                 errno = 0;
102         }
103         else fprintf(stderr, "\n");
104
105         if(error_log_syslog) {
106                 va_start( args, fmt );
107                 vsyslog(LOG_ERR,  fmt, args );
108                 va_end( args );
109         }
110 }
111
112 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
113 {
114         va_list args;
115
116         log_date(stderr);
117
118         va_start( args, fmt );
119         if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
120         else            fprintf(stderr, "FATAL: %s: ", program_name);
121         vfprintf( stderr, fmt, args );
122         va_end( args );
123
124         perror(" # ");
125         fprintf(stderr, "\n");
126
127         if(error_log_syslog) {
128                 va_start( args, fmt );
129                 vsyslog(LOG_CRIT,  fmt, args );
130                 va_end( args );
131         }
132
133         exit(1);
134 }
135
136 void log_access( const char *fmt, ... )
137 {
138         va_list args;
139
140         if(stdaccess) {
141                 log_date(stdaccess);
142
143                 va_start( args, fmt );
144                 vfprintf( stdaccess, fmt, args );
145                 va_end( args );
146                 fprintf( stdaccess, "\n");
147 #ifdef NETDATA_INTERNAL_CHECKS
148                 fflush( stdaccess );
149 #endif
150         }
151
152         if(access_log_syslog) {
153                 va_start( args, fmt );
154                 vsyslog(LOG_INFO,  fmt, args );
155                 va_end( args );
156         }
157 }
158