1 |
|
|
/* $OpenBSD: log.c,v 1.1 2017/06/03 10:00:29 florian Exp $ */ |
2 |
|
|
|
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 |
|
|
*/ |
18 |
|
|
|
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> |
26 |
|
|
|
27 |
|
|
#include "log.h" |
28 |
|
|
|
29 |
|
|
static int debug; |
30 |
|
|
static int verbose; |
31 |
|
|
static const char *log_procname; |
32 |
|
|
|
33 |
|
|
void |
34 |
|
|
log_init(int n_debug, int facility) |
35 |
|
|
{ |
36 |
|
|
extern char *__progname; |
37 |
|
|
|
38 |
|
36 |
debug = n_debug; |
39 |
|
18 |
verbose = n_debug; |
40 |
|
18 |
log_procinit(__progname); |
41 |
|
|
|
42 |
✓✓ |
18 |
if (!debug) |
43 |
|
9 |
openlog(__progname, LOG_PID | LOG_NDELAY, facility); |
44 |
|
|
|
45 |
|
18 |
tzset(); |
46 |
|
18 |
} |
47 |
|
|
|
48 |
|
|
void |
49 |
|
|
log_procinit(const char *procname) |
50 |
|
|
{ |
51 |
✓✗ |
54 |
if (procname != NULL) |
52 |
|
27 |
log_procname = procname; |
53 |
|
27 |
} |
54 |
|
|
|
55 |
|
|
void |
56 |
|
|
log_setverbose(int v) |
57 |
|
|
{ |
58 |
|
36 |
verbose = v; |
59 |
|
18 |
} |
60 |
|
|
|
61 |
|
|
int |
62 |
|
|
log_getverbose(void) |
63 |
|
|
{ |
64 |
|
|
return (verbose); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
void |
68 |
|
|
logit(int pri, const char *fmt, ...) |
69 |
|
|
{ |
70 |
|
|
va_list ap; |
71 |
|
|
|
72 |
|
|
va_start(ap, fmt); |
73 |
|
|
vlog(pri, fmt, ap); |
74 |
|
|
va_end(ap); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
void |
78 |
|
|
vlog(int pri, const char *fmt, va_list ap) |
79 |
|
|
{ |
80 |
|
36 |
char *nfmt; |
81 |
|
18 |
int saved_errno = errno; |
82 |
|
|
|
83 |
✗✓ |
18 |
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 |
|
18 |
vsyslog(pri, fmt, ap); |
95 |
|
|
|
96 |
|
18 |
errno = saved_errno; |
97 |
|
18 |
} |
98 |
|
|
|
99 |
|
|
void |
100 |
|
|
log_warn(const char *emsg, ...) |
101 |
|
|
{ |
102 |
|
|
char *nfmt; |
103 |
|
|
va_list ap; |
104 |
|
|
int saved_errno = errno; |
105 |
|
|
|
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); |
111 |
|
|
|
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); |
120 |
|
|
} |
121 |
|
|
va_end(ap); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
errno = saved_errno; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
void |
128 |
|
|
log_warnx(const char *emsg, ...) |
129 |
|
|
{ |
130 |
|
|
va_list ap; |
131 |
|
|
|
132 |
|
|
va_start(ap, emsg); |
133 |
|
|
vlog(LOG_ERR, emsg, ap); |
134 |
|
|
va_end(ap); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
void |
138 |
|
|
log_info(const char *emsg, ...) |
139 |
|
|
{ |
140 |
|
36 |
va_list ap; |
141 |
|
|
|
142 |
|
18 |
va_start(ap, emsg); |
143 |
|
18 |
vlog(LOG_INFO, emsg, ap); |
144 |
|
18 |
va_end(ap); |
145 |
|
18 |
} |
146 |
|
|
|
147 |
|
|
void |
148 |
|
|
log_debug(const char *emsg, ...) |
149 |
|
|
{ |
150 |
|
18 |
va_list ap; |
151 |
|
|
|
152 |
✗✓ |
9 |
if (verbose) { |
153 |
|
|
va_start(ap, emsg); |
154 |
|
|
vlog(LOG_DEBUG, emsg, ap); |
155 |
|
|
va_end(ap); |
156 |
|
|
} |
157 |
|
9 |
} |
158 |
|
|
|
159 |
|
|
static void |
160 |
|
|
vfatalc(int code, const char *emsg, va_list ap) |
161 |
|
|
{ |
162 |
|
|
static char s[BUFSIZ]; |
163 |
|
|
const char *sep; |
164 |
|
|
|
165 |
|
|
if (emsg != NULL) { |
166 |
|
|
(void)vsnprintf(s, sizeof(s), emsg, ap); |
167 |
|
|
sep = ": "; |
168 |
|
|
} else { |
169 |
|
|
s[0] = '\0'; |
170 |
|
|
sep = ""; |
171 |
|
|
} |
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); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
void |
180 |
|
|
fatal(const char *emsg, ...) |
181 |
|
|
{ |
182 |
|
|
va_list ap; |
183 |
|
|
|
184 |
|
|
va_start(ap, emsg); |
185 |
|
|
vfatalc(errno, emsg, ap); |
186 |
|
|
va_end(ap); |
187 |
|
|
exit(1); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
void |
191 |
|
|
fatalx(const char *emsg, ...) |
192 |
|
|
{ |
193 |
|
|
va_list ap; |
194 |
|
|
|
195 |
|
|
va_start(ap, emsg); |
196 |
|
|
vfatalc(0, emsg, ap); |
197 |
|
|
va_end(ap); |
198 |
|
|
exit(1); |
199 |
|
|
} |