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