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