2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * 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 AUTHORS 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 AUTHORS 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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
52 #define PJDLOG_NEVER_INITIALIZED 0
53 #define PJDLOG_NOT_INITIALIZED 1
54 #define PJDLOG_INITIALIZED 2
56 static int pjdlog_initialized = PJDLOG_NEVER_INITIALIZED;
57 static int pjdlog_mode, pjdlog_debug_level;
58 static char pjdlog_prefix[128];
61 pjdlog_printf_arginfo_humanized_number(const struct printf_info *pi __unused,
66 argt[0] = PA_INT | PA_FLAG_INTMAX;
71 pjdlog_printf_render_humanized_number(struct __printf_io *io,
72 const struct printf_info *pi, const void * const *arg)
78 num = *(const intmax_t *)arg[0];
79 humanize_number(buf, sizeof(buf), (int64_t)num, "", HN_AUTOSCALE,
80 HN_NOSPACE | HN_DECIMAL);
81 ret = __printf_out(io, pi, buf, strlen(buf));
87 pjdlog_printf_arginfo_sockaddr(const struct printf_info *pi __unused,
97 pjdlog_printf_render_sockaddr(struct __printf_io *io,
98 const struct printf_info *pi, const void * const *arg)
100 const struct sockaddr_storage *ss;
104 ss = *(const struct sockaddr_storage * const *)arg[0];
105 switch (ss->ss_family) {
108 char addr[INET_ADDRSTRLEN];
109 const struct sockaddr_in *sin;
112 sin = (const struct sockaddr_in *)ss;
113 port = ntohs(sin->sin_port);
114 if (inet_ntop(ss->ss_family, &sin->sin_addr, addr,
115 sizeof(addr)) == NULL) {
116 PJDLOG_ABORT("inet_ntop(AF_INET) failed: %s.",
119 snprintf(buf, sizeof(buf), "%s:%u", addr, port);
124 char addr[INET6_ADDRSTRLEN];
125 const struct sockaddr_in6 *sin;
128 sin = (const struct sockaddr_in6 *)ss;
129 port = ntohs(sin->sin6_port);
130 if (inet_ntop(ss->ss_family, &sin->sin6_addr, addr,
131 sizeof(addr)) == NULL) {
132 PJDLOG_ABORT("inet_ntop(AF_INET6) failed: %s.",
135 snprintf(buf, sizeof(buf), "[%s]:%u", addr, port);
139 snprintf(buf, sizeof(buf), "[unsupported family %hhu]",
143 ret = __printf_out(io, pi, buf, strlen(buf));
149 pjdlog_init(int mode)
152 assert(pjdlog_initialized == PJDLOG_NEVER_INITIALIZED ||
153 pjdlog_initialized == PJDLOG_NOT_INITIALIZED);
154 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
156 if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) {
158 register_printf_render_std("T");
159 register_printf_render('N',
160 pjdlog_printf_render_humanized_number,
161 pjdlog_printf_arginfo_humanized_number);
162 register_printf_render('S',
163 pjdlog_printf_render_sockaddr,
164 pjdlog_printf_arginfo_sockaddr);
167 if (mode == PJDLOG_MODE_SYSLOG)
168 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
170 pjdlog_debug_level = 0;
171 bzero(pjdlog_prefix, sizeof(pjdlog_prefix));
173 pjdlog_initialized = PJDLOG_INITIALIZED;
180 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
182 if (pjdlog_mode == PJDLOG_MODE_SYSLOG)
185 pjdlog_initialized = PJDLOG_NOT_INITIALIZED;
189 * Configure where the logs should go.
190 * By default they are send to stdout/stderr, but after going into background
191 * (eg. by calling daemon(3)) application is responsible for changing mode to
192 * PJDLOG_MODE_SYSLOG, so logs will be send to syslog.
195 pjdlog_mode_set(int mode)
198 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
199 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
201 if (pjdlog_mode == mode)
204 if (mode == PJDLOG_MODE_SYSLOG)
205 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
206 else /* if (mode == PJDLOG_MODE_STD) */
213 * Return current mode.
216 pjdlog_mode_get(void)
219 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
221 return (pjdlog_mode);
225 * Set debug level. All the logs above the level specified here will be
229 pjdlog_debug_set(int level)
232 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
235 pjdlog_debug_level = level;
239 * Return current debug level.
242 pjdlog_debug_get(void)
245 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
247 return (pjdlog_debug_level);
251 * Set prefix that will be used before each log.
252 * Setting prefix to NULL will remove it.
255 pjdlog_prefix_set(const char *fmt, ...)
259 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
262 pjdlogv_prefix_set(fmt, ap);
267 * Set prefix that will be used before each log.
268 * Setting prefix to NULL will remove it.
271 pjdlogv_prefix_set(const char *fmt, va_list ap)
274 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
277 vsnprintf(pjdlog_prefix, sizeof(pjdlog_prefix), fmt, ap);
281 * Convert log level into string.
284 pjdlog_level_string(int loglevel)
305 assert(!"Invalid log level.");
306 abort(); /* XXX: gcc */
310 * Common log routine.
313 pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt, ...)
317 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
320 pjdlogv_common(loglevel, debuglevel, error, fmt, ap);
325 * Common log routine, which can handle regular log level as well as debug
326 * level. We decide here where to send the logs (stdout/stderr or syslog).
329 pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
333 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
334 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
335 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
336 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
337 loglevel == LOG_INFO || loglevel == LOG_DEBUG);
338 assert(loglevel != LOG_DEBUG || debuglevel > 0);
341 /* Ignore debug above configured level. */
342 if (loglevel == LOG_DEBUG && debuglevel > pjdlog_debug_level)
345 switch (pjdlog_mode) {
346 case PJDLOG_MODE_STD:
351 * We send errors and warning to stderr and the rest to stdout.
367 assert(!"Invalid loglevel.");
368 abort(); /* XXX: gcc */
371 fprintf(out, "[%s]", pjdlog_level_string(loglevel));
372 /* Attach debuglevel if this is debug log. */
373 if (loglevel == LOG_DEBUG)
374 fprintf(out, "[%d]", debuglevel);
375 fprintf(out, " %s", pjdlog_prefix);
376 vfprintf(out, fmt, ap);
378 fprintf(out, ": %s.", strerror(error));
383 case PJDLOG_MODE_SYSLOG:
388 len = snprintf(log, sizeof(log), "%s", pjdlog_prefix);
389 if ((size_t)len < sizeof(log))
390 len += vsnprintf(log + len, sizeof(log) - len, fmt, ap);
391 if (error != -1 && (size_t)len < sizeof(log)) {
392 (void)snprintf(log + len, sizeof(log) - len, ": %s.",
395 syslog(loglevel, "%s", log);
399 assert(!"Invalid mode.");
407 pjdlogv(int loglevel, const char *fmt, va_list ap)
410 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
412 /* LOG_DEBUG is invalid here, pjdlogv?_debug() should be used. */
413 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
414 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
415 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
416 loglevel == LOG_INFO);
418 pjdlogv_common(loglevel, 0, -1, fmt, ap);
425 pjdlog(int loglevel, const char *fmt, ...)
429 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
432 pjdlogv(loglevel, fmt, ap);
440 pjdlogv_debug(int debuglevel, const char *fmt, va_list ap)
443 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
445 pjdlogv_common(LOG_DEBUG, debuglevel, -1, fmt, ap);
452 pjdlog_debug(int debuglevel, const char *fmt, ...)
456 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
459 pjdlogv_debug(debuglevel, fmt, ap);
464 * Error logs with errno logging.
467 pjdlogv_errno(int loglevel, const char *fmt, va_list ap)
470 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
472 pjdlogv_common(loglevel, 0, errno, fmt, ap);
476 * Error logs with errno logging.
479 pjdlog_errno(int loglevel, const char *fmt, ...)
483 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
486 pjdlogv_errno(loglevel, fmt, ap);
491 * Log error, errno and exit.
494 pjdlogv_exit(int exitcode, const char *fmt, va_list ap)
497 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
499 pjdlogv_errno(LOG_ERR, fmt, ap);
505 * Log error, errno and exit.
508 pjdlog_exit(int exitcode, const char *fmt, ...)
512 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
515 pjdlogv_exit(exitcode, fmt, ap);
521 * Log error and exit.
524 pjdlogv_exitx(int exitcode, const char *fmt, va_list ap)
527 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
529 pjdlogv(LOG_ERR, fmt, ap);
535 * Log error and exit.
538 pjdlog_exitx(int exitcode, const char *fmt, ...)
542 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
545 pjdlogv_exitx(exitcode, fmt, ap);
551 * Log failure message and exit.
554 pjdlog_abort(const char *func, const char *file, int line,
555 const char *failedexpr, const char *fmt, ...)
559 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
562 * When there is no message we pass __func__ as 'fmt'.
563 * It would be cleaner to pass NULL or "", but gcc generates a warning
568 pjdlogv_critical(fmt, ap);
571 if (failedexpr == NULL) {
573 pjdlog_critical("Aborted at file %s, line %d.", file,
576 pjdlog_critical("Aborted at function %s, file %s, line %d.",
581 pjdlog_critical("Assertion failed: (%s), file %s, line %d.",
582 failedexpr, file, line);
584 pjdlog_critical("Assertion failed: (%s), function %s, file %s, line %d.",
585 failedexpr, func, file, line);