Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
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.
7 3efd8e31 2022-10-23 thomas *
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.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
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>
24 3efd8e31 2022-10-23 thomas
25 3efd8e31 2022-10-23 thomas #include "log.h"
26 3efd8e31 2022-10-23 thomas
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;
30 3efd8e31 2022-10-23 thomas
31 3efd8e31 2022-10-23 thomas void
32 3efd8e31 2022-10-23 thomas log_init(int n_debug, int facility)
33 3efd8e31 2022-10-23 thomas {
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());
37 3efd8e31 2022-10-23 thomas
38 3efd8e31 2022-10-23 thomas if (!debug)
39 3efd8e31 2022-10-23 thomas openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
40 3efd8e31 2022-10-23 thomas
41 3efd8e31 2022-10-23 thomas tzset();
42 3efd8e31 2022-10-23 thomas }
43 3efd8e31 2022-10-23 thomas
44 3efd8e31 2022-10-23 thomas void
45 3efd8e31 2022-10-23 thomas log_procinit(const char *procname)
46 3efd8e31 2022-10-23 thomas {
47 3efd8e31 2022-10-23 thomas if (procname != NULL)
48 3efd8e31 2022-10-23 thomas log_procname = procname;
49 3efd8e31 2022-10-23 thomas }
50 3efd8e31 2022-10-23 thomas
51 3efd8e31 2022-10-23 thomas void
52 3efd8e31 2022-10-23 thomas log_setverbose(int v)
53 3efd8e31 2022-10-23 thomas {
54 3efd8e31 2022-10-23 thomas verbose = v;
55 3efd8e31 2022-10-23 thomas }
56 3efd8e31 2022-10-23 thomas
57 3efd8e31 2022-10-23 thomas int
58 3efd8e31 2022-10-23 thomas log_getverbose(void)
59 3efd8e31 2022-10-23 thomas {
60 3efd8e31 2022-10-23 thomas return (verbose);
61 3efd8e31 2022-10-23 thomas }
62 3efd8e31 2022-10-23 thomas
63 3efd8e31 2022-10-23 thomas void
64 3efd8e31 2022-10-23 thomas logit(int pri, const char *fmt, ...)
65 3efd8e31 2022-10-23 thomas {
66 3efd8e31 2022-10-23 thomas va_list ap;
67 3efd8e31 2022-10-23 thomas
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);
71 3efd8e31 2022-10-23 thomas }
72 3efd8e31 2022-10-23 thomas
73 3efd8e31 2022-10-23 thomas void
74 3efd8e31 2022-10-23 thomas vlog(int pri, const char *fmt, va_list ap)
75 3efd8e31 2022-10-23 thomas {
76 3efd8e31 2022-10-23 thomas char *nfmt;
77 3efd8e31 2022-10-23 thomas int saved_errno = errno;
78 3efd8e31 2022-10-23 thomas
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");
84 3efd8e31 2022-10-23 thomas } else {
85 3efd8e31 2022-10-23 thomas vfprintf(stderr, nfmt, ap);
86 3efd8e31 2022-10-23 thomas free(nfmt);
87 3efd8e31 2022-10-23 thomas }
88 3efd8e31 2022-10-23 thomas fflush(stderr);
89 3efd8e31 2022-10-23 thomas } else
90 3efd8e31 2022-10-23 thomas vsyslog(pri, fmt, ap);
91 3efd8e31 2022-10-23 thomas
92 3efd8e31 2022-10-23 thomas errno = saved_errno;
93 3efd8e31 2022-10-23 thomas }
94 3efd8e31 2022-10-23 thomas
95 3efd8e31 2022-10-23 thomas void
96 3efd8e31 2022-10-23 thomas log_warn(const char *emsg, ...)
97 3efd8e31 2022-10-23 thomas {
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;
101 3efd8e31 2022-10-23 thomas
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));
105 3efd8e31 2022-10-23 thomas else {
106 3efd8e31 2022-10-23 thomas va_start(ap, emsg);
107 3efd8e31 2022-10-23 thomas
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));
113 3efd8e31 2022-10-23 thomas } else {
114 3efd8e31 2022-10-23 thomas vlog(LOG_CRIT, nfmt, ap);
115 3efd8e31 2022-10-23 thomas free(nfmt);
116 3efd8e31 2022-10-23 thomas }
117 3efd8e31 2022-10-23 thomas va_end(ap);
118 3efd8e31 2022-10-23 thomas }
119 3efd8e31 2022-10-23 thomas
120 3efd8e31 2022-10-23 thomas errno = saved_errno;
121 3efd8e31 2022-10-23 thomas }
122 3efd8e31 2022-10-23 thomas
123 3efd8e31 2022-10-23 thomas void
124 3efd8e31 2022-10-23 thomas log_warnx(const char *emsg, ...)
125 3efd8e31 2022-10-23 thomas {
126 3efd8e31 2022-10-23 thomas va_list ap;
127 3efd8e31 2022-10-23 thomas
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);
131 3efd8e31 2022-10-23 thomas }
132 3efd8e31 2022-10-23 thomas
133 3efd8e31 2022-10-23 thomas void
134 3efd8e31 2022-10-23 thomas log_info(const char *emsg, ...)
135 3efd8e31 2022-10-23 thomas {
136 3efd8e31 2022-10-23 thomas va_list ap;
137 3efd8e31 2022-10-23 thomas
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);
141 3efd8e31 2022-10-23 thomas }
142 3efd8e31 2022-10-23 thomas
143 3efd8e31 2022-10-23 thomas void
144 3efd8e31 2022-10-23 thomas log_debug(const char *emsg, ...)
145 3efd8e31 2022-10-23 thomas {
146 3efd8e31 2022-10-23 thomas va_list ap;
147 3efd8e31 2022-10-23 thomas
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);
152 3efd8e31 2022-10-23 thomas }
153 3efd8e31 2022-10-23 thomas }
154 3efd8e31 2022-10-23 thomas
155 3efd8e31 2022-10-23 thomas static void
156 3efd8e31 2022-10-23 thomas vfatalc(int code, const char *emsg, va_list ap)
157 3efd8e31 2022-10-23 thomas {
158 3efd8e31 2022-10-23 thomas static char s[BUFSIZ];
159 3efd8e31 2022-10-23 thomas const char *sep;
160 3efd8e31 2022-10-23 thomas
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 = ": ";
164 3efd8e31 2022-10-23 thomas } else {
165 3efd8e31 2022-10-23 thomas s[0] = '\0';
166 3efd8e31 2022-10-23 thomas sep = "";
167 3efd8e31 2022-10-23 thomas }
168 3efd8e31 2022-10-23 thomas if (code)
169 a6d87ea2 2023-02-07 thomas logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
170 3efd8e31 2022-10-23 thomas else
171 a6d87ea2 2023-02-07 thomas logit(LOG_CRIT, "%s", s);
172 3efd8e31 2022-10-23 thomas }
173 3efd8e31 2022-10-23 thomas
174 3efd8e31 2022-10-23 thomas void
175 3efd8e31 2022-10-23 thomas fatal(const char *emsg, ...)
176 3efd8e31 2022-10-23 thomas {
177 3efd8e31 2022-10-23 thomas va_list ap;
178 3efd8e31 2022-10-23 thomas
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);
182 3efd8e31 2022-10-23 thomas exit(1);
183 3efd8e31 2022-10-23 thomas }
184 3efd8e31 2022-10-23 thomas
185 3efd8e31 2022-10-23 thomas void
186 3efd8e31 2022-10-23 thomas fatalx(const char *emsg, ...)
187 3efd8e31 2022-10-23 thomas {
188 3efd8e31 2022-10-23 thomas va_list ap;
189 3efd8e31 2022-10-23 thomas
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);
193 3efd8e31 2022-10-23 thomas exit(1);
194 3efd8e31 2022-10-23 thomas }