1 /* $OpenBSD: log.c,v 1.20 2017/03/21 12:06:56 bluhm 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.
29 const char *log_procname;
31 void log_init(int, int);
32 void log_procinit(const char *);
33 void log_setverbose(int);
34 int log_getverbose(void);
35 void log_warn(const char *, ...)
36 __attribute__((__format__ (printf, 1, 2)));
37 void log_warnx(const char *, ...)
38 __attribute__((__format__ (printf, 1, 2)));
39 void log_info(const char *, ...)
40 __attribute__((__format__ (printf, 1, 2)));
41 void log_debug(const char *, ...)
42 __attribute__((__format__ (printf, 1, 2)));
43 void logit(int, const char *, ...)
44 __attribute__((__format__ (printf, 2, 3)));
45 void vlog(int, const char *, va_list)
46 __attribute__((__format__ (printf, 2, 0)));
47 __dead void fatal(const char *, ...)
48 __attribute__((__format__ (printf, 1, 2)));
49 __dead void fatalx(const char *, ...)
50 __attribute__((__format__ (printf, 1, 2)));
53 log_init(int n_debug, int facility)
55 extern char *__progname;
59 log_procinit(__progname);
62 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
68 log_procinit(const char *procname)
71 log_procname = procname;
87 logit(int pri, const char *fmt, ...)
97 vlog(int pri, const char *fmt, va_list ap)
100 int saved_errno = errno;
103 /* best effort in out of mem situations */
104 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
105 vfprintf(stderr, fmt, ap);
106 fprintf(stderr, "\n");
108 vfprintf(stderr, nfmt, ap);
113 vsyslog(pri, fmt, ap);
119 log_warn(const char *emsg, ...)
123 int saved_errno = errno;
125 /* best effort to even work in out of memory situations */
127 logit(LOG_ERR, "%s", strerror(saved_errno));
131 if (asprintf(&nfmt, "%s: %s", emsg,
132 strerror(saved_errno)) == -1) {
134 vlog(LOG_ERR, emsg, ap);
135 logit(LOG_ERR, "%s", strerror(saved_errno));
137 vlog(LOG_ERR, nfmt, ap);
147 log_warnx(const char *emsg, ...)
152 vlog(LOG_ERR, emsg, ap);
157 log_info(const char *emsg, ...)
162 vlog(LOG_INFO, emsg, ap);
167 log_debug(const char *emsg, ...)
173 vlog(LOG_DEBUG, emsg, ap);
179 vfatalc(int code, const char *emsg, va_list ap)
181 static char s[BUFSIZ];
185 (void)vsnprintf(s, sizeof(s), emsg, ap);
192 logit(LOG_CRIT, "%s: %s%s%s",
193 log_procname, s, sep, strerror(code));
195 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
199 fatal(const char *emsg, ...)
204 vfatalc(errno, emsg, ap);
210 fatalx(const char *emsg, ...)
215 vfatalc(0, emsg, ap);