xref: /openbsd/usr.sbin/sasyncd/log.c (revision 404b540a)
1 /*	$OpenBSD: log.c,v 1.5 2007/01/08 15:31:01 markus Exp $	*/
2 
3 /*
4  * Copyright (c) 2005 H�kan Olsson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * This code was written under funding by Multicom Security AB.
30  */
31 
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <syslog.h>
38 #include <errno.h>
39 #include <time.h>
40 
41 #include "sasyncd.h"
42 
43 static char logbuf[2048];
44 
45 void
46 log_init(char *pname)
47 {
48 	tzset();
49 	openlog(pname, LOG_CONS | LOG_PID, LOG_DAEMON);
50 }
51 
52 static void
53 log_output(char *msg)
54 {
55 	if (cfgstate.debug)
56 		fprintf(stderr, "%s\n", msg);
57 	else
58 		syslog(LOG_CRIT, "%s", msg);
59 }
60 
61 void
62 log_err(const char *fmt, ...)
63 {
64 	extern char	*__progname;
65 	int		off = 0;
66 	va_list		ap;
67 
68 	if (cfgstate.debug) {
69 		snprintf(logbuf, sizeof logbuf, "%s: ", __progname);
70 		off = strlen(logbuf);
71 	}
72 
73 	va_start(ap, fmt);
74 	(void)vsnprintf(logbuf + off, sizeof logbuf - off, fmt, ap);
75 	va_end(ap);
76 
77 	strlcat(logbuf, ": ", sizeof logbuf);
78 	strlcat(logbuf, strerror(errno), sizeof logbuf);
79 
80 	log_output(logbuf);
81 	return;
82 }
83 
84 void
85 log_msg (int minlevel, const char *fmt, ...)
86 {
87 	va_list ap;
88 
89 	if (cfgstate.verboselevel < minlevel)
90 		return;
91 
92 	va_start(ap, fmt);
93 	(void)vsnprintf(logbuf, sizeof logbuf, fmt, ap);
94 	va_end(ap);
95 
96 	log_output(logbuf);
97 	return;
98 }
99