xref: /openbsd/usr.sbin/ypldap/log.c (revision 898184e3)
1 /*	$OpenBSD: log.c,v 1.1 2008/06/26 15:10:01 pyr Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <syslog.h>
27 
28 void		 log_init(int);
29 void		 log_warn(const char *, ...);
30 void		 log_warnx(const char *, ...);
31 void		 log_info(const char *, ...);
32 void		 log_debug(const char *, ...);
33 __dead void	 fatal(const char *);
34 __dead void	 fatalx(const char *);
35 
36 int	 debug;
37 
38 void	 vlog(int, const char *, va_list);
39 void	 logit(int, const char *, ...);
40 
41 void
42 log_init(int n_debug)
43 {
44 	extern char	*__progname;
45 
46 	debug = n_debug;
47 
48 	if (!debug)
49 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
50 
51 	tzset();
52 }
53 
54 void
55 logit(int pri, const char *fmt, ...)
56 {
57 	va_list	ap;
58 
59 	va_start(ap, fmt);
60 	vlog(pri, fmt, ap);
61 	va_end(ap);
62 }
63 
64 void
65 vlog(int pri, const char *fmt, va_list ap)
66 {
67 	char	*nfmt;
68 
69 	if (debug) {
70 		/* best effort in out of mem situations */
71 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
72 			vfprintf(stderr, fmt, ap);
73 			fprintf(stderr, "\n");
74 		} else {
75 			vfprintf(stderr, nfmt, ap);
76 			free(nfmt);
77 		}
78 		fflush(stderr);
79 	} else
80 		vsyslog(pri, fmt, ap);
81 }
82 
83 
84 void
85 log_warn(const char *emsg, ...)
86 {
87 	char	*nfmt;
88 	va_list	 ap;
89 
90 	/* best effort to even work in out of memory situations */
91 	if (emsg == NULL)
92 		logit(LOG_CRIT, "%s", strerror(errno));
93 	else {
94 		va_start(ap, emsg);
95 
96 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
97 			/* we tried it... */
98 			vlog(LOG_CRIT, emsg, ap);
99 			logit(LOG_CRIT, "%s", strerror(errno));
100 		} else {
101 			vlog(LOG_CRIT, nfmt, ap);
102 			free(nfmt);
103 		}
104 		va_end(ap);
105 	}
106 }
107 
108 void
109 log_warnx(const char *emsg, ...)
110 {
111 	va_list	 ap;
112 
113 	va_start(ap, emsg);
114 	vlog(LOG_CRIT, emsg, ap);
115 	va_end(ap);
116 }
117 
118 void
119 log_info(const char *emsg, ...)
120 {
121 	va_list	 ap;
122 
123 	va_start(ap, emsg);
124 	vlog(LOG_INFO, emsg, ap);
125 	va_end(ap);
126 }
127 
128 void
129 log_debug(const char *emsg, ...)
130 {
131 	va_list	 ap;
132 
133 	if (debug > 1) {
134 		va_start(ap, emsg);
135 		vlog(LOG_DEBUG, emsg, ap);
136 		va_end(ap);
137 	}
138 }
139 
140 void
141 fatal(const char *emsg)
142 {
143 	if (emsg == NULL)
144 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
145 	else
146 		if (errno)
147 			logit(LOG_CRIT, "fatal: %s: %s",
148 			    emsg, strerror(errno));
149 		else
150 			logit(LOG_CRIT, "fatal: %s", emsg);
151 
152 	exit(1);
153 }
154 
155 void
156 fatalx(const char *emsg)
157 {
158 	errno = 0;
159 	fatal(emsg);
160 }
161