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