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