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 "got_compat.h"
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>
27 static int debug;
28 static int verbose;
29 const char *log_procname;
31 void log_init(int, int);
32 void log_procinit(const char *);
33 void log_setverbose(int);
34 int log_getverbose(void);
35 void log_warn(const char *, ...)
36 __attribute__((__format__ (printf, 1, 2)));
37 void log_warnx(const char *, ...)
38 __attribute__((__format__ (printf, 1, 2)));
39 void log_info(const char *, ...)
40 __attribute__((__format__ (printf, 1, 2)));
41 void log_debug(const char *, ...)
42 __attribute__((__format__ (printf, 1, 2)));
43 void logit(int, const char *, ...)
44 __attribute__((__format__ (printf, 2, 3)));
45 void vlog(int, const char *, va_list)
46 __attribute__((__format__ (printf, 2, 0)));
47 __dead void fatal(const char *, ...)
48 __attribute__((__format__ (printf, 1, 2)));
49 __dead void fatalx(const char *, ...)
50 __attribute__((__format__ (printf, 1, 2)));
52 void
53 log_init(int n_debug, int facility)
54 {
55 debug = n_debug;
56 verbose = n_debug;
57 log_procinit(getprogname());
59 if (!debug)
60 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
62 tzset();
63 }
65 void
66 log_procinit(const char *procname)
67 {
68 if (procname != NULL)
69 log_procname = procname;
70 }
72 void
73 log_setverbose(int v)
74 {
75 verbose = v;
76 }
78 int
79 log_getverbose(void)
80 {
81 return (verbose);
82 }
84 void
85 logit(int pri, const char *fmt, ...)
86 {
87 va_list ap;
89 va_start(ap, fmt);
90 vlog(pri, fmt, ap);
91 va_end(ap);
92 }
94 void
95 vlog(int pri, const char *fmt, va_list ap)
96 {
97 char *nfmt;
98 int saved_errno = errno;
100 if (debug) {
101 /* best effort in out of mem situations */
102 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
103 vfprintf(stderr, fmt, ap);
104 fprintf(stderr, "\n");
105 } else {
106 vfprintf(stderr, nfmt, ap);
107 free(nfmt);
109 fflush(stderr);
110 } else
111 vsyslog(pri, fmt, ap);
113 errno = saved_errno;
116 void
117 log_warn(const char *emsg, ...)
119 char *nfmt;
120 va_list ap;
121 int saved_errno = errno;
123 /* best effort to even work in out of memory situations */
124 if (emsg == NULL)
125 logit(LOG_CRIT, "%s", strerror(saved_errno));
126 else {
127 va_start(ap, emsg);
129 if (asprintf(&nfmt, "%s: %s", emsg,
130 strerror(saved_errno)) == -1) {
131 /* we tried it... */
132 vlog(LOG_CRIT, emsg, ap);
133 logit(LOG_CRIT, "%s", strerror(saved_errno));
134 } else {
135 vlog(LOG_CRIT, nfmt, ap);
136 free(nfmt);
138 va_end(ap);
141 errno = saved_errno;
144 void
145 log_warnx(const char *emsg, ...)
147 va_list ap;
149 va_start(ap, emsg);
150 vlog(LOG_CRIT, emsg, ap);
151 va_end(ap);
154 void
155 log_info(const char *emsg, ...)
157 va_list ap;
159 va_start(ap, emsg);
160 vlog(LOG_INFO, emsg, ap);
161 va_end(ap);
164 void
165 log_debug(const char *emsg, ...)
167 va_list ap;
169 if (verbose) {
170 va_start(ap, emsg);
171 vlog(LOG_DEBUG, emsg, ap);
172 va_end(ap);
176 static void
177 vfatalc(int code, const char *emsg, va_list ap)
179 static char s[BUFSIZ];
180 const char *sep;
182 if (emsg != NULL) {
183 (void)vsnprintf(s, sizeof(s), emsg, ap);
184 sep = ": ";
185 } else {
186 s[0] = '\0';
187 sep = "";
189 if (code)
190 logit(LOG_CRIT, "%s: %s%s%s",
191 log_procname, s, sep, strerror(code));
192 else
193 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
196 void
197 fatal(const char *emsg, ...)
199 va_list ap;
201 va_start(ap, emsg);
202 vfatalc(errno, emsg, ap);
203 va_end(ap);
204 exit(1);
207 void
208 fatalx(const char *emsg, ...)
210 va_list ap;
212 va_start(ap, emsg);
213 vfatalc(0, emsg, ap);
214 va_end(ap);
215 exit(1);