]> arthur.barton.de Git - netdata.git/blob - src/log.c
apps.plugin: major new features and optimizations: now it reports system usage (all...
[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 void log_date(FILE *out)
31 {
32                 char outstr[200];
33                 time_t t;
34                 struct tm *tmp, tmbuf;
35
36                 t = time(NULL);
37                 tmp = localtime_r(&t, &tmbuf);
38
39                 if (tmp == NULL) return;
40                 if (strftime(outstr, sizeof(outstr), "%y-%m-%d %H:%M:%S", tmp) == 0) return;
41
42                 fprintf(out, "%s: ", outstr);
43 }
44
45 void debug_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
46 {
47         va_list args;
48
49         log_date(stdout);
50         va_start( args, fmt );
51         fprintf(stdout, "DEBUG (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
52         vfprintf( stdout, fmt, args );
53         va_end( args );
54         fprintf(stdout, "\n");
55
56         if(output_log_syslog) {
57                 va_start( args, fmt );
58                 vsyslog(LOG_ERR,  fmt, args );
59                 va_end( args );
60         }
61 }
62
63 void info_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
64 {
65         va_list args;
66
67         log_date(stderr);
68
69         va_start( args, fmt );
70         if(debug_flags) fprintf(stderr, "INFO (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
71         else            fprintf(stderr, "INFO: %s: ", program_name);
72         vfprintf( stderr, fmt, args );
73         va_end( args );
74
75         fprintf(stderr, "\n");
76
77         if(error_log_syslog) {
78                 va_start( args, fmt );
79                 vsyslog(LOG_INFO,  fmt, args );
80                 va_end( args );
81         }
82 }
83
84 void error_int( const char *prefix, const char *file, const char *function, const unsigned long line, const char *fmt, ... )
85 {
86         va_list args;
87
88         log_date(stderr);
89
90         va_start( args, fmt );
91         if(debug_flags) fprintf(stderr, "%s (%04lu@%-10.10s:%-15.15s): %s: ", prefix, line, file, function, program_name);
92         else            fprintf(stderr, "%s: %s: ", prefix, program_name);
93         vfprintf( stderr, fmt, args );
94         va_end( args );
95
96         if(errno) {
97                 char buf[200];
98                 char *s = strerror_r(errno, buf, 200);
99                 fprintf(stderr, " (errno %d, %s)\n", errno, s);
100                 errno = 0;
101         }
102         else fprintf(stderr, "\n");
103
104         if(error_log_syslog) {
105                 va_start( args, fmt );
106                 vsyslog(LOG_ERR,  fmt, args );
107                 va_end( args );
108         }
109 }
110
111 void fatal_int( const char *file, const char *function, const unsigned long line, const char *fmt, ... )
112 {
113         va_list args;
114
115         log_date(stderr);
116
117         va_start( args, fmt );
118         if(debug_flags) fprintf(stderr, "FATAL (%04lu@%-10.10s:%-15.15s): %s: ", line, file, function, program_name);
119         else            fprintf(stderr, "FATAL: %s: ", program_name);
120         vfprintf( stderr, fmt, args );
121         va_end( args );
122
123         perror(" # ");
124         fprintf(stderr, "\n");
125
126         if(error_log_syslog) {
127                 va_start( args, fmt );
128                 vsyslog(LOG_CRIT,  fmt, args );
129                 va_end( args );
130         }
131
132         exit(1);
133 }
134
135 void log_access( const char *fmt, ... )
136 {
137         va_list args;
138
139         if(stdaccess) {
140                 log_date(stdaccess);
141
142                 va_start( args, fmt );
143                 vfprintf( stdaccess, fmt, args );
144                 va_end( args );
145                 fprintf( stdaccess, "\n");
146                 fflush( stdaccess );
147         }
148
149         if(access_log_syslog) {
150                 va_start( args, fmt );
151                 vsyslog(LOG_INFO,  fmt, args );
152                 va_end( args );
153         }
154 }
155