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