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 0237feaa 2021-11-25 op #include <stdio.h>
18 0237feaa 2021-11-25 op #include <stdlib.h>
19 0237feaa 2021-11-25 op #include <stdarg.h>
20 0237feaa 2021-11-25 op #include <string.h>
21 0237feaa 2021-11-25 op #include <syslog.h>
22 0237feaa 2021-11-25 op #include <errno.h>
23 0237feaa 2021-11-25 op #include <time.h>
25 0237feaa 2021-11-25 op #include "log.h"
27 0237feaa 2021-11-25 op static int debug;
28 0237feaa 2021-11-25 op static int verbose;
29 0237feaa 2021-11-25 op static const char *log_procname;
32 0237feaa 2021-11-25 op log_init(int n_debug, int facility)
34 0237feaa 2021-11-25 op debug = n_debug;
35 0237feaa 2021-11-25 op verbose = n_debug;
36 0237feaa 2021-11-25 op log_procinit(getprogname());
39 0237feaa 2021-11-25 op openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
45 0237feaa 2021-11-25 op log_procinit(const char *procname)
47 0237feaa 2021-11-25 op if (procname != NULL)
48 0237feaa 2021-11-25 op log_procname = procname;
52 0237feaa 2021-11-25 op log_setverbose(int v)
58 0237feaa 2021-11-25 op log_getverbose(void)
60 0237feaa 2021-11-25 op return (verbose);
64 0237feaa 2021-11-25 op logit(int pri, const char *fmt, ...)
68 0237feaa 2021-11-25 op va_start(ap, fmt);
69 0237feaa 2021-11-25 op vlog(pri, fmt, ap);
74 0237feaa 2021-11-25 op vlog(int pri, const char *fmt, va_list ap)
77 0237feaa 2021-11-25 op int saved_errno = errno;
80 0237feaa 2021-11-25 op /* best effort in out of mem situations */
81 0237feaa 2021-11-25 op if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
82 0237feaa 2021-11-25 op fprintf(stderr, "%s: ", log_procname);
83 0237feaa 2021-11-25 op vfprintf(stderr, fmt, ap);
84 0237feaa 2021-11-25 op fprintf(stderr, "\n");
86 0237feaa 2021-11-25 op vfprintf(stderr, nfmt, ap);
89 0237feaa 2021-11-25 op fflush(stderr);
91 0237feaa 2021-11-25 op vsyslog(pri, fmt, ap);
93 0237feaa 2021-11-25 op errno = saved_errno;
97 0237feaa 2021-11-25 op log_warn(const char *emsg, ...)
101 0237feaa 2021-11-25 op int saved_errno = errno;
103 0237feaa 2021-11-25 op /* best effort to even work in out of memory situations */
104 0237feaa 2021-11-25 op if (emsg == NULL)
105 0237feaa 2021-11-25 op logit(LOG_ERR, "%s", strerror(saved_errno));
107 0237feaa 2021-11-25 op va_start(ap, emsg);
109 0237feaa 2021-11-25 op if (asprintf(&nfmt, "%s: %s", emsg,
110 0237feaa 2021-11-25 op strerror(saved_errno)) == -1) {
111 0237feaa 2021-11-25 op /* we tried it... */
112 0237feaa 2021-11-25 op vlog(LOG_ERR, emsg, ap);
113 0237feaa 2021-11-25 op logit(LOG_ERR, "%s", strerror(saved_errno));
115 0237feaa 2021-11-25 op vlog(LOG_ERR, nfmt, ap);
121 0237feaa 2021-11-25 op errno = saved_errno;
125 0237feaa 2021-11-25 op log_warnx(const char *emsg, ...)
129 0237feaa 2021-11-25 op va_start(ap, emsg);
130 0237feaa 2021-11-25 op vlog(LOG_ERR, emsg, ap);
135 0237feaa 2021-11-25 op log_info(const char *emsg, ...)
139 0237feaa 2021-11-25 op va_start(ap, emsg);
140 0237feaa 2021-11-25 op vlog(LOG_INFO, emsg, ap);
145 0237feaa 2021-11-25 op log_debug(const char *emsg, ...)
149 0237feaa 2021-11-25 op if (verbose) {
150 0237feaa 2021-11-25 op va_start(ap, emsg);
151 0237feaa 2021-11-25 op vlog(LOG_DEBUG, emsg, ap);
157 0237feaa 2021-11-25 op vfatalc(int code, const char *emsg, va_list ap)
159 0237feaa 2021-11-25 op static char s[BUFSIZ];
160 0237feaa 2021-11-25 op const char *sep;
162 0237feaa 2021-11-25 op if (emsg != NULL) {
163 0237feaa 2021-11-25 op (void)vsnprintf(s, sizeof(s), emsg, ap);
170 0237feaa 2021-11-25 op logit(LOG_CRIT, "fatal in %s: %s%s%s",
171 0237feaa 2021-11-25 op log_procname, s, sep, strerror(code));
173 0237feaa 2021-11-25 op logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
177 0237feaa 2021-11-25 op fatal(const char *emsg, ...)
181 0237feaa 2021-11-25 op va_start(ap, emsg);
182 0237feaa 2021-11-25 op vfatalc(errno, emsg, ap);
188 0237feaa 2021-11-25 op fatalx(const char *emsg, ...)
192 0237feaa 2021-11-25 op va_start(ap, emsg);
193 0237feaa 2021-11-25 op vfatalc(0, emsg, ap);