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)
38 extern char *__progname;
42 log_procinit(__progname);
45 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
51 log_procinit(const char *procname)
54 log_procname = procname;
70 logit(int pri, const char *fmt, ...)
80 vlog(int pri, const char *fmt, va_list ap)
83 int saved_errno = errno;
86 /* best effort in out of mem situations */
87 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
88 vfprintf(stderr, fmt, ap);
89 fprintf(stderr, "\n");
91 vfprintf(stderr, nfmt, ap);
96 vsyslog(pri, fmt, ap);
102 log_warn(const char *emsg, ...)
106 int saved_errno = errno;
108 /* best effort to even work in out of memory situations */
110 logit(LOG_ERR, "%s", strerror(saved_errno));
114 if (asprintf(&nfmt, "%s: %s", emsg,
115 strerror(saved_errno)) == -1) {
117 vlog(LOG_ERR, emsg, ap);
118 logit(LOG_ERR, "%s", strerror(saved_errno));
120 vlog(LOG_ERR, nfmt, ap);
130 log_warnx(const char *emsg, ...)
135 vlog(LOG_ERR, emsg, ap);
140 log_info(const char *emsg, ...)
145 vlog(LOG_INFO, emsg, ap);
150 log_debug(const char *emsg, ...)
156 vlog(LOG_DEBUG, emsg, ap);
162 vfatalc(int code, const char *emsg, va_list ap)
164 static char s[BUFSIZ];
168 (void)vsnprintf(s, sizeof(s), emsg, ap);
175 logit(LOG_CRIT, "fatal in %s: %s%s%s",
176 log_procname, s, sep, strerror(code));
178 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
182 fatal(const char *emsg, ...)
187 vfatalc(errno, emsg, ap);
193 fatalx(const char *emsg, ...)
198 vfatalc(0, emsg, ap);