Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
7 13b2bc37 2022-10-23 stsp *
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
17 13b2bc37 2022-10-23 stsp #include <stdio.h>
18 13b2bc37 2022-10-23 stsp #include <stdlib.h>
19 13b2bc37 2022-10-23 stsp #include <stdarg.h>
20 13b2bc37 2022-10-23 stsp #include <string.h>
21 13b2bc37 2022-10-23 stsp #include <syslog.h>
22 13b2bc37 2022-10-23 stsp #include <errno.h>
23 13b2bc37 2022-10-23 stsp #include <time.h>
24 13b2bc37 2022-10-23 stsp
25 13b2bc37 2022-10-23 stsp #include "log.h"
26 13b2bc37 2022-10-23 stsp
27 13b2bc37 2022-10-23 stsp static int debug;
28 13b2bc37 2022-10-23 stsp static int verbose;
29 13b2bc37 2022-10-23 stsp const char *log_procname;
30 13b2bc37 2022-10-23 stsp
31 13b2bc37 2022-10-23 stsp void
32 13b2bc37 2022-10-23 stsp log_init(int n_debug, int facility)
33 13b2bc37 2022-10-23 stsp {
34 13b2bc37 2022-10-23 stsp debug = n_debug;
35 13b2bc37 2022-10-23 stsp verbose = n_debug;
36 13b2bc37 2022-10-23 stsp log_procinit(getprogname());
37 13b2bc37 2022-10-23 stsp
38 13b2bc37 2022-10-23 stsp if (!debug)
39 13b2bc37 2022-10-23 stsp openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
40 13b2bc37 2022-10-23 stsp
41 13b2bc37 2022-10-23 stsp tzset();
42 13b2bc37 2022-10-23 stsp }
43 13b2bc37 2022-10-23 stsp
44 13b2bc37 2022-10-23 stsp void
45 13b2bc37 2022-10-23 stsp log_procinit(const char *procname)
46 13b2bc37 2022-10-23 stsp {
47 13b2bc37 2022-10-23 stsp if (procname != NULL)
48 13b2bc37 2022-10-23 stsp log_procname = procname;
49 13b2bc37 2022-10-23 stsp }
50 13b2bc37 2022-10-23 stsp
51 13b2bc37 2022-10-23 stsp void
52 13b2bc37 2022-10-23 stsp log_setverbose(int v)
53 13b2bc37 2022-10-23 stsp {
54 13b2bc37 2022-10-23 stsp verbose = v;
55 13b2bc37 2022-10-23 stsp }
56 13b2bc37 2022-10-23 stsp
57 13b2bc37 2022-10-23 stsp int
58 13b2bc37 2022-10-23 stsp log_getverbose(void)
59 13b2bc37 2022-10-23 stsp {
60 13b2bc37 2022-10-23 stsp return (verbose);
61 13b2bc37 2022-10-23 stsp }
62 13b2bc37 2022-10-23 stsp
63 13b2bc37 2022-10-23 stsp void
64 13b2bc37 2022-10-23 stsp logit(int pri, const char *fmt, ...)
65 13b2bc37 2022-10-23 stsp {
66 13b2bc37 2022-10-23 stsp va_list ap;
67 13b2bc37 2022-10-23 stsp
68 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
69 13b2bc37 2022-10-23 stsp vlog(pri, fmt, ap);
70 13b2bc37 2022-10-23 stsp va_end(ap);
71 13b2bc37 2022-10-23 stsp }
72 13b2bc37 2022-10-23 stsp
73 13b2bc37 2022-10-23 stsp void
74 13b2bc37 2022-10-23 stsp vlog(int pri, const char *fmt, va_list ap)
75 13b2bc37 2022-10-23 stsp {
76 13b2bc37 2022-10-23 stsp char *nfmt;
77 13b2bc37 2022-10-23 stsp int saved_errno = errno;
78 13b2bc37 2022-10-23 stsp
79 13b2bc37 2022-10-23 stsp if (debug) {
80 13b2bc37 2022-10-23 stsp /* best effort in out of mem situations */
81 6b5e6124 2023-01-08 stsp if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
82 13b2bc37 2022-10-23 stsp vfprintf(stderr, fmt, ap);
83 13b2bc37 2022-10-23 stsp fprintf(stderr, "\n");
84 13b2bc37 2022-10-23 stsp } else {
85 13b2bc37 2022-10-23 stsp vfprintf(stderr, nfmt, ap);
86 13b2bc37 2022-10-23 stsp free(nfmt);
87 13b2bc37 2022-10-23 stsp }
88 13b2bc37 2022-10-23 stsp fflush(stderr);
89 13b2bc37 2022-10-23 stsp } else
90 13b2bc37 2022-10-23 stsp vsyslog(pri, fmt, ap);
91 13b2bc37 2022-10-23 stsp
92 13b2bc37 2022-10-23 stsp errno = saved_errno;
93 13b2bc37 2022-10-23 stsp }
94 13b2bc37 2022-10-23 stsp
95 13b2bc37 2022-10-23 stsp void
96 13b2bc37 2022-10-23 stsp log_warn(const char *emsg, ...)
97 13b2bc37 2022-10-23 stsp {
98 13b2bc37 2022-10-23 stsp char *nfmt;
99 13b2bc37 2022-10-23 stsp va_list ap;
100 13b2bc37 2022-10-23 stsp int saved_errno = errno;
101 13b2bc37 2022-10-23 stsp
102 13b2bc37 2022-10-23 stsp /* best effort to even work in out of memory situations */
103 13b2bc37 2022-10-23 stsp if (emsg == NULL)
104 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s", strerror(saved_errno));
105 13b2bc37 2022-10-23 stsp else {
106 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
107 13b2bc37 2022-10-23 stsp
108 13b2bc37 2022-10-23 stsp if (asprintf(&nfmt, "%s: %s", emsg,
109 13b2bc37 2022-10-23 stsp strerror(saved_errno)) == -1) {
110 13b2bc37 2022-10-23 stsp /* we tried it... */
111 13b2bc37 2022-10-23 stsp vlog(LOG_CRIT, emsg, ap);
112 13b2bc37 2022-10-23 stsp logit(LOG_CRIT, "%s", strerror(saved_errno));
113 13b2bc37 2022-10-23 stsp } else {
114 13b2bc37 2022-10-23 stsp vlog(LOG_CRIT, nfmt, ap);
115 13b2bc37 2022-10-23 stsp free(nfmt);
116 13b2bc37 2022-10-23 stsp }
117 13b2bc37 2022-10-23 stsp va_end(ap);
118 13b2bc37 2022-10-23 stsp }
119 13b2bc37 2022-10-23 stsp
120 13b2bc37 2022-10-23 stsp errno = saved_errno;
121 13b2bc37 2022-10-23 stsp }
122 13b2bc37 2022-10-23 stsp
123 13b2bc37 2022-10-23 stsp void
124 13b2bc37 2022-10-23 stsp log_warnx(const char *emsg, ...)
125 13b2bc37 2022-10-23 stsp {
126 13b2bc37 2022-10-23 stsp va_list ap;
127 13b2bc37 2022-10-23 stsp
128 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
129 13b2bc37 2022-10-23 stsp vlog(LOG_CRIT, emsg, ap);
130 13b2bc37 2022-10-23 stsp va_end(ap);
131 13b2bc37 2022-10-23 stsp }
132 13b2bc37 2022-10-23 stsp
133 13b2bc37 2022-10-23 stsp void
134 13b2bc37 2022-10-23 stsp log_info(const char *emsg, ...)
135 13b2bc37 2022-10-23 stsp {
136 13b2bc37 2022-10-23 stsp va_list ap;
137 13b2bc37 2022-10-23 stsp
138 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
139 13b2bc37 2022-10-23 stsp vlog(LOG_INFO, emsg, ap);
140 13b2bc37 2022-10-23 stsp va_end(ap);
141 13b2bc37 2022-10-23 stsp }
142 13b2bc37 2022-10-23 stsp
143 13b2bc37 2022-10-23 stsp void
144 13b2bc37 2022-10-23 stsp log_debug(const char *emsg, ...)
145 13b2bc37 2022-10-23 stsp {
146 13b2bc37 2022-10-23 stsp va_list ap;
147 13b2bc37 2022-10-23 stsp
148 13b2bc37 2022-10-23 stsp if (verbose) {
149 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
150 13b2bc37 2022-10-23 stsp vlog(LOG_DEBUG, emsg, ap);
151 13b2bc37 2022-10-23 stsp va_end(ap);
152 13b2bc37 2022-10-23 stsp }
153 13b2bc37 2022-10-23 stsp }
154 13b2bc37 2022-10-23 stsp
155 13b2bc37 2022-10-23 stsp static void
156 13b2bc37 2022-10-23 stsp vfatalc(int code, const char *emsg, va_list ap)
157 13b2bc37 2022-10-23 stsp {
158 13b2bc37 2022-10-23 stsp static char s[BUFSIZ];
159 13b2bc37 2022-10-23 stsp const char *sep;
160 13b2bc37 2022-10-23 stsp
161 13b2bc37 2022-10-23 stsp if (emsg != NULL) {
162 13b2bc37 2022-10-23 stsp (void)vsnprintf(s, sizeof(s), emsg, ap);
163 13b2bc37 2022-10-23 stsp sep = ": ";
164 13b2bc37 2022-10-23 stsp } else {
165 13b2bc37 2022-10-23 stsp s[0] = '\0';
166 13b2bc37 2022-10-23 stsp sep = "";
167 13b2bc37 2022-10-23 stsp }
168 13b2bc37 2022-10-23 stsp if (code)
169 ba4dfaf7 2023-02-07 mark logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
170 13b2bc37 2022-10-23 stsp else
171 ba4dfaf7 2023-02-07 mark logit(LOG_CRIT, "%s", s);
172 13b2bc37 2022-10-23 stsp }
173 13b2bc37 2022-10-23 stsp
174 13b2bc37 2022-10-23 stsp void
175 13b2bc37 2022-10-23 stsp fatal(const char *emsg, ...)
176 13b2bc37 2022-10-23 stsp {
177 13b2bc37 2022-10-23 stsp va_list ap;
178 13b2bc37 2022-10-23 stsp
179 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
180 13b2bc37 2022-10-23 stsp vfatalc(errno, emsg, ap);
181 13b2bc37 2022-10-23 stsp va_end(ap);
182 13b2bc37 2022-10-23 stsp exit(1);
183 13b2bc37 2022-10-23 stsp }
184 13b2bc37 2022-10-23 stsp
185 13b2bc37 2022-10-23 stsp void
186 13b2bc37 2022-10-23 stsp fatalx(const char *emsg, ...)
187 13b2bc37 2022-10-23 stsp {
188 13b2bc37 2022-10-23 stsp va_list ap;
189 13b2bc37 2022-10-23 stsp
190 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
191 13b2bc37 2022-10-23 stsp vfatalc(0, emsg, ap);
192 13b2bc37 2022-10-23 stsp va_end(ap);
193 13b2bc37 2022-10-23 stsp exit(1);
194 13b2bc37 2022-10-23 stsp }