sync
[bitrig.git] / usr.sbin / npppd / npppd / log.c
blobdeb85fd9362573bfc52e94ac9584cc98b80a6b0a
1 /* $OpenBSD: log.c,v 1.2 2012/11/13 17:10:40 yasuoka Exp $ */
3 /*
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 MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
23 #include <errno.h>
24 #include <netdb.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <time.h>
32 #include "debugutil.h"
33 #include "log.h"
35 int debug;
36 extern int debugsyslog;
38 void logit(int, const char *, ...);
40 void
41 log_init(int n_debug)
43 extern char *__progname;
45 debug = n_debug;
47 if (!debug)
48 openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
50 tzset();
53 void
54 logit(int pri, const char *fmt, ...)
56 va_list ap;
58 va_start(ap, fmt);
59 vlog(pri, fmt, ap);
60 va_end(ap);
63 void
64 vlog(int pri, const char *fmt, va_list ap)
66 vlog_printf(pri, fmt, ap);
70 void
71 log_warn(const char *emsg, ...)
73 char *nfmt;
74 va_list ap;
76 /* best effort to even work in out of memory situations */
77 if (emsg == NULL)
78 logit(LOG_CRIT, "%s", strerror(errno));
79 else {
80 va_start(ap, emsg);
82 if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
83 /* we tried it... */
84 vlog(LOG_CRIT, emsg, ap);
85 logit(LOG_CRIT, "%s", strerror(errno));
86 } else {
87 vlog(LOG_CRIT, nfmt, ap);
88 free(nfmt);
90 va_end(ap);
94 void
95 log_warnx(const char *emsg, ...)
97 va_list ap;
99 va_start(ap, emsg);
100 vlog(LOG_CRIT, emsg, ap);
101 va_end(ap);
104 void
105 log_info(const char *emsg, ...)
107 va_list ap;
109 va_start(ap, emsg);
110 vlog(LOG_INFO, emsg, ap);
111 va_end(ap);
114 void
115 log_debug(const char *emsg, ...)
117 va_list ap;
119 if (debug || debugsyslog) {
120 va_start(ap, emsg);
121 vlog(LOG_DEBUG, emsg, ap);
122 va_end(ap);
126 void
127 fatal(const char *emsg)
129 if (emsg == NULL)
130 logit(LOG_CRIT, "fatal: %s", strerror(errno));
131 else
132 if (errno)
133 logit(LOG_CRIT, "fatal: %s: %s",
134 emsg, strerror(errno));
135 else
136 logit(LOG_CRIT, "fatal: %s", emsg);
138 exit(1);
141 void
142 fatalx(const char *emsg)
144 errno = 0;
145 fatal(emsg);