]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/logger.c
b42a9d3f77ba358d9b5c73b889f5851c827c5023
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
32 #include <sys/types.h>
33 #include <sys/param.h>
44 #if TIME_WITH_SYS_TIME
45 # include <sys/time.h>
49 # include <sys/time.h>
66 p
= (struct log
*)racoon_malloc(sizeof(*p
));
69 memset(p
, 0, sizeof(*p
));
71 p
->buf
= (char **)racoon_malloc(sizeof(char *) * siz
);
76 memset(p
->buf
, 0, sizeof(char *) * siz
);
78 p
->tbuf
= (time_t *)racoon_malloc(sizeof(time_t *) * siz
);
79 if (p
->tbuf
== NULL
) {
84 memset(p
->tbuf
, 0, sizeof(time_t *) * siz
);
88 p
->fname
= strdup(fname
);
94 * append string to ring buffer.
95 * string must be \n-terminated (since we add timestamps).
96 * even if not, we'll add \n to avoid formatting mistake (see log_close()).
103 /* syslog if p->fname == NULL? */
105 racoon_free(p
->buf
[p
->head
]);
106 p
->buf
[p
->head
] = strdup(str
);
107 p
->tbuf
[p
->head
] = time(NULL
);
113 * write out string to the log file, as is.
114 * \n-termination is up to the caller. if you don't add \n, the file
115 * format may be broken.
124 if (p
->fname
== NULL
)
125 return -1; /*XXX syslog?*/
126 fp
= fopen(p
->fname
, "a");
129 fprintf(fp
, "%s", str
);
136 log_vprint(struct log
*p
, const char *fmt
, ...)
142 if (p
->fname
== NULL
)
143 return -1; /*XXX syslog?*/
144 fp
= fopen(p
->fname
, "a");
148 vfprintf(fp
, fmt
, ap
);
157 log_vaprint(struct log
*p
, const char *fmt
, va_list ap
)
161 if (p
->fname
== NULL
)
162 return -1; /*XXX syslog?*/
163 fp
= fopen(p
->fname
, "a");
166 vfprintf(fp
, fmt
, ap
);
173 * write out content of ring buffer, and reclaim the log structure
184 if (p
->fname
== NULL
)
186 fp
= fopen(p
->fname
, "a");
190 for (i
= 0; i
< p
->siz
; i
++) {
191 j
= (p
->head
+ i
) % p
->siz
;
193 tm
= localtime(&p
->tbuf
[j
]);
194 strftime(ts
, sizeof(ts
), "%B %d %T", tm
);
195 fprintf(fp
, "%s: %s\n", ts
, p
->buf
[j
]);
196 if (*(p
->buf
[j
] + strlen(p
->buf
[j
]) - 1) != '\n')
213 for (i
= 0; i
< p
->siz
; i
++)
214 racoon_free(p
->buf
[i
]);
216 racoon_free(p
->tbuf
);
218 racoon_free(p
->fname
);
226 vatest(const char *fmt
, ...)
230 log_vaprint(l
, fmt
, ap
);
241 l
= log_open(30, "/tmp/hoge");
245 for (i
= 0; i
< 50; i
++) {
250 log_print(l
, "hoge\n");
251 log_vprint(l
, "hoge %s\n", "this is test");
252 vatest("%s %s\n", "this is", "vprint test");