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.
27 const char *log_procname;
29 void log_init(int, int);
30 void log_procinit(const char *);
31 void log_setverbose(int);
32 int log_getverbose(void);
33 void log_warn(const char *, ...)
34 __attribute__((__format__ (printf, 1, 2)));
35 void log_warnx(const char *, ...)
36 __attribute__((__format__ (printf, 1, 2)));
37 void log_info(const char *, ...)
38 __attribute__((__format__ (printf, 1, 2)));
39 void log_debug(const char *, ...)
40 __attribute__((__format__ (printf, 1, 2)));
41 void logit(int, const char *, ...)
42 __attribute__((__format__ (printf, 2, 3)));
43 void vlog(int, const char *, va_list)
44 __attribute__((__format__ (printf, 2, 0)));
45 __dead void fatal(const char *, ...)
46 __attribute__((__format__ (printf, 1, 2)));
47 __dead void fatalx(const char *, ...)
48 __attribute__((__format__ (printf, 1, 2)));
51 log_init(int n_debug, int facility)
55 log_procinit(getprogname());
58 openlog(getprogname(), LOG_PID | LOG_NDELAY, facility);
64 log_procinit(const char *procname)
67 log_procname = procname;
83 logit(int pri, const char *fmt, ...)
93 vlog(int pri, const char *fmt, va_list ap)
96 int saved_errno = errno;
99 /* best effort in out of mem situations */
100 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
101 vfprintf(stderr, fmt, ap);
102 fprintf(stderr, "\n");
104 vfprintf(stderr, nfmt, ap);
109 vsyslog(pri, fmt, ap);
115 log_warn(const char *emsg, ...)
119 int saved_errno = errno;
121 /* best effort to even work in out of memory situations */
123 logit(LOG_CRIT, "%s", strerror(saved_errno));
127 if (asprintf(&nfmt, "%s: %s", emsg,
128 strerror(saved_errno)) == -1) {
130 vlog(LOG_CRIT, emsg, ap);
131 logit(LOG_CRIT, "%s", strerror(saved_errno));
133 vlog(LOG_CRIT, nfmt, ap);
143 log_warnx(const char *emsg, ...)
148 vlog(LOG_CRIT, emsg, ap);
153 log_info(const char *emsg, ...)
158 vlog(LOG_INFO, emsg, ap);
163 log_debug(const char *emsg, ...)
169 vlog(LOG_DEBUG, emsg, ap);
175 vfatalc(int code, const char *emsg, va_list ap)
177 static char s[BUFSIZ];
181 (void)vsnprintf(s, sizeof(s), emsg, ap);
188 logit(LOG_CRIT, "%s: %s%s%s",
189 log_procname, s, sep, strerror(code));
191 logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
195 fatal(const char *emsg, ...)
200 vfatalc(errno, emsg, ap);
206 fatalx(const char *emsg, ...)
211 vfatalc(0, emsg, ap);