kernel - Order ipfw3 module before other ipfw3_* modules
[dragonfly.git] / usr.sbin / autofs / log.c
blob895decc7fa46bca1d2a73ed49c5540ce4a050eff
1 /*-
2 * Copyright (c) 2016 The DragonFly Project
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * All rights reserved.
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
32 #include <errno.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <vis.h>
40 #include "common.h"
42 static int log_level = 0;
43 static char *peer_name = NULL;
44 static char *peer_addr = NULL;
46 #define MSGBUF_LEN 1024
48 void
49 log_init(int level)
52 log_level = level;
53 openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
56 void
57 log_set_peer_name(const char *name)
61 * XXX: Turn it into assertion?
63 if (peer_name != NULL)
64 log_errx(1, "%s called twice", __func__);
65 if (peer_addr == NULL)
66 log_errx(1, "%s called before log_set_peer_addr", __func__);
68 peer_name = checked_strdup(name);
71 void
72 log_set_peer_addr(const char *addr)
76 * XXX: Turn it into assertion?
78 if (peer_addr != NULL)
79 log_errx(1, "%s called twice", __func__);
81 peer_addr = checked_strdup(addr);
84 static void
85 log_common(int priority, int log_errno, const char *fmt, va_list ap)
87 static char msgbuf[MSGBUF_LEN];
88 static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
89 char *errstr;
90 int ret;
92 ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
93 if (ret < 0) {
94 fprintf(stderr, "%s: snprintf failed", getprogname());
95 syslog(LOG_CRIT, "snprintf failed");
96 exit(1);
99 ret = strnvis(msgbuf_strvised, msgbuf, sizeof(msgbuf_strvised), VIS_NL);
100 if (ret < 0) {
101 fprintf(stderr, "%s: strnvis failed", getprogname());
102 syslog(LOG_CRIT, "strnvis failed");
103 exit(1);
106 if (log_errno == -1) {
107 if (peer_name != NULL) {
108 fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
109 peer_addr, peer_name, msgbuf_strvised);
110 syslog(priority, "%s (%s): %s",
111 peer_addr, peer_name, msgbuf_strvised);
112 } else if (peer_addr != NULL) {
113 fprintf(stderr, "%s: %s: %s\n", getprogname(),
114 peer_addr, msgbuf_strvised);
115 syslog(priority, "%s: %s",
116 peer_addr, msgbuf_strvised);
117 } else {
118 fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
119 syslog(priority, "%s", msgbuf_strvised);
122 } else {
123 errstr = strerror(log_errno);
125 if (peer_name != NULL) {
126 fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
127 peer_addr, peer_name, msgbuf_strvised, errstr);
128 syslog(priority, "%s (%s): %s: %s",
129 peer_addr, peer_name, msgbuf_strvised, errstr);
130 } else if (peer_addr != NULL) {
131 fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
132 peer_addr, msgbuf_strvised, errstr);
133 syslog(priority, "%s: %s: %s",
134 peer_addr, msgbuf_strvised, errstr);
135 } else {
136 fprintf(stderr, "%s: %s: %s\n", getprogname(),
137 msgbuf_strvised, errstr);
138 syslog(priority, "%s: %s",
139 msgbuf_strvised, errstr);
144 void
145 log_err(int eval, const char *fmt, ...)
147 va_list ap;
149 va_start(ap, fmt);
150 log_common(LOG_CRIT, errno, fmt, ap);
151 va_end(ap);
153 exit(eval);
156 void
157 log_errx(int eval, const char *fmt, ...)
159 va_list ap;
161 va_start(ap, fmt);
162 log_common(LOG_CRIT, -1, fmt, ap);
163 va_end(ap);
165 exit(eval);
168 void
169 log_warn(const char *fmt, ...)
171 va_list ap;
173 va_start(ap, fmt);
174 log_common(LOG_WARNING, errno, fmt, ap);
175 va_end(ap);
178 void
179 log_warnx(const char *fmt, ...)
181 va_list ap;
183 va_start(ap, fmt);
184 log_common(LOG_WARNING, -1, fmt, ap);
185 va_end(ap);
188 void
189 log_debugx(const char *fmt, ...)
191 va_list ap;
193 if (log_level == 0)
194 return;
196 va_start(ap, fmt);
197 log_common(LOG_DEBUG, -1, fmt, ap);
198 va_end(ap);