1 3baa2617 2022-02-16 op /* $OpenBSD: log.c,v 1.1 2018/07/10 16:39:54 florian Exp $ */
4 3baa2617 2022-02-16 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
7 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
8 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
10 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 3baa2617 2022-02-16 op #include <stdio.h>
20 3baa2617 2022-02-16 op #include <stdlib.h>
21 3baa2617 2022-02-16 op #include <stdarg.h>
22 3baa2617 2022-02-16 op #include <string.h>
23 3baa2617 2022-02-16 op #include <syslog.h>
24 3baa2617 2022-02-16 op #include <errno.h>
25 3baa2617 2022-02-16 op #include <time.h>
27 3baa2617 2022-02-16 op #include "log.h"
29 3baa2617 2022-02-16 op static int debug;
30 3baa2617 2022-02-16 op static int verbose;
31 3baa2617 2022-02-16 op static const char *log_procname;
34 3baa2617 2022-02-16 op log_init(int n_debug, int facility)
36 3baa2617 2022-02-16 op extern char *__progname;
38 3baa2617 2022-02-16 op debug = n_debug;
39 3baa2617 2022-02-16 op verbose = n_debug;
40 3baa2617 2022-02-16 op log_procinit(__progname);
43 3baa2617 2022-02-16 op openlog(__progname, LOG_PID | LOG_NDELAY, facility);
49 3baa2617 2022-02-16 op log_procinit(const char *procname)
51 3baa2617 2022-02-16 op if (procname != NULL)
52 3baa2617 2022-02-16 op log_procname = procname;
56 3baa2617 2022-02-16 op log_setverbose(int v)
62 3baa2617 2022-02-16 op log_getverbose(void)
64 3baa2617 2022-02-16 op return (verbose);
68 3baa2617 2022-02-16 op logit(int pri, const char *fmt, ...)
72 3baa2617 2022-02-16 op va_start(ap, fmt);
73 3baa2617 2022-02-16 op vlog(pri, fmt, ap);
78 3baa2617 2022-02-16 op vlog(int pri, const char *fmt, va_list ap)
81 3baa2617 2022-02-16 op int saved_errno = errno;
84 3baa2617 2022-02-16 op /* best effort in out of mem situations */
85 3baa2617 2022-02-16 op if (asprintf(&nfmt, "%s\n", fmt) == -1) {
86 3baa2617 2022-02-16 op vfprintf(stderr, fmt, ap);
87 3baa2617 2022-02-16 op fprintf(stderr, "\n");
89 3baa2617 2022-02-16 op vfprintf(stderr, nfmt, ap);
92 3baa2617 2022-02-16 op fflush(stderr);
94 3baa2617 2022-02-16 op vsyslog(pri, fmt, ap);
96 3baa2617 2022-02-16 op errno = saved_errno;
100 3baa2617 2022-02-16 op log_warn(const char *emsg, ...)
104 3baa2617 2022-02-16 op int saved_errno = errno;
106 3baa2617 2022-02-16 op /* best effort to even work in out of memory situations */
107 3baa2617 2022-02-16 op if (emsg == NULL)
108 3baa2617 2022-02-16 op logit(LOG_ERR, "%s", strerror(saved_errno));
110 3baa2617 2022-02-16 op va_start(ap, emsg);
112 3baa2617 2022-02-16 op if (asprintf(&nfmt, "%s: %s", emsg,
113 3baa2617 2022-02-16 op strerror(saved_errno)) == -1) {
114 3baa2617 2022-02-16 op /* we tried it... */
115 3baa2617 2022-02-16 op vlog(LOG_ERR, emsg, ap);
116 3baa2617 2022-02-16 op logit(LOG_ERR, "%s", strerror(saved_errno));
118 3baa2617 2022-02-16 op vlog(LOG_ERR, nfmt, ap);
124 3baa2617 2022-02-16 op errno = saved_errno;
128 3baa2617 2022-02-16 op log_warnx(const char *emsg, ...)
132 3baa2617 2022-02-16 op va_start(ap, emsg);
133 3baa2617 2022-02-16 op vlog(LOG_ERR, emsg, ap);
138 3baa2617 2022-02-16 op log_info(const char *emsg, ...)
142 3baa2617 2022-02-16 op va_start(ap, emsg);
143 3baa2617 2022-02-16 op vlog(LOG_INFO, emsg, ap);
148 3baa2617 2022-02-16 op log_debug(const char *emsg, ...)
152 3baa2617 2022-02-16 op if (verbose) {
153 3baa2617 2022-02-16 op va_start(ap, emsg);
154 3baa2617 2022-02-16 op vlog(LOG_DEBUG, emsg, ap);
160 3baa2617 2022-02-16 op vfatalc(int code, const char *emsg, va_list ap)
162 3baa2617 2022-02-16 op static char s[BUFSIZ];
163 3baa2617 2022-02-16 op const char *sep;
165 3baa2617 2022-02-16 op if (emsg != NULL) {
166 3baa2617 2022-02-16 op (void)vsnprintf(s, sizeof(s), emsg, ap);
173 3baa2617 2022-02-16 op logit(LOG_CRIT, "fatal in %s: %s%s%s",
174 3baa2617 2022-02-16 op log_procname, s, sep, strerror(code));
176 3baa2617 2022-02-16 op logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
180 3baa2617 2022-02-16 op fatal(const char *emsg, ...)
184 3baa2617 2022-02-16 op va_start(ap, emsg);
185 3baa2617 2022-02-16 op vfatalc(errno, emsg, ap);
191 3baa2617 2022-02-16 op fatalx(const char *emsg, ...)
195 3baa2617 2022-02-16 op va_start(ap, emsg);
196 3baa2617 2022-02-16 op vfatalc(0, emsg, ap);