forked from radvd-project/radvd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
161 lines (143 loc) · 3.54 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
/*
*
* Authors:
* Lars Fenneberg <[email protected]>
*
* This software is Copyright 1996,1997 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file
* COPYRIGHT applies to this software. If your distribution is missing
* this file, you may request it from <[email protected]>.
*
*/
#include "config.h"
#include "includes.h"
#include "radvd.h"
__attribute__((format(printf, 2, 0))) static int vlog(int prio, char const *format, va_list ap);
static int log_method = L_NONE;
static char const *log_ident;
static char const *log_file;
static FILE *log_file_fd;
static int log_facility;
static int debug_level = 0;
int log_open(int method, char const *ident, char const *log, int facility)
{
log_method = method;
log_ident = ident;
switch (log_method) {
case L_NONE:
case L_UNSPEC:
case L_STDERR:
break;
case L_STDERR_CLEAN:
/* fallthrough */
case L_STDERR_SYSLOG:
/* fallthrough */
case L_SYSLOG:
if (facility == -1)
log_facility = LOG_DAEMON;
else
log_facility = facility;
openlog(log_ident, LOG_PID, log_facility);
break;
case L_LOGFILE:
if (!log) {
fprintf(stderr, "%s (%d): no logfile specified\n", log_ident, getpid());
return -1;
}
log_file = log;
if ((log_file_fd = fopen(log_file, "a")) == NULL) {
fprintf(stderr, "%s (%d): can't open %s: %s\n", log_ident, getpid(), log_file, strerror(errno));
return -1;
}
break;
default:
fprintf(stderr, "%s (%d): unknown logging method: %d\n", log_ident, getpid(), log_method);
log_method = L_NONE;
return -1;
}
return 0;
}
/* note: [dfv]log() is also called from root context */
__attribute__((format(printf, 2, 0))) static int vlog(int prio, char const *format, va_list ap)
{
char tstamp[64], buff[1024];
struct tm *tm;
time_t current;
vsnprintf(buff, sizeof(buff), format, ap);
switch (log_method) {
case L_NONE:
case L_UNSPEC:
break;
case L_SYSLOG:
syslog(prio, "%s", buff);
break;
case L_STDERR_SYSLOG:
syslog(prio, "%s", buff);
if (debug_level < prio) /* fall through for messages with high priority */
break;
case L_STDERR:
current = time(NULL);
tm = localtime(¤t);
(void)strftime(tstamp, sizeof(tstamp), LOG_TIME_FORMAT, tm);
fprintf(stderr, "[%s] %s (%d): %s\n", tstamp, log_ident, getpid(), buff);
fflush(stderr);
break;
case L_STDERR_CLEAN:
fprintf(stderr, "%s\n", buff);
fflush(stderr);
break;
case L_LOGFILE:
current = time(NULL);
tm = localtime(¤t);
(void)strftime(tstamp, sizeof(tstamp), LOG_TIME_FORMAT, tm);
fprintf(log_file_fd, "[%s] %s (%d): %s\n", tstamp, log_ident, getpid(), buff);
fflush(log_file_fd);
break;
default:
fprintf(stderr, "%s (%d): unknown logging method: %d\n", log_ident, getpid(), log_method);
log_method = L_NONE;
return -1;
}
return 0;
}
void dlog(int prio, int level, char const *format, ...)
{
if (debug_level < level)
return;
va_list ap;
va_start(ap, format);
vlog(prio, format, ap);
va_end(ap);
}
void flog(int prio, char const *format, ...)
{
va_list ap;
va_start(ap, format);
vlog(prio, format, ap);
va_end(ap);
}
int log_close(void)
{
switch (log_method) {
case L_NONE:
case L_UNSPEC:
case L_STDERR:
break;
case L_STDERR_SYSLOG:
case L_SYSLOG:
closelog();
break;
case L_LOGFILE:
fclose(log_file_fd);
break;
default:
fprintf(stderr, "%s (%d): unknown logging method: %d\n", log_ident, getpid(), log_method);
log_method = L_NONE;
return -1;
}
return 0;
}
void set_debuglevel(int level) { debug_level = level; }
int get_debuglevel(void) { return debug_level; }