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