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