2 0237feaa 2021-11-25 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
4 0237feaa 2021-11-25 op * Permission to use, copy, modify, and distribute this software for any
5 0237feaa 2021-11-25 op * purpose with or without fee is hereby granted, provided that the above
6 0237feaa 2021-11-25 op * copyright notice and this permission notice appear in all copies.
8 0237feaa 2021-11-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0237feaa 2021-11-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0237feaa 2021-11-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0237feaa 2021-11-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0237feaa 2021-11-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0237feaa 2021-11-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0237feaa 2021-11-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0efd7c9a 2022-07-30 op #include "config.h"
19 0237feaa 2021-11-25 op #include <stdio.h>
20 0237feaa 2021-11-25 op #include <stdlib.h>
21 0237feaa 2021-11-25 op #include <stdarg.h>
22 0237feaa 2021-11-25 op #include <string.h>
23 0237feaa 2021-11-25 op #include <syslog.h>
24 0237feaa 2021-11-25 op #include <errno.h>
25 0237feaa 2021-11-25 op #include <time.h>
27 0237feaa 2021-11-25 op #include "log.h"
29 0237feaa 2021-11-25 op static int debug;
30 0237feaa 2021-11-25 op static int verbose;
31 0237feaa 2021-11-25 op static const char *log_procname;
34 0237feaa 2021-11-25 op log_init(int n_debug, int facility)
36 0237feaa 2021-11-25 op debug = n_debug;
37 0237feaa 2021-11-25 op verbose = n_debug;
38 0237feaa 2021-11-25 op log_procinit(getprogname());
41 0237feaa 2021-11-25 op openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
47 0237feaa 2021-11-25 op log_procinit(const char *procname)
49 0237feaa 2021-11-25 op if (procname != NULL)
50 0237feaa 2021-11-25 op log_procname = procname;
54 0237feaa 2021-11-25 op log_setverbose(int v)
60 0237feaa 2021-11-25 op log_getverbose(void)
62 0237feaa 2021-11-25 op return (verbose);
66 0237feaa 2021-11-25 op logit(int pri, const char *fmt, ...)
70 0237feaa 2021-11-25 op va_start(ap, fmt);
71 0237feaa 2021-11-25 op vlog(pri, fmt, ap);
76 0237feaa 2021-11-25 op vlog(int pri, const char *fmt, va_list ap)
79 0237feaa 2021-11-25 op int saved_errno = errno;
82 0237feaa 2021-11-25 op /* best effort in out of mem situations */
83 0237feaa 2021-11-25 op if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
84 0237feaa 2021-11-25 op fprintf(stderr, "%s: ", log_procname);
85 0237feaa 2021-11-25 op vfprintf(stderr, fmt, ap);
86 0237feaa 2021-11-25 op fprintf(stderr, "\n");
88 0237feaa 2021-11-25 op vfprintf(stderr, nfmt, ap);
91 0237feaa 2021-11-25 op fflush(stderr);
93 0237feaa 2021-11-25 op vsyslog(pri, fmt, ap);
95 0237feaa 2021-11-25 op errno = saved_errno;
99 0237feaa 2021-11-25 op log_warn(const char *emsg, ...)
103 0237feaa 2021-11-25 op int saved_errno = errno;
105 0237feaa 2021-11-25 op /* best effort to even work in out of memory situations */
106 0237feaa 2021-11-25 op if (emsg == NULL)
107 0237feaa 2021-11-25 op logit(LOG_ERR, "%s", strerror(saved_errno));
109 0237feaa 2021-11-25 op va_start(ap, emsg);
111 0237feaa 2021-11-25 op if (asprintf(&nfmt, "%s: %s", emsg,
112 0237feaa 2021-11-25 op strerror(saved_errno)) == -1) {
113 0237feaa 2021-11-25 op /* we tried it... */
114 0237feaa 2021-11-25 op vlog(LOG_ERR, emsg, ap);
115 0237feaa 2021-11-25 op logit(LOG_ERR, "%s", strerror(saved_errno));
117 0237feaa 2021-11-25 op vlog(LOG_ERR, nfmt, ap);
123 0237feaa 2021-11-25 op errno = saved_errno;
127 0237feaa 2021-11-25 op log_warnx(const char *emsg, ...)
131 0237feaa 2021-11-25 op va_start(ap, emsg);
132 0237feaa 2021-11-25 op vlog(LOG_ERR, emsg, ap);
137 0237feaa 2021-11-25 op log_info(const char *emsg, ...)
141 0237feaa 2021-11-25 op va_start(ap, emsg);
142 0237feaa 2021-11-25 op vlog(LOG_INFO, emsg, ap);
147 0237feaa 2021-11-25 op log_debug(const char *emsg, ...)
151 0237feaa 2021-11-25 op if (verbose) {
152 0237feaa 2021-11-25 op va_start(ap, emsg);
153 0237feaa 2021-11-25 op vlog(LOG_DEBUG, emsg, ap);
159 0237feaa 2021-11-25 op vfatalc(int code, const char *emsg, va_list ap)
161 0237feaa 2021-11-25 op static char s[BUFSIZ];
162 0237feaa 2021-11-25 op const char *sep;
164 0237feaa 2021-11-25 op if (emsg != NULL) {
165 0237feaa 2021-11-25 op (void)vsnprintf(s, sizeof(s), emsg, ap);
172 0237feaa 2021-11-25 op logit(LOG_CRIT, "fatal in %s: %s%s%s",
173 0237feaa 2021-11-25 op log_procname, s, sep, strerror(code));
175 0237feaa 2021-11-25 op logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
179 0237feaa 2021-11-25 op fatal(const char *emsg, ...)
183 0237feaa 2021-11-25 op va_start(ap, emsg);
184 0237feaa 2021-11-25 op vfatalc(errno, emsg, ap);
190 0237feaa 2021-11-25 op fatalx(const char *emsg, ...)
194 0237feaa 2021-11-25 op va_start(ap, emsg);
195 0237feaa 2021-11-25 op vfatalc(0, emsg, ap);