Blame


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