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