2 * Copyright (c) 2016 The DragonFly Project
3 * 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
42 static int log_level
= 0;
43 static char *peer_name
= NULL
;
44 static char *peer_addr
= NULL
;
46 #define MSGBUF_LEN 1024
53 openlog(getprogname(), LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
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
);
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
);
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];
92 ret
= vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, ap
);
94 fprintf(stderr
, "%s: snprintf failed", getprogname());
95 syslog(LOG_CRIT
, "snprintf failed");
99 ret
= strnvis(msgbuf_strvised
, msgbuf
, sizeof(msgbuf_strvised
), VIS_NL
);
101 fprintf(stderr
, "%s: strnvis failed", getprogname());
102 syslog(LOG_CRIT
, "strnvis failed");
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
);
118 fprintf(stderr
, "%s: %s\n", getprogname(), msgbuf_strvised
);
119 syslog(priority
, "%s", msgbuf_strvised
);
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
);
136 fprintf(stderr
, "%s: %s: %s\n", getprogname(),
137 msgbuf_strvised
, errstr
);
138 syslog(priority
, "%s: %s",
139 msgbuf_strvised
, errstr
);
145 log_err(int eval
, const char *fmt
, ...)
150 log_common(LOG_CRIT
, errno
, fmt
, ap
);
157 log_errx(int eval
, const char *fmt
, ...)
162 log_common(LOG_CRIT
, -1, fmt
, ap
);
169 log_warn(const char *fmt
, ...)
174 log_common(LOG_WARNING
, errno
, fmt
, ap
);
179 log_warnx(const char *fmt
, ...)
184 log_common(LOG_WARNING
, -1, fmt
, ap
);
189 log_debugx(const char *fmt
, ...)
197 log_common(LOG_DEBUG
, -1, fmt
, ap
);