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 #include "log.h"
27 static int debug;
28 static int verbose;
29 static const char *log_procname;
31 void
32 log_init(int n_debug, int facility)
33 {
34 debug = n_debug;
35 verbose = n_debug;
36 log_procinit(getprogname());
38 if (!debug)
39 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
41 tzset();
42 }
44 void
45 log_procinit(const char *procname)
46 {
47 if (procname != NULL)
48 log_procname = procname;
49 }
51 void
52 log_setverbose(int v)
53 {
54 verbose = v;
55 }
57 int
58 log_getverbose(void)
59 {
60 return (verbose);
61 }
63 void
64 logit(int pri, const char *fmt, ...)
65 {
66 va_list ap;
68 va_start(ap, fmt);
69 vlog(pri, fmt, ap);
70 va_end(ap);
71 }
73 void
74 vlog(int pri, const char *fmt, va_list ap)
75 {
76 char *nfmt;
77 int saved_errno = errno;
79 if (debug) {
80 /* best effort in out of mem situations */
81 if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
82 fprintf(stderr, "%s: ", log_procname);
83 vfprintf(stderr, fmt, ap);
84 fprintf(stderr, "\n");
85 } else {
86 vfprintf(stderr, nfmt, ap);
87 free(nfmt);
88 }
89 fflush(stderr);
90 } else
91 vsyslog(pri, fmt, ap);
93 errno = saved_errno;
94 }
96 void
97 log_warn(const char *emsg, ...)
98 {
99 char *nfmt;
100 va_list ap;
101 int saved_errno = errno;
103 /* best effort to even work in out of memory situations */
104 if (emsg == NULL)
105 logit(LOG_ERR, "%s", strerror(saved_errno));
106 else {
107 va_start(ap, emsg);
109 if (asprintf(&nfmt, "%s: %s", emsg,
110 strerror(saved_errno)) == -1) {
111 /* we tried it... */
112 vlog(LOG_ERR, emsg, ap);
113 logit(LOG_ERR, "%s", strerror(saved_errno));
114 } else {
115 vlog(LOG_ERR, nfmt, ap);
116 free(nfmt);
118 va_end(ap);
121 errno = saved_errno;
124 void
125 log_warnx(const char *emsg, ...)
127 va_list ap;
129 va_start(ap, emsg);
130 vlog(LOG_ERR, emsg, ap);
131 va_end(ap);
134 void
135 log_info(const char *emsg, ...)
137 va_list ap;
139 va_start(ap, emsg);
140 vlog(LOG_INFO, emsg, ap);
141 va_end(ap);
144 void
145 log_debug(const char *emsg, ...)
147 va_list ap;
149 if (verbose) {
150 va_start(ap, emsg);
151 vlog(LOG_DEBUG, emsg, ap);
152 va_end(ap);
156 static void
157 vfatalc(int code, const char *emsg, va_list ap)
159 static char s[BUFSIZ];
160 const char *sep;
162 if (emsg != NULL) {
163 (void)vsnprintf(s, sizeof(s), emsg, ap);
164 sep = ": ";
165 } else {
166 s[0] = '\0';
167 sep = "";
169 if (code)
170 logit(LOG_CRIT, "fatal in %s: %s%s%s",
171 log_procname, s, sep, strerror(code));
172 else
173 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
176 void
177 fatal(const char *emsg, ...)
179 va_list ap;
181 va_start(ap, emsg);
182 vfatalc(errno, emsg, ap);
183 va_end(ap);
184 exit(1);
187 void
188 fatalx(const char *emsg, ...)
190 va_list ap;
192 va_start(ap, emsg);
193 vfatalc(0, emsg, ap);
194 va_end(ap);
195 exit(1);