Blame


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