1 /* $NetBSD: plog.c,v 1.5 2007/10/02 09:47:40 vanhu Exp $ */
3 /* Id: plog.c,v 1.11 2006/06/20 09:57:31 vanhu 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>
69 # define VA_COPY(dst,src) memcpy(&(dst), &(src), sizeof(va_list))
73 u_int32_t loglevel
= LLV_BASE
;
76 int print_location
= 0;
78 static struct log
*logp
= NULL
;
79 static char *logfile
= NULL
;
81 static char *plog_common
__P((int, const char *, const char *));
83 static struct plogtags
{
87 { "(not defined)", 0, },
88 { "ERROR", LOG_INFO
, },
89 { "WARNING", LOG_INFO
, },
90 { "NOTIFY", LOG_INFO
, },
91 { "INFO", LOG_INFO
, },
92 { "DEBUG", LOG_DEBUG
, },
93 { "DEBUG2", LOG_DEBUG
, },
97 plog_common(pri
, fmt
, func
)
99 const char *fmt
, *func
;
101 static char buf
[800]; /* XXX shoule be allocated every time ? */
106 reslen
= sizeof(buf
);
108 if (logfile
|| f_foreground
) {
114 len
= strftime(p
, reslen
, "%Y-%m-%d %T: ", tm
);
119 if (pri
< ARRAYLEN(ptab
)) {
120 len
= snprintf(p
, reslen
, "%s: ", ptab
[pri
].name
);
121 if (len
>= 0 && len
< reslen
) {
129 snprintf(p
, reslen
, "%s: %s", func
, fmt
);
131 snprintf(p
, reslen
, "%s", fmt
);
133 while ((p
= strstr(buf
,"%z")) != NULL
)
141 _plog(int pri
, const char *func
, struct sockaddr
*sa
, const char *fmt
, ...)
146 plogv(pri
, func
, sa
, fmt
, ap
);
151 plogv(int pri
, const char *func
, struct sockaddr
*sa
,
152 const char *fmt
, va_list ap
)
160 newfmt
= plog_common(pri
, fmt
, func
);
168 log_vaprint(logp
, newfmt
, ap_bak
);
170 if (pri
< ARRAYLEN(ptab
))
171 vsyslog(ptab
[pri
].priority
, newfmt
, ap_bak
);
173 vsyslog(LOG_ALERT
, newfmt
, ap_bak
);
178 plogdump(pri
, data
, len
)
191 * 2 words a bytes + 1 space 4 bytes + 1 newline 32 bytes
194 buflen
= (len
* 2) + (len
/ 4) + (len
/ 32) + 3;
195 buf
= racoon_malloc(buflen
);
205 snprintf(&buf
[i
], buflen
- i
, "%02x",
206 ((unsigned char *)data
)[j
] & 0xff);
210 if (buflen
- i
>= 2) {
214 plog(pri
, LOCATION
, NULL
, "%s", buf
);
223 logp
= log_open(250, logfile
);
225 errx(1, "ERROR: failed to open log file %s.", logfile
);
229 openlog(pname
, LOG_NDELAY
, LOG_DAEMON
);
237 racoon_free(logfile
);
238 logfile
= racoon_strdup(file
);
239 STRDUP_FATAL(logfile
);
243 Returns a printable string from (possibly) binary data ;
244 concatenates all unprintable chars to one space.
245 XXX Maybe the printable chars range is too large...
248 binsanitize(binstr
, n
)
255 d
= racoon_malloc(n
+ 1);
256 for (p
= 0, q
= 0; p
< n
; p
++) {
257 if (isgraph((int)binstr
[p
])) {
260 if (q
&& d
[q
- 1] != ' ')