-
Notifications
You must be signed in to change notification settings - Fork 61
/
log.c
447 lines (343 loc) · 8.61 KB
/
log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* Copyright (C) 2011 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <config.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include "monitor.h"
/**
* Implementation of a logger that appends log messages to a file
* with a preceding timestamp. Methods support both syslog or own
* logfile.
*
* @author Jan-Henrik Haukeland, <[email protected]>
* @author Martin Pala, <[email protected]>
*
* @file
*/
/* ------------------------------------------------------------- Definitions */
static FILE *LOG= NULL;
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
static struct mylogpriority {
int priority;
char *description;
} logPriority[]= {
{LOG_EMERG, "emergency"},
{LOG_ALERT, "alert"},
{LOG_CRIT, "critical"},
{LOG_ERR, "error"},
{LOG_WARNING, "warning"},
{LOG_NOTICE, "notice"},
{LOG_INFO, "info"},
{LOG_DEBUG, "debug"},
{-1, NULL}
};
/* -------------------------------------------------------------- Prototypes */
static int open_log();
static char *timefmt(char *t, int size);
static const char *logPriorityDescription(int p);
static void log_log(int priority, const char *s, va_list ap);
static void log_backtrace();
/* ------------------------------------------------------------------ Public */
/**
* Initialize the log system and 'log' function
* @return TRUE if the log system was successfully initialized
*/
int log_init() {
if (!Run.dolog) {
return TRUE;
}
if (!open_log()) {
return FALSE;
}
/* Register log_close to be
called at program termination */
atexit(log_close);
return TRUE;
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogEmergency(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_EMERG, s, ap);
va_end(ap);
log_backtrace();
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogAlert(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_ALERT, s, ap);
va_end(ap);
log_backtrace();
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogCritical(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_CRIT, s, ap);
va_end(ap);
log_backtrace();
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogError(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_ERR, s, ap);
va_end(ap);
log_backtrace();
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogWarning(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_WARNING, s, ap);
va_end(ap);
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogNotice(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_NOTICE, s, ap);
va_end(ap);
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogInfo(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_INFO, s, ap);
va_end(ap);
}
/**
* Logging interface with priority support
* @param s A formated (printf-style) string to log
*/
void LogDebug(const char *s, ...) {
va_list ap;
ASSERT(s);
va_start(ap, s);
log_log(LOG_DEBUG, s, ap);
va_end(ap);
}
/**
* Close the log file or syslog
*/
void log_close() {
if (Run.use_syslog) {
closelog();
}
if (LOG && (0 != fclose(LOG))) {
LogError("%s: Error closing the log file -- %s\n", prog, STRERROR);
}
LOG= NULL;
}
#ifndef HAVE_VSYSLOG
#ifdef HAVE_SYSLOG
void vsyslog (int facility_priority, const char *format, va_list arglist) {
char msg[STRLEN+1];
vsnprintf(msg, STRLEN, format, arglist);
syslog(facility_priority, "%s", msg);
}
#endif /* HAVE_SYSLOG */
#endif /* HAVE_VSYSLOG */
/* ----------------------------------------------------------------- Private */
/**
* Open a log file or syslog
*/
static int open_log() {
if (Run.use_syslog) {
openlog(prog, LOG_PID, Run.facility);
} else {
umask(LOGMASK);
if ((LOG= fopen(Run.logfile,"a+")) == (FILE *)NULL) {
LogError("%s: Error opening the log file '%s' for writing -- %s\n", prog, Run.logfile, STRERROR);
return(FALSE);
}
/* Set logger in unbuffered mode */
setvbuf(LOG, NULL, _IONBF, 0);
}
return TRUE;
}
/**
* Returns the current time as a formated string, see the TIMEFORMAT
* macro in monitor.h
*/
static char *timefmt(char *t, int size) {
time_t now;
struct tm tm;
time(&now);
localtime_r(&now, &tm);
if ( !strftime(t, size, TIMEFORMAT, &tm))
*t = 0;
return t;
}
/**
* Get a textual description of the actual log priority.
* @param p The log priority
* @return A string describing the log priority in clear text. If the
* priority is not found NULL is returned.
*/
static const char *logPriorityDescription(int p) {
struct mylogpriority *lp= logPriority;
while ((*lp).description)
{
if (p == (*lp).priority)
{
return (*lp).description;
}
lp++;
}
return "unknown";
}
/**
* Log a message to monits logfile or syslog.
* @param priority A message priority
* @param s A formated (printf-style) string to log
*/
static void log_log(int priority, const char *s, va_list ap) {
#ifdef HAVE_VA_COPY
va_list ap_copy;
#endif
ASSERT(s);
LOCK(log_mutex)
#ifdef HAVE_VA_COPY
va_copy(ap_copy, ap);
vfprintf(stderr, s, ap_copy);
va_end(ap_copy);
#else
vfprintf(stderr, s, ap);
#endif
fflush(stderr);
if (Run.dolog) {
if (Run.use_syslog) {
#ifdef HAVE_VA_COPY
va_copy(ap_copy, ap);
vsyslog(priority, s, ap_copy);
va_end(ap_copy);
#else
vsyslog(priority, s, ap);
#endif
} else if (LOG) {
char datetime[STRLEN];
fprintf(LOG, "[%s] %-8s : ", timefmt(datetime, STRLEN), logPriorityDescription(priority));
#ifdef HAVE_VA_COPY
va_copy(ap_copy, ap);
vfprintf(LOG, s, ap_copy);
va_end(ap_copy);
#else
vfprintf(LOG, s, ap);
#endif
}
}
END_LOCK;
}
static void log_backtrace() {
#ifdef HAVE_BACKTRACE
int i, frames;
void *callstack[128];
char **strs;
if (Run.debug) {
frames = backtrace(callstack, 128);
strs = backtrace_symbols(callstack, frames);
LogDebug("-------------------------------------------------------------------------------\n");
for (i = 0; i < frames; ++i)
LogDebug(" %s\n", strs[i]);
LogDebug("-------------------------------------------------------------------------------\n");
FREE(strs);
}
#endif
}