forked from logrotate/logrotate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
108 lines (92 loc) · 2.12 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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef HAVE_VSYSLOG
#include <syslog.h>
#endif
#include "log.h"
static int logLevel = MESS_DEBUG;
static FILE *messageFile = NULL;
static int _logToSyslog = 0;
void logSetLevel(int level)
{
logLevel = level;
}
void logSetMessageFile(FILE * f)
{
messageFile = f;
}
void logToSyslog(int enable) {
_logToSyslog = enable;
#ifdef HAVE_VSYSLOG
if (_logToSyslog) {
openlog("logrotate", 0, LOG_USER);
}
else {
closelog();
}
#endif
}
__attribute__((format (printf, 3, 0)))
static void log_once(FILE *where, int level, const char *format, va_list args)
{
switch (level) {
case MESS_DEBUG:
case MESS_NORMAL:
case MESS_VERBOSE:
break;
default:
fprintf(where, "error: ");
break;
}
vfprintf(where, format, args);
fflush(where);
}
__attribute__((format (printf, 2, 3)))
void message(int level, const char *format, ...)
{
va_list args;
if (level >= logLevel) {
va_start(args, format);
log_once(stderr, level, format, args);
va_end(args);
}
if (messageFile != NULL) {
va_start(args, format);
log_once(messageFile, level, format, args);
va_end(args);
}
#ifdef HAVE_VSYSLOG
if (_logToSyslog) {
int priority = LOG_USER;
switch(level) {
case MESS_REALDEBUG:
priority |= LOG_DEBUG;
break;
case MESS_DEBUG:
case MESS_VERBOSE:
case MESS_NORMAL:
priority |= LOG_INFO;
break;
case MESS_ERROR:
priority |= LOG_ERR;
break;
case MESS_FATAL:
priority |= LOG_CRIT;
break;
default:
priority |= LOG_INFO;
break;
}
va_start(args, format);
vsyslog(priority, format, args);
va_end(args);
}
#endif
if (level == MESS_FATAL)
exit(1);
}
/* vim: set et sw=4 ts=4: */