xref: /freebsd/usr.sbin/ypldap/log.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*	$OpenBSD: log.c,v 1.1 2008/06/26 15:10:01 pyr Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*
5  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <time.h>
29 
30 void		 log_init(int);
31 void		 log_warn(const char *, ...);
32 void		 log_warnx(const char *, ...);
33 void		 log_info(const char *, ...);
34 void		 log_debug(const char *, ...);
35 __dead2 void	 fatal(const char *);
36 __dead2 void	 fatalx(const char *);
37 
38 int	 debug;
39 
40 void	 vlog(int, const char *, va_list);
41 void	 logit(int, const char *, ...);
42 
43 void
44 log_init(int n_debug)
45 {
46 	extern char	*__progname;
47 
48 	debug = n_debug;
49 
50 	if (!debug)
51 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
52 
53 	tzset();
54 }
55 
56 void
57 logit(int pri, const char *fmt, ...)
58 {
59 	va_list	ap;
60 
61 	va_start(ap, fmt);
62 	vlog(pri, fmt, ap);
63 	va_end(ap);
64 }
65 
66 void
67 vlog(int pri, const char *fmt, va_list ap)
68 {
69 	char	*nfmt;
70 
71 	if (debug) {
72 		/* best effort in out of mem situations */
73 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
74 			vfprintf(stderr, fmt, ap);
75 			fprintf(stderr, "\n");
76 		} else {
77 			vfprintf(stderr, nfmt, ap);
78 			free(nfmt);
79 		}
80 		fflush(stderr);
81 	} else
82 		vsyslog(pri, fmt, ap);
83 }
84 
85 
86 void
87 log_warn(const char *emsg, ...)
88 {
89 	char	*nfmt;
90 	va_list	 ap;
91 
92 	/* best effort to even work in out of memory situations */
93 	if (emsg == NULL)
94 		logit(LOG_CRIT, "%s", strerror(errno));
95 	else {
96 		va_start(ap, emsg);
97 
98 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
99 			/* we tried it... */
100 			vlog(LOG_CRIT, emsg, ap);
101 			logit(LOG_CRIT, "%s", strerror(errno));
102 		} else {
103 			vlog(LOG_CRIT, nfmt, ap);
104 			free(nfmt);
105 		}
106 		va_end(ap);
107 	}
108 }
109 
110 void
111 log_warnx(const char *emsg, ...)
112 {
113 	va_list	 ap;
114 
115 	va_start(ap, emsg);
116 	vlog(LOG_CRIT, emsg, ap);
117 	va_end(ap);
118 }
119 
120 void
121 log_info(const char *emsg, ...)
122 {
123 	va_list	 ap;
124 
125 	va_start(ap, emsg);
126 	vlog(LOG_INFO, emsg, ap);
127 	va_end(ap);
128 }
129 
130 void
131 log_debug(const char *emsg, ...)
132 {
133 	va_list	 ap;
134 
135 	if (debug > 1) {
136 		va_start(ap, emsg);
137 		vlog(LOG_DEBUG, emsg, ap);
138 		va_end(ap);
139 	}
140 }
141 
142 void
143 fatal(const char *emsg)
144 {
145 	if (emsg == NULL)
146 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
147 	else
148 		if (errno)
149 			logit(LOG_CRIT, "fatal: %s: %s",
150 			    emsg, strerror(errno));
151 		else
152 			logit(LOG_CRIT, "fatal: %s", emsg);
153 
154 	exit(1);
155 }
156 
157 void
158 fatalx(const char *emsg)
159 {
160 	errno = 0;
161 	fatal(emsg);
162 }
163