3 /* $KAME: logger.c,v 1.9 2002/09/03 14:37:03 itojun Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
70 p
= (struct log
*)racoon_malloc(sizeof(*p
));
73 memset(p
, 0, sizeof(*p
));
75 p
->buf
= (char **)racoon_malloc(sizeof(char *) * siz
);
80 memset(p
->buf
, 0, sizeof(char *) * siz
);
82 p
->tbuf
= (time_t *)racoon_malloc(sizeof(time_t *) * siz
);
83 if (p
->tbuf
== NULL
) {
88 memset(p
->tbuf
, 0, sizeof(time_t *) * siz
);
92 p
->fname
= racoon_strdup(fname
);
98 * append string to ring buffer.
99 * string must be \n-terminated (since we add timestamps).
100 * even if not, we'll add \n to avoid formatting mistake (see log_close()).
107 /* syslog if p->fname == NULL? */
109 racoon_free(p
->buf
[p
->head
]);
110 p
->buf
[p
->head
] = racoon_strdup(str
);
111 p
->tbuf
[p
->head
] = time(NULL
);
117 * write out string to the log file, as is.
118 * \n-termination is up to the caller. if you don't add \n, the file
119 * format may be broken.
128 if (p
->fname
== NULL
)
129 return -1; /*XXX syslog?*/
130 fp
= fopen(p
->fname
, "a");
133 fprintf(fp
, "%s", str
);
140 log_vprint(struct log
*p
, const char *fmt
, ...)
146 if (p
->fname
== NULL
)
147 return -1; /*XXX syslog?*/
148 fp
= fopen(p
->fname
, "a");
152 vfprintf(fp
, fmt
, ap
);
161 log_vaprint(struct log
*p
, const char *fmt
, va_list ap
)
165 if (p
->fname
== NULL
)
166 return -1; /*XXX syslog?*/
167 fp
= fopen(p
->fname
, "a");
170 vfprintf(fp
, fmt
, ap
);
177 * write out content of ring buffer, and reclaim the log structure
188 if (p
->fname
== NULL
)
190 fp
= fopen(p
->fname
, "a");
194 for (i
= 0; i
< p
->siz
; i
++) {
195 j
= (p
->head
+ i
) % p
->siz
;
197 tm
= localtime(&p
->tbuf
[j
]);
198 strftime(ts
, sizeof(ts
), "%B %d %T", tm
);
199 fprintf(fp
, "%s: %s\n", ts
, p
->buf
[j
]);
200 if (*(p
->buf
[j
] + strlen(p
->buf
[j
]) - 1) != '\n')
217 for (i
= 0; i
< p
->siz
; i
++)
218 racoon_free(p
->buf
[i
]);
220 racoon_free(p
->tbuf
);
222 racoon_free(p
->fname
);
230 vatest(const char *fmt
, ...)
234 log_vaprint(l
, fmt
, ap
);
245 l
= log_open(30, "/tmp/hoge");
249 for (i
= 0; i
< 50; i
++) {
254 log_print(l
, "hoge\n");
255 log_vprint(l
, "hoge %s\n", "this is test");
256 vatest("%s %s\n", "this is", "vprint test");