]>
Commit | Line | Data |
---|---|---|
52b7d2ce A |
1 | /* $KAME: logger.c,v 1.9 2002/09/03 14:37:03 itojun Exp $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * 3. Neither the name of the project nor the names of its contributors | |
16 | * may be used to endorse or promote products derived from this software | |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
32 | #include "config.h" | |
33 | ||
34 | #include <sys/types.h> | |
35 | #include <sys/param.h> | |
e8d9021d | 36 | #include <sys/stat.h> |
52b7d2ce A |
37 | |
38 | #include <stdlib.h> | |
39 | #include <stdio.h> | |
40 | #include <string.h> | |
41 | #include <errno.h> | |
42 | #ifdef HAVE_STDARG_H | |
43 | #include <stdarg.h> | |
44 | #else | |
45 | #include <varargs.h> | |
46 | #endif | |
47 | #if TIME_WITH_SYS_TIME | |
48 | # include <sys/time.h> | |
49 | # include <time.h> | |
50 | #else | |
51 | # if HAVE_SYS_TIME_H | |
52 | # include <sys/time.h> | |
53 | # else | |
54 | # include <time.h> | |
55 | # endif | |
56 | #endif | |
57 | ||
58 | #include "logger.h" | |
59 | #include "var.h" | |
60 | #include "gcmalloc.h" | |
61 | ||
e8d9021d A |
62 | #define MAX_LOG_FILESIZE_BYTES 2097152 // 2MB |
63 | #define MAX_LOG_FILESIZE_KBYTES (MAX_LOG_FILESIZE_BYTES/1024) | |
64 | #define MAX_LOG_FILESIZE_MBYTES (MAX_LOG_FILESIZE_BYTES/(1024 * 1024)) | |
65 | #define LOG_DISCARD_BYTES (MAX_LOG_FILESIZE_BYTES/3) | |
66 | ||
67 | static int log_flush (struct log *p, int newbytes) | |
68 | { | |
69 | struct stat st; | |
70 | int good = 0; | |
71 | ||
72 | if (!p || !p->fp) { | |
73 | return -1; | |
74 | } | |
75 | ||
76 | if (!p->byteswritten) { | |
77 | bzero(&st, sizeof(st)); | |
78 | if (fstat(fileno(p->fp), &st) < 0) { | |
79 | return -1; | |
80 | } | |
81 | if (st.st_size < 0) { | |
82 | return -1; | |
83 | } | |
84 | p->byteswritten = st.st_size; | |
85 | } | |
86 | if (newbytes > 0) { | |
87 | p->byteswritten += newbytes; | |
88 | } | |
89 | ||
90 | if (p->byteswritten > MAX_LOG_FILESIZE_BYTES) { | |
91 | // hack to delete the first 1/3 of the file: won't work on some devices because malloc(MAX_LOG_FILESIZE_BYTES) fails | |
92 | char *buf = NULL; | |
93 | size_t discard, saved = 0; | |
94 | FILE *fp; | |
95 | ||
96 | // calc how much to seek into the file | |
97 | discard = p->byteswritten/3; | |
98 | if (discard < LOG_DISCARD_BYTES) { | |
99 | discard = LOG_DISCARD_BYTES; | |
100 | } | |
101 | fp = fopen(p->fname, "r"); | |
102 | // get a temp buffer to hold the last 2/3 of the file | |
103 | buf = malloc(MAX_LOG_FILESIZE_BYTES); | |
104 | // seek into the file (skipping the first 1/3 of the file) | |
105 | if (fp && buf) { | |
106 | if (fseeko(fp, discard, SEEK_SET) == 0) { | |
107 | // try reading as much as possible.. shouldn't fill up buffer | |
108 | saved = fread(buf, MAX_LOG_FILESIZE_BYTES, sizeof(*buf), fp); | |
109 | // p->byteswritten may be inaccurate (e.g another stream is writing to the file) | |
110 | if (saved == MAX_LOG_FILESIZE_BYTES) { | |
111 | saved = 0; | |
112 | } | |
113 | } | |
114 | } | |
115 | if (fp) { | |
116 | fclose(fp); | |
117 | } | |
118 | ||
119 | p->byteswritten = 0; | |
120 | (void)fpurge(p->fp); | |
121 | // delete file and start appending logs again | |
122 | p->fp = freopen(p->fname, "wa", p->fp); | |
123 | if (p->fp == NULL) | |
124 | return -1; | |
125 | fprintf(p->fp, "logfile turned over due to size>%d%s\n", | |
126 | (MAX_LOG_FILESIZE_MBYTES > 0)? MAX_LOG_FILESIZE_MBYTES:MAX_LOG_FILESIZE_KBYTES, | |
127 | (MAX_LOG_FILESIZE_MBYTES > 0)? "MB":"KB"); | |
128 | // append some of the previous logs (if successfully we buffered 2/3 of the file) | |
129 | if (buf && saved) { | |
130 | (void)fwrite(buf, saved, sizeof(*buf), p->fp); | |
131 | } | |
132 | if (buf) { | |
133 | free(buf); | |
134 | } | |
135 | } | |
136 | (void)fflush(p->fp); | |
137 | return 0; | |
138 | } | |
139 | ||
52b7d2ce A |
140 | struct log * |
141 | log_open(siz, fname) | |
142 | size_t siz; | |
143 | char *fname; | |
144 | { | |
145 | struct log *p; | |
146 | ||
147 | p = (struct log *)racoon_malloc(sizeof(*p)); | |
148 | if (p == NULL) | |
149 | return NULL; | |
150 | memset(p, 0, sizeof(*p)); | |
151 | ||
152 | p->buf = (char **)racoon_malloc(sizeof(char *) * siz); | |
153 | if (p->buf == NULL) { | |
154 | racoon_free(p); | |
155 | return NULL; | |
156 | } | |
157 | memset(p->buf, 0, sizeof(char *) * siz); | |
158 | ||
159 | p->tbuf = (time_t *)racoon_malloc(sizeof(time_t *) * siz); | |
160 | if (p->tbuf == NULL) { | |
161 | racoon_free(p->buf); | |
162 | racoon_free(p); | |
163 | return NULL; | |
164 | } | |
165 | memset(p->tbuf, 0, sizeof(time_t *) * siz); | |
166 | ||
167 | p->siz = siz; | |
168 | if (fname) | |
d1e348cf | 169 | p->fname = racoon_strdup(fname); |
52b7d2ce A |
170 | |
171 | return p; | |
172 | } | |
173 | ||
174 | /* | |
175 | * append string to ring buffer. | |
176 | * string must be \n-terminated (since we add timestamps). | |
177 | * even if not, we'll add \n to avoid formatting mistake (see log_close()). | |
178 | */ | |
179 | void | |
180 | log_add(p, str) | |
181 | struct log *p; | |
182 | char *str; | |
183 | { | |
184 | /* syslog if p->fname == NULL? */ | |
185 | if (p->buf[p->head]) | |
186 | racoon_free(p->buf[p->head]); | |
d1e348cf | 187 | p->buf[p->head] = racoon_strdup(str); |
52b7d2ce A |
188 | p->tbuf[p->head] = time(NULL); |
189 | p->head++; | |
190 | p->head %= p->siz; | |
191 | } | |
192 | ||
193 | /* | |
194 | * write out string to the log file, as is. | |
195 | * \n-termination is up to the caller. if you don't add \n, the file | |
196 | * format may be broken. | |
197 | */ | |
198 | int | |
199 | log_print(p, str) | |
200 | struct log *p; | |
201 | char *str; | |
202 | { | |
e8d9021d | 203 | int bytes; |
52b7d2ce A |
204 | |
205 | if (p->fname == NULL) | |
206 | return -1; /*XXX syslog?*/ | |
e8d9021d A |
207 | if (p->fp == NULL) { |
208 | p->fp = fopen(p->fname, "a"); | |
209 | } | |
210 | if (p->fp == NULL) | |
52b7d2ce | 211 | return -1; |
e8d9021d A |
212 | bytes = fprintf(p->fp, "%s", str); |
213 | if (log_flush(p, bytes)) { | |
214 | return -1; | |
215 | } | |
52b7d2ce A |
216 | |
217 | return 0; | |
218 | } | |
219 | ||
220 | int | |
221 | log_vprint(struct log *p, const char *fmt, ...) | |
222 | { | |
223 | va_list ap; | |
e8d9021d | 224 | int bytes; |
52b7d2ce A |
225 | |
226 | if (p->fname == NULL) | |
227 | return -1; /*XXX syslog?*/ | |
e8d9021d A |
228 | if (p->fp == NULL) { |
229 | p->fp = fopen(p->fname, "a"); | |
230 | } | |
231 | if (p->fp == NULL) | |
52b7d2ce A |
232 | return -1; |
233 | va_start(ap, fmt); | |
e8d9021d | 234 | bytes = vfprintf(p->fp, fmt, ap); |
52b7d2ce | 235 | va_end(ap); |
e8d9021d A |
236 | if (log_flush(p, bytes)) { |
237 | return -1; | |
238 | } | |
52b7d2ce A |
239 | |
240 | return 0; | |
241 | } | |
242 | ||
243 | int | |
244 | log_vaprint(struct log *p, const char *fmt, va_list ap) | |
245 | { | |
e8d9021d | 246 | int bytes; |
52b7d2ce A |
247 | |
248 | if (p->fname == NULL) | |
249 | return -1; /*XXX syslog?*/ | |
e8d9021d A |
250 | if (p->fp == NULL) { |
251 | p->fp = fopen(p->fname, "a"); | |
252 | } | |
253 | if (p->fp == NULL) | |
52b7d2ce | 254 | return -1; |
e8d9021d A |
255 | bytes = vfprintf(p->fp, fmt, ap); |
256 | if (log_flush(p, bytes)) { | |
257 | return -1; | |
258 | } | |
52b7d2ce A |
259 | |
260 | return 0; | |
261 | } | |
262 | ||
263 | /* | |
264 | * write out content of ring buffer, and reclaim the log structure | |
265 | */ | |
266 | int | |
267 | log_close(p) | |
268 | struct log *p; | |
269 | { | |
52b7d2ce A |
270 | int i, j; |
271 | char ts[256]; | |
272 | struct tm *tm; | |
e8d9021d | 273 | int bytes; |
52b7d2ce A |
274 | |
275 | if (p->fname == NULL) | |
276 | goto nowrite; | |
e8d9021d A |
277 | if (p->fp == NULL) { |
278 | p->fp = fopen(p->fname, "a"); | |
279 | } | |
280 | if (p->fp == NULL) | |
52b7d2ce A |
281 | goto nowrite; |
282 | ||
283 | for (i = 0; i < p->siz; i++) { | |
284 | j = (p->head + i) % p->siz; | |
285 | if (p->buf[j]) { | |
286 | tm = localtime(&p->tbuf[j]); | |
287 | strftime(ts, sizeof(ts), "%B %d %T", tm); | |
e8d9021d A |
288 | bytes = fprintf(p->fp, "%s: %s\n", ts, p->buf[j]); |
289 | (void)log_flush(p, bytes); | |
290 | if (*(p->buf[j] + strlen(p->buf[j]) - 1) != '\n') { | |
291 | bytes = fprintf(p->fp, "\n"); | |
292 | (void)log_flush(p, bytes); | |
293 | } | |
52b7d2ce A |
294 | } |
295 | } | |
52b7d2ce A |
296 | |
297 | nowrite: | |
298 | log_free(p); | |
299 | return 0; | |
300 | } | |
301 | ||
302 | void | |
303 | log_free(p) | |
304 | struct log *p; | |
305 | { | |
306 | int i; | |
307 | ||
308 | for (i = 0; i < p->siz; i++) | |
309 | racoon_free(p->buf[i]); | |
310 | racoon_free(p->buf); | |
311 | racoon_free(p->tbuf); | |
312 | if (p->fname) | |
313 | racoon_free(p->fname); | |
e8d9021d A |
314 | if (p->fp) { |
315 | fclose(p->fp); | |
316 | } | |
52b7d2ce A |
317 | racoon_free(p); |
318 | } | |
319 | ||
320 | #ifdef TEST | |
321 | struct log *l; | |
322 | ||
323 | void | |
324 | vatest(const char *fmt, ...) | |
325 | { | |
326 | va_list ap; | |
327 | va_start(ap, fmt); | |
328 | log_vaprint(l, fmt, ap); | |
329 | va_end(ap); | |
330 | } | |
331 | ||
332 | int | |
333 | main(argc, argv) | |
334 | int argc; | |
335 | char **argv; | |
336 | { | |
337 | int i; | |
338 | ||
339 | l = log_open(30, "/tmp/hoge"); | |
340 | if (l == NULL) | |
341 | errx(1, "hoge"); | |
342 | ||
343 | for (i = 0; i < 50; i++) { | |
344 | log_add(l, "foo"); | |
345 | log_add(l, "baa"); | |
346 | log_add(l, "baz"); | |
347 | } | |
348 | log_print(l, "hoge\n"); | |
349 | log_vprint(l, "hoge %s\n", "this is test"); | |
350 | vatest("%s %s\n", "this is", "vprint test"); | |
351 | abort(); | |
352 | log_free(l); | |
353 | } | |
354 | ||
355 | #endif | |
356 |