-
Notifications
You must be signed in to change notification settings - Fork 33
/
log.c
239 lines (196 loc) · 6.14 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
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* moxi logging API
* Based on log.[ch] from lighttpd source
*/
#include "config.h"
#undef write
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include "log.h"
#ifdef HAVE_VALGRIND_VALGRIND_H
#include <valgrind/valgrind.h>
#endif
#ifndef O_LARGEFILE
# define O_LARGEFILE 0
#endif
#define MAX_LOGBUF_LEN 4096
/**
* open the errorlog
*
* we have 3 possibilities:
* - stderr (default)
* - syslog
* - logfile
*
* if the open failed, report to the user and die
*/
int log_error_open(moxi_log *mlog) {
assert(mlog);
if (!mlog->logbuf) {
mlog->logbuf = calloc(1, MAX_LOGBUF_LEN + 1);
mlog->logbuf_used = 0;
}
if (mlog->log_mode == ERRORLOG_FILE) {
const char *logfile = mlog->log_file;
if (-1 == (mlog->fd = open(logfile, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) {
#ifdef HAVE_SYSLOG_H
fprintf(stderr, "ERROR: opening errorlog '%s' failed. error: %s, Switching to syslog.\n",
logfile, strerror(errno));
mlog->log_mode = ERRORLOG_SYSLOG;
#else
fprintf(stderr, "ERROR: opening errorlog '%s' failed. error: %s, Switching to stderr.\n",
logfile, strerror(errno));
mlog->log_mode = ERRORLOG_STDERR;
#endif
}
}
#ifdef HAVE_SYSLOG_H
if (mlog->log_mode == ERRORLOG_SYSLOG) {
openlog(mlog->log_ident, LOG_CONS | LOG_PID, LOG_DAEMON);
}
#endif
return 0;
}
/**
* open the errorlog
*
* if the open failed, report to the user and die
* if no filename is given, use syslog instead
*/
int log_error_cycle(moxi_log *mlog) {
/* only cycle if we are not in syslog-mode */
if (mlog->log_mode == ERRORLOG_FILE) {
const char *logfile = mlog->log_file;
/* already check of opening time */
int new_fd;
log_error_write(mlog, __FILE__, __LINE__, "About to cycle log \n");
if (-1 == (new_fd = open(logfile, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) {
#ifdef HAVE_SYSLOG_H
/* write to old log */
log_error_write(mlog, __FILE__, __LINE__,
"cycling errorlog '%s' failed: %s. failing back to syslog()",
logfile, strerror(errno));
mlog->log_mode = ERRORLOG_SYSLOG;
#else
log_error_write(mlog, __FILE__, __LINE__,
"cycling errorlog '%s' failed: %s. failing back to stderr",
logfile, strerror(errno));
mlog->log_mode = ERRORLOG_STDERR;
#endif
close(mlog->fd);
mlog->fd = -1;
} else {
/* ok, new log is open, close the old one */
close(mlog->fd);
mlog->fd = new_fd;
log_error_write(mlog, __FILE__, __LINE__, "Log Cycled \n");
}
}
return 0;
}
int log_error_close(moxi_log *mlog) {
switch(mlog->log_mode) {
case ERRORLOG_FILE:
close(mlog->fd);
break;
#ifdef HAVE_SYSLOG_H
case ERRORLOG_SYSLOG:
closelog();
break;
#endif
case ERRORLOG_STDERR:
break;
}
return 0;
}
static inline
void mappend_log(moxi_log *mlog, const char *str) {
int str_len = strlen(str);
if (mlog->logbuf_used + str_len >= MAX_LOGBUF_LEN)
str_len = MAX_LOGBUF_LEN - 1 - mlog->logbuf_used;
if (str_len <= 0)
return;
memcpy(mlog->logbuf + mlog->logbuf_used, str, str_len);
mlog->logbuf_used += str_len;
assert(mlog->logbuf_used < MAX_LOGBUF_LEN);
}
static inline
void mappend_log_int(moxi_log *mlog, int num) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", num);
mappend_log(mlog, buf);
}
int log_error_write(moxi_log *mlog, const char *filename, unsigned int line, const char *fmt, ...) {
va_list ap;
static char ts_debug_str[255];
int written = 0;
mlog->logbuf_used = 0;
switch(mlog->log_mode) {
case ERRORLOG_FILE:
case ERRORLOG_STDERR:
/* cache the generated timestamp */
if (!mlog->cur_ts) {
mlog->cur_ts = time(NULL);
}
if (mlog->cur_ts != mlog->last_generated_debug_ts) {
memset(ts_debug_str, 0, sizeof(ts_debug_str));
strftime(ts_debug_str, 254, "%Y-%m-%d %H:%M:%S", localtime(&(mlog->cur_ts)));
mlog->last_generated_debug_ts = mlog->cur_ts;
}
mappend_log(mlog, ts_debug_str);
/*mappend_log(zl, ts_debug_str)*/
mappend_log(mlog, ": (");
break;
#ifdef HAVE_SYSLOG_H
case ERRORLOG_SYSLOG:
memset(mlog->logbuf, 0, MAX_LOGBUF_LEN);
/* syslog is generating its own timestamps */
mappend_log(mlog, "(");
break;
#endif
}
mappend_log(mlog, filename);
mappend_log(mlog, ".");
mappend_log_int(mlog, line);
mappend_log(mlog, ") ");
assert(mlog->logbuf_used < MAX_LOGBUF_LEN);
va_start(ap, fmt);
mlog->logbuf_used +=
vsnprintf((mlog->logbuf + mlog->logbuf_used), (MAX_LOGBUF_LEN - mlog->logbuf_used - 1), fmt, ap);
va_end(ap);
/* vsprintf returns total string length, so no buffer overflow is
* possible, but we can shoot logbuf_used past MAX_LOGBUF_LEN */
if (mlog->logbuf_used >= MAX_LOGBUF_LEN) {
mlog->logbuf_used = MAX_LOGBUF_LEN - 1;
}
if (mlog->logbuf_used > 1) {
mlog->logbuf[mlog->logbuf_used - 1] = '\n';
}
assert(mlog->logbuf_used < MAX_LOGBUF_LEN);
mlog->logbuf[mlog->logbuf_used] = '\0';
switch(mlog->log_mode) {
case ERRORLOG_FILE:
written = write(mlog->fd, mlog->logbuf, mlog->logbuf_used);
break;
case ERRORLOG_STDERR:
written = write(STDERR_FILENO, mlog->logbuf, mlog->logbuf_used);
break;
#ifdef HAVE_SYSLOG_H
case ERRORLOG_SYSLOG:
syslog(LOG_ERR, "%s", mlog->logbuf);
break;
#endif
}
return 0;
}