2 13b2bc37 2022-10-23 stsp * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
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>
25 13b2bc37 2022-10-23 stsp #include "log.h"
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;
32 13b2bc37 2022-10-23 stsp log_init(int n_debug, int facility)
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());
39 13b2bc37 2022-10-23 stsp openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
45 13b2bc37 2022-10-23 stsp log_procinit(const char *procname)
47 13b2bc37 2022-10-23 stsp if (procname != NULL)
48 13b2bc37 2022-10-23 stsp log_procname = procname;
52 13b2bc37 2022-10-23 stsp log_setverbose(int v)
54 13b2bc37 2022-10-23 stsp verbose = v;
58 13b2bc37 2022-10-23 stsp log_getverbose(void)
60 13b2bc37 2022-10-23 stsp return (verbose);
64 13b2bc37 2022-10-23 stsp logit(int pri, const char *fmt, ...)
68 13b2bc37 2022-10-23 stsp va_start(ap, fmt);
69 13b2bc37 2022-10-23 stsp vlog(pri, fmt, ap);
74 13b2bc37 2022-10-23 stsp vlog(int pri, const char *fmt, va_list ap)
77 13b2bc37 2022-10-23 stsp int saved_errno = errno;
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");
85 13b2bc37 2022-10-23 stsp vfprintf(stderr, nfmt, ap);
88 13b2bc37 2022-10-23 stsp fflush(stderr);
90 13b2bc37 2022-10-23 stsp vsyslog(pri, fmt, ap);
92 13b2bc37 2022-10-23 stsp errno = saved_errno;
96 13b2bc37 2022-10-23 stsp log_warn(const char *emsg, ...)
100 13b2bc37 2022-10-23 stsp int saved_errno = errno;
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));
106 13b2bc37 2022-10-23 stsp va_start(ap, emsg);
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));
114 13b2bc37 2022-10-23 stsp vlog(LOG_CRIT, nfmt, ap);
115 13b2bc37 2022-10-23 stsp free(nfmt);
117 13b2bc37 2022-10-23 stsp va_end(ap);
120 13b2bc37 2022-10-23 stsp errno = saved_errno;
124 13b2bc37 2022-10-23 stsp log_warnx(const char *emsg, ...)
126 13b2bc37 2022-10-23 stsp va_list ap;
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);
134 13b2bc37 2022-10-23 stsp log_info(const char *emsg, ...)
136 13b2bc37 2022-10-23 stsp va_list ap;
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);
144 13b2bc37 2022-10-23 stsp log_debug(const char *emsg, ...)
146 13b2bc37 2022-10-23 stsp va_list ap;
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);
155 13b2bc37 2022-10-23 stsp static void
156 13b2bc37 2022-10-23 stsp vfatalc(int code, const char *emsg, va_list ap)
158 13b2bc37 2022-10-23 stsp static char s[BUFSIZ];
159 13b2bc37 2022-10-23 stsp const char *sep;
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 = ": ";
165 13b2bc37 2022-10-23 stsp s[0] = '\0';
169 ba4dfaf7 2023-02-07 mark logit(LOG_CRIT, "%s%s%s", s, sep, strerror(code));
171 ba4dfaf7 2023-02-07 mark logit(LOG_CRIT, "%s", s);
175 13b2bc37 2022-10-23 stsp fatal(const char *emsg, ...)
177 13b2bc37 2022-10-23 stsp va_list ap;
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);
186 13b2bc37 2022-10-23 stsp fatalx(const char *emsg, ...)
188 13b2bc37 2022-10-23 stsp va_list ap;
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);