-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
213 lines (177 loc) · 3.56 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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include "timespec.h"
#include "log.h"
static FILE *log_file = NULL;
static int priority_max_level = LOG_NOTICE;
static struct {
char *name;
int val;
} priority_names[] = {
{ "emerg", LOG_EMERG }, /* system in unusable */
{ "alert", LOG_ALERT }, /* action must be taken immediately */
{ "crit", LOG_CRIT }, /* critical conditions */
{ "err", LOG_ERR }, /* error conditions */
{ "warning", LOG_WARNING }, /* warning conditions */
{ "notice", LOG_NOTICE }, /* normal but significant condition */
{ "info", LOG_INFO }, /* informational */
{ "debug", LOG_DEBUG }, /* debug-level messages */
{ NULL, -1 }
};
int
log_priority_from_name(char *name)
{
int i = 0;
if (name == NULL)
return (-1);
while (priority_names[i].name) {
if (strcmp(priority_names[i].name, name) == 0)
break;
++i;
}
return (priority_names[i].val);
}
char *
log_name_from_priority(int priority)
{
if (priority < 0 || priority > LOG_DEBUG)
return ("unknown");
if (priority_names[priority].val == priority)
return (priority_names[priority].name);
return ("unknown");
}
void
log_set_priority_max_level(int priority)
{
priority_max_level = priority;
}
static void
log_time(char *s, int size)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
timespec_str(&ts, s, size);
}
static void
log_msg_syslog(int priority, const char *buf)
{
syslog(priority, "%s", buf);
}
#define TIMEBUF_SIZE 256
static void
log_msg_stderr(int priority, const char *buf)
{
char tb[TIMEBUF_SIZE];
log_time(tb, TIMEBUF_SIZE);
fprintf(stderr, "%s: <%s> %s\n", tb, log_name_from_priority(priority),
buf);
}
static void
log_msg_file(int priority, const char *buf)
{
char tb[TIMEBUF_SIZE];
log_time(tb, TIMEBUF_SIZE);
fprintf(log_file, "%s: <%s> %s\n", tb, log_name_from_priority(priority),
buf);
}
static void
(*log_msg)(int priority, const char *buf)
= log_msg_stderr;
static void
log_vmessage(int priority, const char *format, va_list ap)
{
char buffer[2048];
if (priority > priority_max_level)
return;
vsnprintf(buffer, sizeof buffer, format, ap);
log_msg(priority, buffer);
}
void
log_message(int priority, const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(priority, format, ap);
va_end(ap);
}
void
log_error(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(LOG_ERR, format, ap);
va_end(ap);
}
void
log_warning(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(LOG_WARNING, format, ap);
va_end(ap);
}
void
log_notice(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(LOG_NOTICE, format, ap);
va_end(ap);
}
void
log_info(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(LOG_INFO, format, ap);
va_end(ap);
}
void
log_debug(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(LOG_DEBUG, format, ap);
va_end(ap);
}
void
log_fatal(const char *format, ...)
{
va_list ap;
va_start(ap, format);
log_vmessage(LOG_ERR, format, ap);
va_end(ap);
exit(2);
}
void
log_syslog_open(const char *log_identifier, int option, int facility)
{
openlog(log_identifier, option, facility);
log_msg = log_msg_syslog;
}
int
log_file_open(const char *path)
{
FILE *f;
f = fopen(path, "a");
if (f == NULL)
return (-1);
if (log_file != NULL)
fclose(log_file);
setbuf(f, NULL);
log_file = f;
log_msg = log_msg_file;
return (0);
}
void
log_term()
{
if (log_file)
fclose(log_file);
log_file = NULL;
log_msg = log_msg_stderr;
}