2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 The FreeBSD Foundation
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
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
32 #include <sys/cdefs.h>
43 static int log_level = 0;
44 static char *peer_name = NULL;
45 static char *peer_addr = NULL;
47 #define MSGBUF_LEN 1024
54 openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
58 log_set_peer_name(const char *name)
62 * XXX: Turn it into assertion?
64 if (peer_name != NULL)
65 log_errx(1, "%s called twice", __func__);
66 if (peer_addr == NULL)
67 log_errx(1, "%s called before log_set_peer_addr", __func__);
69 peer_name = checked_strdup(name);
73 log_set_peer_addr(const char *addr)
77 * XXX: Turn it into assertion?
79 if (peer_addr != NULL)
80 log_errx(1, "%s called twice", __func__);
82 peer_addr = checked_strdup(addr);
86 log_common(int priority, int log_errno, const char *fmt, va_list ap)
88 static char msgbuf[MSGBUF_LEN];
89 static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
93 ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
95 fprintf(stderr, "%s: snprintf failed", getprogname());
96 syslog(LOG_CRIT, "snprintf failed");
100 ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
102 fprintf(stderr, "%s: strnvis failed", getprogname());
103 syslog(LOG_CRIT, "strnvis failed");
107 if (log_errno == -1) {
108 if (peer_name != NULL) {
109 fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
110 peer_addr, peer_name, msgbuf_strvised);
111 syslog(priority, "%s (%s): %s",
112 peer_addr, peer_name, msgbuf_strvised);
113 } else if (peer_addr != NULL) {
114 fprintf(stderr, "%s: %s: %s\n", getprogname(),
115 peer_addr, msgbuf_strvised);
116 syslog(priority, "%s: %s",
117 peer_addr, msgbuf_strvised);
119 fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
120 syslog(priority, "%s", msgbuf_strvised);
124 errstr = strerror(log_errno);
126 if (peer_name != NULL) {
127 fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
128 peer_addr, peer_name, msgbuf_strvised, errstr);
129 syslog(priority, "%s (%s): %s: %s",
130 peer_addr, peer_name, msgbuf_strvised, errstr);
131 } else if (peer_addr != NULL) {
132 fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
133 peer_addr, msgbuf_strvised, errstr);
134 syslog(priority, "%s: %s: %s",
135 peer_addr, msgbuf_strvised, errstr);
137 fprintf(stderr, "%s: %s: %s\n", getprogname(),
138 msgbuf_strvised, errstr);
139 syslog(priority, "%s: %s",
140 msgbuf_strvised, errstr);
146 log_err(int eval, const char *fmt, ...)
151 log_common(LOG_CRIT, errno, fmt, ap);
158 log_errx(int eval, const char *fmt, ...)
163 log_common(LOG_CRIT, -1, fmt, ap);
170 log_warn(const char *fmt, ...)
175 log_common(LOG_WARNING, errno, fmt, ap);
180 log_warnx(const char *fmt, ...)
185 log_common(LOG_WARNING, -1, fmt, ap);
190 log_debugx(const char *fmt, ...)
198 log_common(LOG_DEBUG, -1, fmt, ap);