2 a596b957 2022-07-14 tracey * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
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>
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;
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)));
51 a596b957 2022-07-14 tracey log_init(int n_debug, int facility)
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());
57 a596b957 2022-07-14 tracey if (!debug)
58 a596b957 2022-07-14 tracey openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
64 a596b957 2022-07-14 tracey log_procinit(const char *procname)
66 a596b957 2022-07-14 tracey if (procname != NULL)
67 a596b957 2022-07-14 tracey log_procname = procname;
71 a596b957 2022-07-14 tracey log_setverbose(int v)
73 a596b957 2022-07-14 tracey verbose = v;
77 a596b957 2022-07-14 tracey log_getverbose(void)
79 a596b957 2022-07-14 tracey return (verbose);
83 a596b957 2022-07-14 tracey logit(int pri, const char *fmt, ...)
85 a596b957 2022-07-14 tracey va_list ap;
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);
93 a596b957 2022-07-14 tracey vlog(int pri, const char *fmt, va_list ap)
95 a596b957 2022-07-14 tracey char *nfmt;
96 a596b957 2022-07-14 tracey int saved_errno = errno;
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");
104 a596b957 2022-07-14 tracey vfprintf(stderr, nfmt, ap);
105 a596b957 2022-07-14 tracey free(nfmt);
107 a596b957 2022-07-14 tracey fflush(stderr);
109 a596b957 2022-07-14 tracey vsyslog(pri, fmt, ap);
111 a596b957 2022-07-14 tracey errno = saved_errno;
115 a596b957 2022-07-14 tracey log_warn(const char *emsg, ...)
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;
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));
125 a596b957 2022-07-14 tracey va_start(ap, emsg);
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));
133 a596b957 2022-07-14 tracey vlog(LOG_CRIT, nfmt, ap);
134 a596b957 2022-07-14 tracey free(nfmt);
136 a596b957 2022-07-14 tracey va_end(ap);
139 a596b957 2022-07-14 tracey errno = saved_errno;
143 a596b957 2022-07-14 tracey log_warnx(const char *emsg, ...)
145 a596b957 2022-07-14 tracey va_list ap;
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);
153 a596b957 2022-07-14 tracey log_info(const char *emsg, ...)
155 a596b957 2022-07-14 tracey va_list ap;
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);
163 a596b957 2022-07-14 tracey log_debug(const char *emsg, ...)
165 a596b957 2022-07-14 tracey va_list ap;
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);
174 a596b957 2022-07-14 tracey static void
175 a596b957 2022-07-14 tracey vfatalc(int code, const char *emsg, va_list ap)
177 a596b957 2022-07-14 tracey static char s[BUFSIZ];
178 a596b957 2022-07-14 tracey const char *sep;
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 = ": ";
184 a596b957 2022-07-14 tracey s[0] = '\0';
185 a596b957 2022-07-14 tracey sep = "";
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));
191 a596b957 2022-07-14 tracey logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
195 a596b957 2022-07-14 tracey fatal(const char *emsg, ...)
197 a596b957 2022-07-14 tracey va_list ap;
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);
206 a596b957 2022-07-14 tracey fatalx(const char *emsg, ...)
208 a596b957 2022-07-14 tracey va_list ap;
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);