Blob


1 /* $OpenBSD: log.c,v 1.1 2018/07/10 16:39:54 florian Exp $ */
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 */
19 #include "config.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <syslog.h>
26 #include <errno.h>
27 #include <time.h>
29 #include "log.h"
31 static int debug;
32 static int verbose;
33 static const char *log_procname;
35 void
36 log_init(int n_debug, int facility)
37 {
38 debug = n_debug;
39 verbose = n_debug;
40 log_procinit(getprogname());
42 if (!debug)
43 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
45 tzset();
46 }
48 void
49 log_procinit(const char *procname)
50 {
51 if (procname != NULL)
52 log_procname = procname;
53 }
55 void
56 log_setverbose(int v)
57 {
58 verbose = v;
59 }
61 int
62 log_getverbose(void)
63 {
64 return (verbose);
65 }
67 void
68 logit(int pri, const char *fmt, ...)
69 {
70 va_list ap;
72 va_start(ap, fmt);
73 vlog(pri, fmt, ap);
74 va_end(ap);
75 }
77 void
78 vlog(int pri, const char *fmt, va_list ap)
79 {
80 char *nfmt;
81 int saved_errno = errno;
83 if (debug) {
84 /* best effort in out of mem situations */
85 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
86 vfprintf(stderr, fmt, ap);
87 fprintf(stderr, "\n");
88 } else {
89 vfprintf(stderr, nfmt, ap);
90 free(nfmt);
91 }
92 fflush(stderr);
93 } else
94 vsyslog(pri, fmt, ap);
96 errno = saved_errno;
97 }
99 void
100 log_warn(const char *emsg, ...)
102 char *nfmt;
103 va_list ap;
104 int saved_errno = errno;
106 /* best effort to even work in out of memory situations */
107 if (emsg == NULL)
108 logit(LOG_ERR, "%s", strerror(saved_errno));
109 else {
110 va_start(ap, emsg);
112 if (asprintf(&nfmt, "%s: %s", emsg,
113 strerror(saved_errno)) == -1) {
114 /* we tried it... */
115 vlog(LOG_ERR, emsg, ap);
116 logit(LOG_ERR, "%s", strerror(saved_errno));
117 } else {
118 vlog(LOG_ERR, nfmt, ap);
119 free(nfmt);
121 va_end(ap);
124 errno = saved_errno;
127 void
128 log_warnx(const char *emsg, ...)
130 va_list ap;
132 va_start(ap, emsg);
133 vlog(LOG_ERR, emsg, ap);
134 va_end(ap);
137 void
138 log_info(const char *emsg, ...)
140 va_list ap;
142 va_start(ap, emsg);
143 vlog(LOG_INFO, emsg, ap);
144 va_end(ap);
147 void
148 log_debug(const char *emsg, ...)
150 va_list ap;
152 if (verbose) {
153 va_start(ap, emsg);
154 vlog(LOG_DEBUG, emsg, ap);
155 va_end(ap);
159 static void
160 vfatalc(int code, const char *emsg, va_list ap)
162 static char s[BUFSIZ];
163 const char *sep;
165 if (emsg != NULL) {
166 (void)vsnprintf(s, sizeof(s), emsg, ap);
167 sep = ": ";
168 } else {
169 s[0] = '\0';
170 sep = "";
172 if (code)
173 logit(LOG_CRIT, "fatal in %s: %s%s%s",
174 log_procname, s, sep, strerror(code));
175 else
176 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
179 void
180 fatal(const char *emsg, ...)
182 va_list ap;
184 va_start(ap, emsg);
185 vfatalc(errno, emsg, ap);
186 va_end(ap);
187 exit(1);
190 void
191 fatalx(const char *emsg, ...)
193 va_list ap;
195 va_start(ap, emsg);
196 vfatalc(0, emsg, ap);
197 va_end(ap);
198 exit(1);