]>
git.saurik.com Git - apple/ipsec.git/blob - ipsec-tools/racoon/logger.c
1 /* $KAME: logger.c,v 1.9 2002/09/03 14:37:03 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/types.h>
35 #include <sys/param.h>
47 #if TIME_WITH_SYS_TIME
48 # include <sys/time.h>
52 # include <sys/time.h>
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)
67 static int log_flush (struct log
*p
, int newbytes
)
76 if (!p
->byteswritten
) {
77 bzero(&st
, sizeof(st
));
78 if (fstat(fileno(p
->fp
), &st
) < 0) {
84 p
->byteswritten
= st
.st_size
;
87 p
->byteswritten
+= newbytes
;
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
93 size_t discard
, saved
= 0;
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
;
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)
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
) {
121 // delete file and start appending logs again
122 p
->fp
= freopen(p
->fname
, "wa", p
->fp
);
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)
130 (void)fwrite(buf
, saved
, sizeof(*buf
), p
->fp
);
147 p
= (struct log
*)racoon_malloc(sizeof(*p
));
150 memset(p
, 0, sizeof(*p
));
152 p
->buf
= (char **)racoon_malloc(sizeof(char *) * siz
);
153 if (p
->buf
== NULL
) {
157 memset(p
->buf
, 0, sizeof(char *) * siz
);
159 p
->tbuf
= (time_t *)racoon_malloc(sizeof(time_t *) * siz
);
160 if (p
->tbuf
== NULL
) {
165 memset(p
->tbuf
, 0, sizeof(time_t *) * siz
);
169 p
->fname
= racoon_strdup(fname
);
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()).
184 /* syslog if p->fname == NULL? */
186 racoon_free(p
->buf
[p
->head
]);
187 p
->buf
[p
->head
] = racoon_strdup(str
);
188 p
->tbuf
[p
->head
] = time(NULL
);
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.
205 if (p
->fname
== NULL
)
206 return -1; /*XXX syslog?*/
208 p
->fp
= fopen(p
->fname
, "a");
212 bytes
= fprintf(p
->fp
, "%s", str
);
213 if (log_flush(p
, bytes
)) {
221 log_vprint(struct log
*p
, const char *fmt
, ...)
226 if (p
->fname
== NULL
)
227 return -1; /*XXX syslog?*/
229 p
->fp
= fopen(p
->fname
, "a");
234 bytes
= vfprintf(p
->fp
, fmt
, ap
);
236 if (log_flush(p
, bytes
)) {
244 log_vaprint(struct log
*p
, const char *fmt
, va_list ap
)
248 if (p
->fname
== NULL
)
249 return -1; /*XXX syslog?*/
251 p
->fp
= fopen(p
->fname
, "a");
255 bytes
= vfprintf(p
->fp
, fmt
, ap
);
256 if (log_flush(p
, bytes
)) {
264 * write out content of ring buffer, and reclaim the log structure
275 if (p
->fname
== NULL
)
278 p
->fp
= fopen(p
->fname
, "a");
283 for (i
= 0; i
< p
->siz
; i
++) {
284 j
= (p
->head
+ i
) % p
->siz
;
286 tm
= localtime(&p
->tbuf
[j
]);
287 strftime(ts
, sizeof(ts
), "%B %d %T", tm
);
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
);
308 for (i
= 0; i
< p
->siz
; i
++)
309 racoon_free(p
->buf
[i
]);
311 racoon_free(p
->tbuf
);
313 racoon_free(p
->fname
);
324 vatest(const char *fmt
, ...)
328 log_vaprint(l
, fmt
, ap
);
339 l
= log_open(30, "/tmp/hoge");
343 for (i
= 0; i
< 50; i
++) {
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");