xref: /openbsd/sbin/iked/log.c (revision d4fdf7ed)
1 /*	$OpenBSD: log.c,v 1.6 2015/11/21 13:46:29 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <syslog.h>
24 #include <errno.h>
25 #include <time.h>
26 
27 int	 debug;
28 int	 verbose;
29 
30 void	log_init(int);
31 void	log_verbose(int);
32 void	log_warn(const char *, ...)
33 	    __attribute__((__format__ (printf, 1, 2)));
34 void	log_warnx(const char *, ...)
35 	    __attribute__((__format__ (printf, 1, 2)));
36 void	log_info(const char *, ...)
37 	    __attribute__((__format__ (printf, 1, 2)));
38 void	log_debug(const char *, ...)
39 	    __attribute__((__format__ (printf, 1, 2)));
40 void	logit(int, const char *, ...)
41 	    __attribute__((__format__ (printf, 2, 3)));
42 void	vlog(int, const char *, va_list)
43 	    __attribute__((__format__ (printf, 2, 0)));
44 __dead void fatal(const char *);
45 __dead void fatalx(const char *);
46 
47 void
48 log_init(int n_debug)
49 {
50 	extern char	*__progname;
51 
52 	debug = n_debug;
53 	verbose = n_debug;
54 
55 	if (!debug)
56 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
57 
58 	tzset();
59 }
60 
61 void
62 log_verbose(int v)
63 {
64 	verbose = v;
65 }
66 
67 void
68 logit(int pri, const char *fmt, ...)
69 {
70 	va_list	ap;
71 
72 	va_start(ap, fmt);
73 	vlog(pri, fmt, ap);
74 	va_end(ap);
75 }
76 
77 void
78 vlog(int pri, const char *fmt, va_list ap)
79 {
80 	char	*nfmt;
81 
82 	if (debug) {
83 		/* best effort in out of mem situations */
84 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
85 			vfprintf(stderr, fmt, ap);
86 			fprintf(stderr, "\n");
87 		} else {
88 			vfprintf(stderr, nfmt, ap);
89 			free(nfmt);
90 		}
91 		fflush(stderr);
92 	} else
93 		vsyslog(pri, fmt, ap);
94 }
95 
96 
97 void
98 log_warn(const char *emsg, ...)
99 {
100 	char	*nfmt;
101 	va_list	 ap;
102 
103 	/* best effort to even work in out of memory situations */
104 	if (emsg == NULL)
105 		logit(LOG_CRIT, "%s", strerror(errno));
106 	else {
107 		va_start(ap, emsg);
108 
109 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
110 			/* we tried it... */
111 			vlog(LOG_CRIT, emsg, ap);
112 			logit(LOG_CRIT, "%s", strerror(errno));
113 		} else {
114 			vlog(LOG_CRIT, nfmt, ap);
115 			free(nfmt);
116 		}
117 		va_end(ap);
118 	}
119 }
120 
121 void
122 log_warnx(const char *emsg, ...)
123 {
124 	va_list	 ap;
125 
126 	va_start(ap, emsg);
127 	vlog(LOG_CRIT, emsg, ap);
128 	va_end(ap);
129 }
130 
131 void
132 log_info(const char *emsg, ...)
133 {
134 	va_list	 ap;
135 
136 	va_start(ap, emsg);
137 	vlog(LOG_INFO, emsg, ap);
138 	va_end(ap);
139 }
140 
141 void
142 log_debug(const char *emsg, ...)
143 {
144 	va_list	 ap;
145 
146 	if (verbose > 1) {
147 		va_start(ap, emsg);
148 		vlog(LOG_DEBUG, emsg, ap);
149 		va_end(ap);
150 	}
151 }
152 
153 void
154 fatal(const char *emsg)
155 {
156 	if (emsg == NULL)
157 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
158 	else
159 		if (errno)
160 			logit(LOG_CRIT, "fatal: %s: %s",
161 			    emsg, strerror(errno));
162 		else
163 			logit(LOG_CRIT, "fatal: %s", emsg);
164 
165 	exit(1);
166 }
167 
168 void
169 fatalx(const char *emsg)
170 {
171 	errno = 0;
172 	fatal(emsg);
173 }
174