2 8a35f56c 2022-07-16 thomas * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
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>
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;
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)));
53 8a35f56c 2022-07-16 thomas log_init(int n_debug, int facility)
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());
59 8a35f56c 2022-07-16 thomas if (!debug)
60 8a35f56c 2022-07-16 thomas openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
66 8a35f56c 2022-07-16 thomas log_procinit(const char *procname)
68 8a35f56c 2022-07-16 thomas if (procname != NULL)
69 8a35f56c 2022-07-16 thomas log_procname = procname;
73 8a35f56c 2022-07-16 thomas log_setverbose(int v)
75 8a35f56c 2022-07-16 thomas verbose = v;
79 8a35f56c 2022-07-16 thomas log_getverbose(void)
81 8a35f56c 2022-07-16 thomas return (verbose);
85 8a35f56c 2022-07-16 thomas logit(int pri, const char *fmt, ...)
87 8a35f56c 2022-07-16 thomas va_list ap;
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);
95 8a35f56c 2022-07-16 thomas vlog(int pri, const char *fmt, va_list ap)
97 8a35f56c 2022-07-16 thomas char *nfmt;
98 8a35f56c 2022-07-16 thomas int saved_errno = errno;
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");
106 8a35f56c 2022-07-16 thomas vfprintf(stderr, nfmt, ap);
107 8a35f56c 2022-07-16 thomas free(nfmt);
109 8a35f56c 2022-07-16 thomas fflush(stderr);
111 8a35f56c 2022-07-16 thomas vsyslog(pri, fmt, ap);
113 8a35f56c 2022-07-16 thomas errno = saved_errno;
117 8a35f56c 2022-07-16 thomas log_warn(const char *emsg, ...)
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;
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));
127 8a35f56c 2022-07-16 thomas va_start(ap, emsg);
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));
135 8a35f56c 2022-07-16 thomas vlog(LOG_CRIT, nfmt, ap);
136 8a35f56c 2022-07-16 thomas free(nfmt);
138 8a35f56c 2022-07-16 thomas va_end(ap);
141 8a35f56c 2022-07-16 thomas errno = saved_errno;
145 8a35f56c 2022-07-16 thomas log_warnx(const char *emsg, ...)
147 8a35f56c 2022-07-16 thomas va_list ap;
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);
155 8a35f56c 2022-07-16 thomas log_info(const char *emsg, ...)
157 8a35f56c 2022-07-16 thomas va_list ap;
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);
165 8a35f56c 2022-07-16 thomas log_debug(const char *emsg, ...)
167 8a35f56c 2022-07-16 thomas va_list ap;
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);
176 8a35f56c 2022-07-16 thomas static void
177 8a35f56c 2022-07-16 thomas vfatalc(int code, const char *emsg, va_list ap)
179 8a35f56c 2022-07-16 thomas static char s[BUFSIZ];
180 8a35f56c 2022-07-16 thomas const char *sep;
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 = ": ";
186 8a35f56c 2022-07-16 thomas s[0] = '\0';
187 8a35f56c 2022-07-16 thomas sep = "";
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));
193 8a35f56c 2022-07-16 thomas logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
197 8a35f56c 2022-07-16 thomas fatal(const char *emsg, ...)
199 8a35f56c 2022-07-16 thomas va_list ap;
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);
208 8a35f56c 2022-07-16 thomas fatalx(const char *emsg, ...)
210 8a35f56c 2022-07-16 thomas va_list ap;
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);