1 /* $OpenBSD: log.c,v 1.17 2023/12/21 12:43:31 martijn 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 <sys/types.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <syslog.h>
26 #include <errno.h>
27 #include <time.h>
28
29 static int debug;
30 static int verbose;
31 const char *log_procname;
32
33 void log_init(int, int);
34 void log_procinit(const char *);
35 void log_setverbose(int);
36 int log_getverbose(void);
37 void log_warn(const char *, ...)
38 __attribute__((__format__ (printf, 1, 2)));
39 void log_warnx(const char *, ...)
40 __attribute__((__format__ (printf, 1, 2)));
41 void log_info(const char *, ...)
42 __attribute__((__format__ (printf, 1, 2)));
43 void log_debug(const char *, ...)
44 __attribute__((__format__ (printf, 1, 2)));
45 void logit(int, const char *, ...)
46 __attribute__((__format__ (printf, 2, 3)));
47 void vlog(int, const char *, va_list)
48 __attribute__((__format__ (printf, 2, 0)));
49 __dead void fatal(const char *, ...)
50 __attribute__((__format__ (printf, 1, 2)));
51 __dead void fatalx(const char *, ...)
52 __attribute__((__format__ (printf, 1, 2)));
53
54 void
log_init(int n_debug,int facility)55 log_init(int n_debug, int facility)
56 {
57 extern char *__progname;
58
59 debug = n_debug;
60 verbose = n_debug;
61 log_procinit(__progname);
62
63 if (!debug)
64 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
65
66 tzset();
67 }
68
69 void
log_procinit(const char * procname)70 log_procinit(const char *procname)
71 {
72 if (procname != NULL)
73 log_procname = procname;
74 }
75
76 void
log_setverbose(int v)77 log_setverbose(int v)
78 {
79 verbose = v;
80 }
81
82 int
log_getverbose(void)83 log_getverbose(void)
84 {
85 return (verbose);
86 }
87
88 void
logit(int pri,const char * fmt,...)89 logit(int pri, const char *fmt, ...)
90 {
91 va_list ap;
92
93 va_start(ap, fmt);
94 vlog(pri, fmt, ap);
95 va_end(ap);
96 }
97
98 void
vlog(int pri,const char * fmt,va_list ap)99 vlog(int pri, const char *fmt, va_list ap)
100 {
101 char *nfmt;
102 int saved_errno = errno;
103
104 if (debug) {
105 /* best effort in out of mem situations */
106 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
107 vfprintf(stderr, fmt, ap);
108 fprintf(stderr, "\n");
109 } else {
110 vfprintf(stderr, nfmt, ap);
111 free(nfmt);
112 }
113 fflush(stderr);
114 } else
115 vsyslog(pri, fmt, ap);
116
117 errno = saved_errno;
118 }
119
120 void
log_warn(const char * emsg,...)121 log_warn(const char *emsg, ...)
122 {
123 char *nfmt;
124 va_list ap;
125 int saved_errno = errno;
126
127 /* best effort to even work in out of memory situations */
128 if (emsg == NULL)
129 logit(LOG_ERR, "%s", strerror(saved_errno));
130 else {
131 va_start(ap, emsg);
132
133 if (asprintf(&nfmt, "%s: %s", emsg,
134 strerror(saved_errno)) == -1) {
135 /* we tried it... */
136 vlog(LOG_ERR, emsg, ap);
137 logit(LOG_ERR, "%s", strerror(saved_errno));
138 } else {
139 vlog(LOG_ERR, nfmt, ap);
140 free(nfmt);
141 }
142 va_end(ap);
143 }
144
145 errno = saved_errno;
146 }
147
148 void
log_warnx(const char * emsg,...)149 log_warnx(const char *emsg, ...)
150 {
151 va_list ap;
152
153 va_start(ap, emsg);
154 vlog(LOG_ERR, emsg, ap);
155 va_end(ap);
156 }
157
158 void
log_info(const char * emsg,...)159 log_info(const char *emsg, ...)
160 {
161 va_list ap;
162
163 va_start(ap, emsg);
164 vlog(LOG_INFO, emsg, ap);
165 va_end(ap);
166 }
167
168 void
log_debug(const char * emsg,...)169 log_debug(const char *emsg, ...)
170 {
171 va_list ap;
172
173 if (verbose > 1) {
174 va_start(ap, emsg);
175 vlog(LOG_DEBUG, emsg, ap);
176 va_end(ap);
177 }
178 }
179
180 static void
vfatalc(int code,const char * emsg,va_list ap)181 vfatalc(int code, const char *emsg, va_list ap)
182 {
183 static char s[BUFSIZ];
184 const char *sep;
185
186 if (emsg != NULL) {
187 (void)vsnprintf(s, sizeof(s), emsg, ap);
188 sep = ": ";
189 } else {
190 s[0] = '\0';
191 sep = "";
192 }
193 if (code)
194 logit(LOG_CRIT, "%s: %s%s%s",
195 log_procname, s, sep, strerror(code));
196 else
197 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
198 }
199
200 void
fatal(const char * emsg,...)201 fatal(const char *emsg, ...)
202 {
203 va_list ap;
204
205 va_start(ap, emsg);
206 vfatalc(errno, emsg, ap);
207 va_end(ap);
208 exit(1);
209 }
210
211 void
fatalx(const char * emsg,...)212 fatalx(const char *emsg, ...)
213 {
214 va_list ap;
215
216 va_start(ap, emsg);
217 vfatalc(0, emsg, ap);
218 va_end(ap);
219 exit(1);
220 }
221