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