Blame


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