2 * Copyright (c) 2012 The FreeBSD Foundation
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
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
44 static int log_level
= 0;
45 static char *peer_name
= NULL
;
46 static char *peer_addr
= NULL
;
48 #define MSGBUF_LEN 1024
55 openlog(getprogname(), LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
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
);
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
);
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];
94 ret
= vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, ap
);
96 fprintf(stderr
, "%s: snprintf failed", getprogname());
97 syslog(LOG_CRIT
, "snprintf failed");
101 ret
= strnvis(msgbuf_strvised
, sizeof(msgbuf_strvised
), msgbuf
, VIS_NL
);
103 fprintf(stderr
, "%s: strnvis failed", getprogname());
104 syslog(LOG_CRIT
, "strnvis failed");
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
);
120 fprintf(stderr
, "%s: %s\n", getprogname(), msgbuf_strvised
);
121 syslog(priority
, "%s", msgbuf_strvised
);
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
);
138 fprintf(stderr
, "%s: %s: %s\n", getprogname(),
139 msgbuf_strvised
, errstr
);
140 syslog(priority
, "%s: %s",
141 msgbuf_strvised
, errstr
);
147 log_err(int eval
, const char *fmt
, ...)
152 log_common(LOG_CRIT
, errno
, fmt
, ap
);
159 log_errx(int eval
, const char *fmt
, ...)
164 log_common(LOG_CRIT
, -1, fmt
, ap
);
171 log_warn(const char *fmt
, ...)
176 log_common(LOG_WARNING
, errno
, fmt
, ap
);
181 log_warnx(const char *fmt
, ...)
186 log_common(LOG_WARNING
, -1, fmt
, ap
);
191 log_debugx(const char *fmt
, ...)
199 log_common(LOG_DEBUG
, -1, fmt
, ap
);