Blob


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