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 #define _GNU_SOURCE
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 extern char *__progname;
40 debug = n_debug;
41 verbose = n_debug;
42 log_procinit(__progname);
44 if (!debug)
45 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
47 tzset();
48 }
50 void
51 log_procinit(const char *procname)
52 {
53 if (procname != NULL)
54 log_procname = procname;
55 }
57 void
58 log_setverbose(int v)
59 {
60 verbose = v;
61 }
63 int
64 log_getverbose(void)
65 {
66 return (verbose);
67 }
69 void
70 logit(int pri, const char *fmt, ...)
71 {
72 va_list ap;
74 va_start(ap, fmt);
75 vlog(pri, fmt, ap);
76 va_end(ap);
77 }
79 void
80 vlog(int pri, const char *fmt, va_list ap)
81 {
82 char *nfmt;
83 int saved_errno = errno;
85 if (debug) {
86 /* best effort in out of mem situations */
87 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
88 vfprintf(stderr, fmt, ap);
89 fprintf(stderr, "\n");
90 } else {
91 vfprintf(stderr, nfmt, ap);
92 free(nfmt);
93 }
94 fflush(stderr);
95 } else
96 vsyslog(pri, fmt, ap);
98 errno = saved_errno;
99 }
101 void
102 log_warn(const char *emsg, ...)
104 char *nfmt;
105 va_list ap;
106 int saved_errno = errno;
108 /* best effort to even work in out of memory situations */
109 if (emsg == NULL)
110 logit(LOG_ERR, "%s", strerror(saved_errno));
111 else {
112 va_start(ap, emsg);
114 if (asprintf(&nfmt, "%s: %s", emsg,
115 strerror(saved_errno)) == -1) {
116 /* we tried it... */
117 vlog(LOG_ERR, emsg, ap);
118 logit(LOG_ERR, "%s", strerror(saved_errno));
119 } else {
120 vlog(LOG_ERR, nfmt, ap);
121 free(nfmt);
123 va_end(ap);
126 errno = saved_errno;
129 void
130 log_warnx(const char *emsg, ...)
132 va_list ap;
134 va_start(ap, emsg);
135 vlog(LOG_ERR, emsg, ap);
136 va_end(ap);
139 void
140 log_info(const char *emsg, ...)
142 va_list ap;
144 va_start(ap, emsg);
145 vlog(LOG_INFO, emsg, ap);
146 va_end(ap);
149 void
150 log_debug(const char *emsg, ...)
152 va_list ap;
154 if (verbose) {
155 va_start(ap, emsg);
156 vlog(LOG_DEBUG, emsg, ap);
157 va_end(ap);
161 static void
162 vfatalc(int code, const char *emsg, va_list ap)
164 static char s[BUFSIZ];
165 const char *sep;
167 if (emsg != NULL) {
168 (void)vsnprintf(s, sizeof(s), emsg, ap);
169 sep = ": ";
170 } else {
171 s[0] = '\0';
172 sep = "";
174 if (code)
175 logit(LOG_CRIT, "fatal in %s: %s%s%s",
176 log_procname, s, sep, strerror(code));
177 else
178 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
181 void
182 fatal(const char *emsg, ...)
184 va_list ap;
186 va_start(ap, emsg);
187 vfatalc(errno, emsg, ap);
188 va_end(ap);
189 exit(1);
192 void
193 fatalx(const char *emsg, ...)
195 va_list ap;
197 va_start(ap, emsg);
198 vfatalc(0, emsg, ap);
199 va_end(ap);
200 exit(1);