2 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 static const char *log_procname;
32 log_init(int n_debug, int facility)
36 log_procinit(getprogname());
39 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
45 log_procinit(const char *procname)
48 log_procname = procname;
64 logit(int pri, const char *fmt, ...)
74 vlog(int pri, const char *fmt, va_list ap)
77 int saved_errno = errno;
80 /* best effort in out of mem situations */
81 if (asprintf(&nfmt, "%s: %s\n", log_procname, fmt) == -1) {
82 fprintf(stderr, "%s: ", log_procname);
83 vfprintf(stderr, fmt, ap);
84 fprintf(stderr, "\n");
86 vfprintf(stderr, nfmt, ap);
91 vsyslog(pri, fmt, ap);
97 log_warn(const char *emsg, ...)
101 int saved_errno = errno;
103 /* best effort to even work in out of memory situations */
105 logit(LOG_ERR, "%s", strerror(saved_errno));
109 if (asprintf(&nfmt, "%s: %s", emsg,
110 strerror(saved_errno)) == -1) {
112 vlog(LOG_ERR, emsg, ap);
113 logit(LOG_ERR, "%s", strerror(saved_errno));
115 vlog(LOG_ERR, nfmt, ap);
125 log_warnx(const char *emsg, ...)
130 vlog(LOG_ERR, emsg, ap);
135 log_info(const char *emsg, ...)
140 vlog(LOG_INFO, emsg, ap);
145 log_debug(const char *emsg, ...)
151 vlog(LOG_DEBUG, emsg, ap);
157 vfatalc(int code, const char *emsg, va_list ap)
159 static char s[BUFSIZ];
163 (void)vsnprintf(s, sizeof(s), emsg, ap);
170 logit(LOG_CRIT, "fatal in %s: %s%s%s",
171 log_procname, s, sep, strerror(code));
173 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
177 fatal(const char *emsg, ...)
182 vfatalc(errno, emsg, ap);
188 fatalx(const char *emsg, ...)
193 vfatalc(0, emsg, ap);