1 /* $OpenBSD: log.c,v 1.9 2023/09/26 01:53:54 dv Exp $ */
2
3 /*
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <syslog.h>
24 #include <errno.h>
25 #include <time.h>
26
27 #include "proc.h"
28
29 static int debug;
30 static int verbose;
31 static char log_procname[2048];
32
33 void
log_init(int n_debug,int facility)34 log_init(int n_debug, int facility)
35 {
36 extern char *__progname;
37
38 debug = n_debug;
39 verbose = n_debug;
40 log_procinit("%s", __progname);
41
42 if (!debug)
43 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
44
45 tzset();
46 }
47
48 void
log_procinit(const char * fmt,...)49 log_procinit(const char *fmt, ...)
50 {
51 va_list ap;
52 va_start(ap, fmt);
53 vsnprintf(log_procname, sizeof(log_procname), fmt, ap);
54 va_end(ap);
55 }
56
57 void
log_setverbose(int v)58 log_setverbose(int v)
59 {
60 verbose = v;
61 }
62
63 int
log_getverbose(void)64 log_getverbose(void)
65 {
66 return (verbose);
67 }
68
69 void
logit(int pri,const char * fmt,...)70 logit(int pri, const char *fmt, ...)
71 {
72 va_list ap;
73
74 va_start(ap, fmt);
75 vlog(pri, fmt, ap);
76 va_end(ap);
77 }
78
79 void
vlog(int pri,const char * fmt,va_list ap)80 vlog(int pri, const char *fmt, va_list ap)
81 {
82 char *nfmt;
83 int saved_errno = errno;
84
85 if (debug) {
86 /* best effort in out of mem situations */
87 if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
88 vfprintf(stderr, fmt, ap);
89 fprintf(stderr, "\n");
90 } else {
91 vfprintf(stderr, nfmt, ap);
92 free(nfmt);
93 }
94 fflush(stderr);
95 } else
96 vsyslog(pri, fmt, ap);
97
98 errno = saved_errno;
99 }
100
101 void
log_warn(const char * emsg,...)102 log_warn(const char *emsg, ...)
103 {
104 char *nfmt;
105 va_list ap;
106 int saved_errno = errno;
107
108 /* best effort to even work in out of memory situations */
109 if (emsg == NULL)
110 logit(LOG_ERR, "%s", strerror(saved_errno));
111 else {
112 va_start(ap, emsg);
113
114 if (asprintf(&nfmt, "%s: %s", emsg,
115 strerror(saved_errno)) == -1) {
116 /* we tried it... */
117 vlog(LOG_ERR, emsg, ap);
118 logit(LOG_ERR, "%s", strerror(saved_errno));
119 } else {
120 vlog(LOG_ERR, nfmt, ap);
121 free(nfmt);
122 }
123 va_end(ap);
124 }
125
126 errno = saved_errno;
127 }
128
129 void
log_warnx(const char * emsg,...)130 log_warnx(const char *emsg, ...)
131 {
132 va_list ap;
133
134 va_start(ap, emsg);
135 vlog(LOG_ERR, emsg, ap);
136 va_end(ap);
137 }
138
139 void
log_info(const char * emsg,...)140 log_info(const char *emsg, ...)
141 {
142 va_list ap;
143
144 va_start(ap, emsg);
145 vlog(LOG_INFO, emsg, ap);
146 va_end(ap);
147 }
148
149 void
log_debug(const char * emsg,...)150 log_debug(const char *emsg, ...)
151 {
152 va_list ap;
153
154 if (verbose > 1) {
155 va_start(ap, emsg);
156 vlog(LOG_DEBUG, emsg, ap);
157 va_end(ap);
158 }
159 }
160
161 static void
vfatalc(int code,const char * emsg,va_list ap)162 vfatalc(int code, const char *emsg, va_list ap)
163 {
164 static char s[BUFSIZ];
165 const char *sep;
166
167 if (emsg != NULL) {
168 (void)vsnprintf(s, sizeof(s), emsg, ap);
169 sep = ": ";
170 } else {
171 s[0] = '\0';
172 sep = "";
173 }
174 if (code)
175 logit(LOG_CRIT, "%s: %s%s%s",
176 log_procname, s, sep, strerror(code));
177 else
178 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
179 }
180
181 void
fatal(const char * emsg,...)182 fatal(const char *emsg, ...)
183 {
184 va_list ap;
185
186 va_start(ap, emsg);
187 vfatalc(errno, emsg, ap);
188 va_end(ap);
189 exit(1);
190 }
191
192 void
fatalx(const char * emsg,...)193 fatalx(const char *emsg, ...)
194 {
195 va_list ap;
196
197 va_start(ap, emsg);
198 vfatalc(0, emsg, ap);
199 va_end(ap);
200 exit(1);
201 }
202