call unveil earlier in 'got import'
[got-portable.git] / gotd / log.c
bloba6d1b73d3885dd6d04013a9bd11b8b18a750a0c3
1 /*
2 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
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 #include "log.h"
29 static int debug;
30 static int verbose;
31 const char *log_procname;
33 void
34 log_init(int n_debug, int facility)
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();
46 void
47 log_procinit(const char *procname)
49 if (procname != NULL)
50 log_procname = procname;
53 void
54 log_setverbose(int v)
56 verbose = v;
59 int
60 log_getverbose(void)
62 return (verbose);
65 void
66 logit(int pri, const char *fmt, ...)
68 va_list ap;
70 va_start(ap, fmt);
71 vlog(pri, fmt, ap);
72 va_end(ap);
75 void
76 vlog(int pri, const char *fmt, va_list ap)
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 vfprintf(stderr, fmt, ap);
85 fprintf(stderr, "\n");
86 } else {
87 vfprintf(stderr, nfmt, ap);
88 free(nfmt);
90 fflush(stderr);
91 } else
92 vsyslog(pri, fmt, ap);
94 errno = saved_errno;
97 void
98 log_warn(const char *emsg, ...)
100 char *nfmt;
101 va_list ap;
102 int saved_errno = errno;
104 /* best effort to even work in out of memory situations */
105 if (emsg == NULL)
106 logit(LOG_CRIT, "%s", strerror(saved_errno));
107 else {
108 va_start(ap, emsg);
110 if (asprintf(&nfmt, "%s: %s", emsg,
111 strerror(saved_errno)) == -1) {
112 /* we tried it... */
113 vlog(LOG_CRIT, emsg, ap);
114 logit(LOG_CRIT, "%s", strerror(saved_errno));
115 } else {
116 vlog(LOG_CRIT, nfmt, ap);
117 free(nfmt);
119 va_end(ap);
122 errno = saved_errno;
125 void
126 log_warnx(const char *emsg, ...)
128 va_list ap;
130 va_start(ap, emsg);
131 vlog(LOG_CRIT, emsg, ap);
132 va_end(ap);
135 void
136 log_info(const char *emsg, ...)
138 va_list ap;
140 va_start(ap, emsg);
141 vlog(LOG_INFO, emsg, ap);
142 va_end(ap);
145 void
146 log_debug(const char *emsg, ...)
148 va_list ap;
150 if (verbose) {
151 va_start(ap, emsg);
152 vlog(LOG_DEBUG, emsg, ap);
153 va_end(ap);
157 static void
158 vfatalc(int code, const char *emsg, va_list ap)
160 static char s[BUFSIZ];
161 const char *sep;
163 if (emsg != NULL) {
164 (void)vsnprintf(s, sizeof(s), emsg, ap);
165 sep = ": ";
166 } else {
167 s[0] = '\0';
168 sep = "";
170 if (code)
171 logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
172 else
173 logit(LOG_CRIT, "%s", 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);