2 3efd8e31 2022-10-23 thomas * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <stdio.h>
18 3efd8e31 2022-10-23 thomas #include <stdlib.h>
19 3efd8e31 2022-10-23 thomas #include <stdarg.h>
20 3efd8e31 2022-10-23 thomas #include <string.h>
21 3efd8e31 2022-10-23 thomas #include <syslog.h>
22 3efd8e31 2022-10-23 thomas #include <errno.h>
23 3efd8e31 2022-10-23 thomas #include <time.h>
25 3efd8e31 2022-10-23 thomas #include "log.h"
27 3efd8e31 2022-10-23 thomas static int debug;
28 3efd8e31 2022-10-23 thomas static int verbose;
29 3efd8e31 2022-10-23 thomas const char *log_procname;
32 3efd8e31 2022-10-23 thomas log_init(int n_debug, int facility)
34 3efd8e31 2022-10-23 thomas debug = n_debug;
35 3efd8e31 2022-10-23 thomas verbose = n_debug;
36 3efd8e31 2022-10-23 thomas log_procinit(getprogname());
38 3efd8e31 2022-10-23 thomas if (!debug)
39 3efd8e31 2022-10-23 thomas openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
45 3efd8e31 2022-10-23 thomas log_procinit(const char *procname)
47 3efd8e31 2022-10-23 thomas if (procname != NULL)
48 3efd8e31 2022-10-23 thomas log_procname = procname;
52 3efd8e31 2022-10-23 thomas log_setverbose(int v)
54 3efd8e31 2022-10-23 thomas verbose = v;
58 3efd8e31 2022-10-23 thomas log_getverbose(void)
60 3efd8e31 2022-10-23 thomas return (verbose);
64 3efd8e31 2022-10-23 thomas logit(int pri, const char *fmt, ...)
66 3efd8e31 2022-10-23 thomas va_list ap;
68 3efd8e31 2022-10-23 thomas va_start(ap, fmt);
69 3efd8e31 2022-10-23 thomas vlog(pri, fmt, ap);
70 3efd8e31 2022-10-23 thomas va_end(ap);
74 3efd8e31 2022-10-23 thomas vlog(int pri, const char *fmt, va_list ap)
76 3efd8e31 2022-10-23 thomas char *nfmt;
77 3efd8e31 2022-10-23 thomas int saved_errno = errno;
79 3efd8e31 2022-10-23 thomas if (debug) {
80 3efd8e31 2022-10-23 thomas /* best effort in out of mem situations */
81 e1560d23 2023-01-08 thomas if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
82 3efd8e31 2022-10-23 thomas vfprintf(stderr, fmt, ap);
83 3efd8e31 2022-10-23 thomas fprintf(stderr, "\n");
85 3efd8e31 2022-10-23 thomas vfprintf(stderr, nfmt, ap);
86 3efd8e31 2022-10-23 thomas free(nfmt);
88 3efd8e31 2022-10-23 thomas fflush(stderr);
90 3efd8e31 2022-10-23 thomas vsyslog(pri, fmt, ap);
92 3efd8e31 2022-10-23 thomas errno = saved_errno;
96 3efd8e31 2022-10-23 thomas log_warn(const char *emsg, ...)
98 3efd8e31 2022-10-23 thomas char *nfmt;
99 3efd8e31 2022-10-23 thomas va_list ap;
100 3efd8e31 2022-10-23 thomas int saved_errno = errno;
102 3efd8e31 2022-10-23 thomas /* best effort to even work in out of memory situations */
103 3efd8e31 2022-10-23 thomas if (emsg == NULL)
104 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s", strerror(saved_errno));
106 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
108 3efd8e31 2022-10-23 thomas if (asprintf(&nfmt, "%s: %s", emsg,
109 3efd8e31 2022-10-23 thomas strerror(saved_errno)) == -1) {
110 3efd8e31 2022-10-23 thomas /* we tried it... */
111 3efd8e31 2022-10-23 thomas vlog(LOG_CRIT, emsg, ap);
112 3efd8e31 2022-10-23 thomas logit(LOG_CRIT, "%s", strerror(saved_errno));
114 3efd8e31 2022-10-23 thomas vlog(LOG_CRIT, nfmt, ap);
115 3efd8e31 2022-10-23 thomas free(nfmt);
117 3efd8e31 2022-10-23 thomas va_end(ap);
120 3efd8e31 2022-10-23 thomas errno = saved_errno;
124 3efd8e31 2022-10-23 thomas log_warnx(const char *emsg, ...)
126 3efd8e31 2022-10-23 thomas va_list ap;
128 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
129 3efd8e31 2022-10-23 thomas vlog(LOG_CRIT, emsg, ap);
130 3efd8e31 2022-10-23 thomas va_end(ap);
134 3efd8e31 2022-10-23 thomas log_info(const char *emsg, ...)
136 3efd8e31 2022-10-23 thomas va_list ap;
138 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
139 3efd8e31 2022-10-23 thomas vlog(LOG_INFO, emsg, ap);
140 3efd8e31 2022-10-23 thomas va_end(ap);
144 3efd8e31 2022-10-23 thomas log_debug(const char *emsg, ...)
146 3efd8e31 2022-10-23 thomas va_list ap;
148 3efd8e31 2022-10-23 thomas if (verbose) {
149 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
150 3efd8e31 2022-10-23 thomas vlog(LOG_DEBUG, emsg, ap);
151 3efd8e31 2022-10-23 thomas va_end(ap);
155 3efd8e31 2022-10-23 thomas static void
156 3efd8e31 2022-10-23 thomas vfatalc(int code, const char *emsg, va_list ap)
158 3efd8e31 2022-10-23 thomas static char s[BUFSIZ];
159 3efd8e31 2022-10-23 thomas const char *sep;
161 3efd8e31 2022-10-23 thomas if (emsg != NULL) {
162 3efd8e31 2022-10-23 thomas (void)vsnprintf(s, sizeof(s), emsg, ap);
163 3efd8e31 2022-10-23 thomas sep = ": ";
165 3efd8e31 2022-10-23 thomas s[0] = '\0';
166 3efd8e31 2022-10-23 thomas sep = "";
168 3efd8e31 2022-10-23 thomas if (code)
169 a6d87ea2 2023-02-07 thomas logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
171 a6d87ea2 2023-02-07 thomas logit(LOG_CRIT, "%s", s);
175 3efd8e31 2022-10-23 thomas fatal(const char *emsg, ...)
177 3efd8e31 2022-10-23 thomas va_list ap;
179 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
180 3efd8e31 2022-10-23 thomas vfatalc(errno, emsg, ap);
181 3efd8e31 2022-10-23 thomas va_end(ap);
186 3efd8e31 2022-10-23 thomas fatalx(const char *emsg, ...)
188 3efd8e31 2022-10-23 thomas va_list ap;
190 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
191 3efd8e31 2022-10-23 thomas vfatalc(0, emsg, ap);
192 3efd8e31 2022-10-23 thomas va_end(ap);