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 "config.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 #include "log.h"
29 static int debug;
30 static int verbose;
31 static const char *log_procname;
33 void
34 log_init(int n_debug, int facility)
35 {
36 debug = n_debug;
37 verbose = n_debug;
38 log_procinit(getprogname());
40 if (!debug)
41 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
43 tzset();
44 }
46 void
47 log_procinit(const char *procname)
48 {
49 if (procname != NULL)
50 log_procname = procname;
51 }
53 void
54 log_setverbose(int v)
55 {
56 verbose = v;
57 }
59 int
60 log_getverbose(void)
61 {
62 return (verbose);
63 }
65 void
66 logit(int pri, const char *fmt, ...)
67 {
68 va_list ap;
70 va_start(ap, fmt);
71 vlog(pri, fmt, ap);
72 va_end(ap);
73 }
75 void
76 vlog(int pri, const char *fmt, va_list ap)
77 {
78 char *nfmt;
79 int saved_errno = errno;
81 if (debug) {
82 /* best effort in out of mem situations */
83 if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
84 fprintf(stderr, "%s: ", log_procname);
85 vfprintf(stderr, fmt, ap);
86 fprintf(stderr, "\n");
87 } else {
88 vfprintf(stderr, nfmt, ap);
89 free(nfmt);
90 }
91 fflush(stderr);
92 } else
93 vsyslog(pri, fmt, ap);
95 errno = saved_errno;
96 }
98 void
99 log_warn(const char *emsg, ...)
101 char *nfmt;
102 va_list ap;
103 int saved_errno = errno;
105 /* best effort to even work in out of memory situations */
106 if (emsg == NULL)
107 logit(LOG_ERR, "%s", strerror(saved_errno));
108 else {
109 va_start(ap, emsg);
111 if (asprintf(&nfmt, "%s: %s", emsg,
112 strerror(saved_errno)) == -1) {
113 /* we tried it... */
114 vlog(LOG_ERR, emsg, ap);
115 logit(LOG_ERR, "%s", strerror(saved_errno));
116 } else {
117 vlog(LOG_ERR, nfmt, ap);
118 free(nfmt);
120 va_end(ap);
123 errno = saved_errno;
126 void
127 log_warnx(const char *emsg, ...)
129 va_list ap;
131 va_start(ap, emsg);
132 vlog(LOG_ERR, emsg, ap);
133 va_end(ap);
136 void
137 log_info(const char *emsg, ...)
139 va_list ap;
141 va_start(ap, emsg);
142 vlog(LOG_INFO, emsg, ap);
143 va_end(ap);
146 void
147 log_debug(const char *emsg, ...)
149 va_list ap;
151 if (verbose) {
152 va_start(ap, emsg);
153 vlog(LOG_DEBUG, emsg, ap);
154 va_end(ap);
158 static void
159 vfatalc(int code, const char *emsg, va_list ap)
161 static char s[BUFSIZ];
162 const char *sep;
164 if (emsg != NULL) {
165 (void)vsnprintf(s, sizeof(s), emsg, ap);
166 sep = ": ";
167 } else {
168 s[0] = '\0';
169 sep = "";
171 if (code)
172 logit(LOG_CRIT, "fatal in %s: %s%s%s",
173 log_procname, s, sep, strerror(code));
174 else
175 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
178 void
179 fatal(const char *emsg, ...)
181 va_list ap;
183 va_start(ap, emsg);
184 vfatalc(errno, emsg, ap);
185 va_end(ap);
186 exit(1);
189 void
190 fatalx(const char *emsg, ...)
192 va_list ap;
194 va_start(ap, emsg);
195 vfatalc(0, emsg, ap);
196 va_end(ap);
197 exit(1);