1 /* $OpenBSD: log.c,v 1.7 2016/08/27 04:21:08 guenther 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 <sys/select.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <syslog.h>
39 #include <errno.h>
40 #include <time.h>
41
42 #include "sasyncd.h"
43
44 static char logbuf[2048];
45
46 void
log_init(char * pname)47 log_init(char *pname)
48 {
49 tzset();
50 openlog(pname, LOG_CONS | LOG_PID, LOG_DAEMON);
51 }
52
53 static void
log_output(char * msg)54 log_output(char *msg)
55 {
56 if (cfgstate.debug)
57 fprintf(stderr, "%s\n", msg);
58 else
59 syslog(LOG_CRIT, "%s", msg);
60 }
61
62 void
log_err(const char * fmt,...)63 log_err(const char *fmt, ...)
64 {
65 extern char *__progname;
66 int off = 0;
67 va_list ap;
68
69 if (cfgstate.debug) {
70 snprintf(logbuf, sizeof logbuf, "%s: ", __progname);
71 off = strlen(logbuf);
72 }
73
74 va_start(ap, fmt);
75 (void)vsnprintf(logbuf + off, sizeof logbuf - off, fmt, ap);
76 va_end(ap);
77
78 strlcat(logbuf, ": ", sizeof logbuf);
79 strlcat(logbuf, strerror(errno), sizeof logbuf);
80
81 log_output(logbuf);
82 return;
83 }
84
85 void
log_msg(int minlevel,const char * fmt,...)86 log_msg(int minlevel, const char *fmt, ...)
87 {
88 va_list ap;
89
90 if (cfgstate.verboselevel < minlevel)
91 return;
92
93 va_start(ap, fmt);
94 (void)vsnprintf(logbuf, sizeof logbuf, fmt, ap);
95 va_end(ap);
96
97 log_output(logbuf);
98 return;
99 }
100