1 /* $OpenBSD: log.c,v 1.1 2018/07/10 16:39:54 florian Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 static const char *log_procname;
36 log_init(int n_debug, int facility)
40 log_procinit(getprogname());
43 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
49 log_procinit(const char *procname)
52 log_procname = procname;
68 logit(int pri, const char *fmt, ...)
78 vlog(int pri, const char *fmt, va_list ap)
81 int saved_errno = errno;
84 /* best effort in out of mem situations */
85 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
86 vfprintf(stderr, fmt, ap);
87 fprintf(stderr, "\n");
89 vfprintf(stderr, nfmt, ap);
94 vsyslog(pri, fmt, ap);
100 log_warn(const char *emsg, ...)
104 int saved_errno = errno;
106 /* best effort to even work in out of memory situations */
108 logit(LOG_ERR, "%s", strerror(saved_errno));
112 if (asprintf(&nfmt, "%s: %s", emsg,
113 strerror(saved_errno)) == -1) {
115 vlog(LOG_ERR, emsg, ap);
116 logit(LOG_ERR, "%s", strerror(saved_errno));
118 vlog(LOG_ERR, nfmt, ap);
128 log_warnx(const char *emsg, ...)
133 vlog(LOG_ERR, emsg, ap);
138 log_info(const char *emsg, ...)
143 vlog(LOG_INFO, emsg, ap);
148 log_debug(const char *emsg, ...)
154 vlog(LOG_DEBUG, emsg, ap);
160 vfatalc(int code, const char *emsg, va_list ap)
162 static char s[BUFSIZ];
166 (void)vsnprintf(s, sizeof(s), emsg, ap);
173 logit(LOG_CRIT, "fatal in %s: %s%s%s",
174 log_procname, s, sep, strerror(code));
176 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
180 fatal(const char *emsg, ...)
185 vfatalc(errno, emsg, ap);
191 fatalx(const char *emsg, ...)
196 vfatalc(0, emsg, ap);