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