]> arthur.barton.de Git - netdata.git/blob - src/log.c
splitted netdata to multiple source files
[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         if(file) { ; }
43         va_list args;
44
45         log_date(stdout);
46         va_start( args, fmt );
47         fprintf(stdout, "DEBUG (%04lu@%-15.15s): ", line, function);
48         vfprintf( stdout, fmt, args );
49         va_end( args );
50         fprintf(stdout, "\n");
51
52         if(output_log_syslog) {
53                 va_start( args, fmt );
54                 vsyslog(LOG_ERR,  fmt, args );
55                 va_end( args );
56         }
57 }
58
59 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
60 {
61         if(file) { ; }
62         va_list args;
63
64         log_date(stderr);
65
66         va_start( args, fmt );
67         if(debug_flags) fprintf(stderr, "INFO (%04lu@%-15.15s): ", line, function);
68         else            fprintf(stderr, "INFO: ");
69         vfprintf( stderr, fmt, args );
70         va_end( args );
71
72         fprintf(stderr, "\n");
73
74         if(error_log_syslog) {
75                 va_start( args, fmt );
76                 vsyslog(LOG_INFO,  fmt, args );
77                 va_end( args );
78         }
79 }
80
81 void error_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
82 {
83         if(file) { ; }
84         va_list args;
85
86         log_date(stderr);
87
88         va_start( args, fmt );
89         if(debug_flags) fprintf(stderr, "ERROR (%04lu@%-15.15s): ", line, function);
90         else            fprintf(stderr, "ERROR: ");
91         vfprintf( stderr, fmt, args );
92         va_end( args );
93
94         if(errno) {
95                         fprintf(stderr, " (errno %d, %s)\n", errno, strerror(errno));
96                         errno = 0;
97         }
98         else fprintf(stderr, "\n");
99
100         if(error_log_syslog) {
101                 va_start( args, fmt );
102                 vsyslog(LOG_ERR,  fmt, args );
103                 va_end( args );
104         }
105 }
106
107 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
108 {
109         if(file) { ; }
110         va_list args;
111
112         log_date(stderr);
113
114         va_start( args, fmt );
115         if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-15.15s): ", line, function);
116         else            fprintf(stderr, "FATAL: ");
117         vfprintf( stderr, fmt, args );
118         va_end( args );
119
120         perror(" # ");
121         fprintf(stderr, "\n");
122
123         if(error_log_syslog) {
124                 va_start( args, fmt );
125                 vsyslog(LOG_CRIT,  fmt, args );
126                 va_end( args );
127         }
128
129         exit(1);
130 }
131
132 void log_access( const char *fmt, ... )
133 {
134         va_list args;
135
136         if(stdaccess) {
137                 log_date(stdaccess);
138
139                 va_start( args, fmt );
140                 vfprintf( stdaccess, fmt, args );
141                 va_end( args );
142                 fprintf( stdaccess, "\n");
143                 fflush( stdaccess );
144         }
145
146         if(access_log_syslog) {
147                 va_start( args, fmt );
148                 vsyslog(LOG_INFO,  fmt, args );
149                 va_end( args );
150         }
151 }
152