sed(1): Appease older GCC.
[freebsd-src.git] / usr.sbin / autofs / log.c
blobb3935dc0ed01496ff8e8c5e047ff24d4e641fa3b
1 /*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <syslog.h>
40 #include <vis.h>
42 #include "common.h"
44 static int log_level = 0;
45 static char *peer_name = NULL;
46 static char *peer_addr = NULL;
48 #define MSGBUF_LEN 1024
50 void
51 log_init(int level)
54 log_level = level;
55 openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
58 void
59 log_set_peer_name(const char *name)
63 * XXX: Turn it into assertion?
65 if (peer_name != NULL)
66 log_errx(1, "%s called twice", __func__);
67 if (peer_addr == NULL)
68 log_errx(1, "%s called before log_set_peer_addr", __func__);
70 peer_name = checked_strdup(name);
73 void
74 log_set_peer_addr(const char *addr)
78 * XXX: Turn it into assertion?
80 if (peer_addr != NULL)
81 log_errx(1, "%s called twice", __func__);
83 peer_addr = checked_strdup(addr);
86 static void
87 log_common(int priority, int log_errno, const char *fmt, va_list ap)
89 static char msgbuf[MSGBUF_LEN];
90 static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
91 char *errstr;
92 int ret;
94 ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
95 if (ret < 0) {
96 fprintf(stderr, "%s: snprintf failed", getprogname());
97 syslog(LOG_CRIT, "snprintf failed");
98 exit(1);
101 ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
102 if (ret < 0) {
103 fprintf(stderr, "%s: strnvis failed", getprogname());
104 syslog(LOG_CRIT, "strnvis failed");
105 exit(1);
108 if (log_errno == -1) {
109 if (peer_name != NULL) {
110 fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
111 peer_addr, peer_name, msgbuf_strvised);
112 syslog(priority, "%s (%s): %s",
113 peer_addr, peer_name, msgbuf_strvised);
114 } else if (peer_addr != NULL) {
115 fprintf(stderr, "%s: %s: %s\n", getprogname(),
116 peer_addr, msgbuf_strvised);
117 syslog(priority, "%s: %s",
118 peer_addr, msgbuf_strvised);
119 } else {
120 fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
121 syslog(priority, "%s", msgbuf_strvised);
124 } else {
125 errstr = strerror(log_errno);
127 if (peer_name != NULL) {
128 fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
129 peer_addr, peer_name, msgbuf_strvised, errstr);
130 syslog(priority, "%s (%s): %s: %s",
131 peer_addr, peer_name, msgbuf_strvised, errstr);
132 } else if (peer_addr != NULL) {
133 fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
134 peer_addr, msgbuf_strvised, errstr);
135 syslog(priority, "%s: %s: %s",
136 peer_addr, msgbuf_strvised, errstr);
137 } else {
138 fprintf(stderr, "%s: %s: %s\n", getprogname(),
139 msgbuf_strvised, errstr);
140 syslog(priority, "%s: %s",
141 msgbuf_strvised, errstr);
146 void
147 log_err(int eval, const char *fmt, ...)
149 va_list ap;
151 va_start(ap, fmt);
152 log_common(LOG_CRIT, errno, fmt, ap);
153 va_end(ap);
155 exit(eval);
158 void
159 log_errx(int eval, const char *fmt, ...)
161 va_list ap;
163 va_start(ap, fmt);
164 log_common(LOG_CRIT, -1, fmt, ap);
165 va_end(ap);
167 exit(eval);
170 void
171 log_warn(const char *fmt, ...)
173 va_list ap;
175 va_start(ap, fmt);
176 log_common(LOG_WARNING, errno, fmt, ap);
177 va_end(ap);
180 void
181 log_warnx(const char *fmt, ...)
183 va_list ap;
185 va_start(ap, fmt);
186 log_common(LOG_WARNING, -1, fmt, ap);
187 va_end(ap);
190 void
191 log_debugx(const char *fmt, ...)
193 va_list ap;
195 if (log_level == 0)
196 return;
198 va_start(ap, fmt);
199 log_common(LOG_DEBUG, -1, fmt, ap);
200 va_end(ap);