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/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #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];
62 pjdlog_printf_arginfo_humanized_number(const struct printf_info *pi __unused,
67 argt[0] = PA_INT | PA_FLAG_INTMAX;
72 pjdlog_printf_render_humanized_number(struct __printf_io *io,
73 const struct printf_info *pi, const void * const *arg)
79 num = *(const intmax_t *)arg[0];
80 humanize_number(buf, sizeof(buf), (int64_t)num, "", HN_AUTOSCALE,
81 HN_NOSPACE | HN_DECIMAL);
82 ret = __printf_out(io, pi, buf, strlen(buf));
88 pjdlog_printf_arginfo_sockaddr(const struct printf_info *pi __unused,
98 pjdlog_printf_render_sockaddr(struct __printf_io *io,
99 const struct printf_info *pi, const void * const *arg)
101 const struct sockaddr_storage *ss;
105 ss = *(const struct sockaddr_storage * const *)arg[0];
106 switch (ss->ss_family) {
109 char addr[INET_ADDRSTRLEN];
110 const struct sockaddr_in *sin;
113 sin = (const struct sockaddr_in *)ss;
114 port = ntohs(sin->sin_port);
115 if (inet_ntop(ss->ss_family, &sin->sin_addr, addr,
116 sizeof(addr)) == NULL) {
117 PJDLOG_ABORT("inet_ntop(AF_INET) failed: %s.",
120 snprintf(buf, sizeof(buf), "%s:%u", addr, port);
125 char addr[INET6_ADDRSTRLEN];
126 const struct sockaddr_in6 *sin;
129 sin = (const struct sockaddr_in6 *)ss;
130 port = ntohs(sin->sin6_port);
131 if (inet_ntop(ss->ss_family, &sin->sin6_addr, addr,
132 sizeof(addr)) == NULL) {
133 PJDLOG_ABORT("inet_ntop(AF_INET6) failed: %s.",
136 snprintf(buf, sizeof(buf), "[%s]:%u", addr, port);
140 snprintf(buf, sizeof(buf), "[unsupported family %hhu]",
144 ret = __printf_out(io, pi, buf, strlen(buf));
148 #endif /* __FreeBSD__ */
151 pjdlog_init(int mode)
155 assert(pjdlog_initialized == PJDLOG_NEVER_INITIALIZED ||
156 pjdlog_initialized == PJDLOG_NOT_INITIALIZED);
157 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
161 if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) {
164 register_printf_render_std("T");
165 register_printf_render('N',
166 pjdlog_printf_render_humanized_number,
167 pjdlog_printf_arginfo_humanized_number);
168 register_printf_render('S',
169 pjdlog_printf_render_sockaddr,
170 pjdlog_printf_arginfo_sockaddr);
174 if (mode == PJDLOG_MODE_SYSLOG)
175 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
177 pjdlog_debug_level = 0;
178 bzero(pjdlog_prefix, sizeof(pjdlog_prefix));
180 pjdlog_initialized = PJDLOG_INITIALIZED;
190 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
194 if (pjdlog_mode == PJDLOG_MODE_SYSLOG)
197 pjdlog_initialized = PJDLOG_NOT_INITIALIZED;
203 * Configure where the logs should go.
204 * By default they are send to stdout/stderr, but after going into background
205 * (eg. by calling daemon(3)) application is responsible for changing mode to
206 * PJDLOG_MODE_SYSLOG, so logs will be send to syslog.
209 pjdlog_mode_set(int mode)
213 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
214 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
216 if (pjdlog_mode == mode)
221 if (mode == PJDLOG_MODE_SYSLOG)
222 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
223 else /* if (mode == PJDLOG_MODE_STD) */
232 * Return current mode.
235 pjdlog_mode_get(void)
238 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
240 return (pjdlog_mode);
244 * Set debug level. All the logs above the level specified here will be
248 pjdlog_debug_set(int level)
251 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
254 pjdlog_debug_level = level;
258 * Return current debug level.
261 pjdlog_debug_get(void)
264 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
266 return (pjdlog_debug_level);
270 * Set prefix that will be used before each log.
271 * Setting prefix to NULL will remove it.
274 pjdlog_prefix_set(const char *fmt, ...)
278 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
281 pjdlogv_prefix_set(fmt, ap);
286 * Set prefix that will be used before each log.
287 * Setting prefix to NULL will remove it.
290 pjdlogv_prefix_set(const char *fmt, va_list ap)
294 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
299 vsnprintf(pjdlog_prefix, sizeof(pjdlog_prefix), fmt, ap);
305 * Convert log level into string.
308 pjdlog_level_string(int loglevel)
329 assert(!"Invalid log level.");
330 abort(); /* XXX: gcc */
334 * Common log routine.
337 pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt, ...)
341 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
344 pjdlogv_common(loglevel, debuglevel, error, fmt, ap);
349 * Common log routine, which can handle regular log level as well as debug
350 * level. We decide here where to send the logs (stdout/stderr or syslog).
353 pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
358 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
359 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
360 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
361 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
362 loglevel == LOG_INFO || loglevel == LOG_DEBUG);
363 assert(loglevel != LOG_DEBUG || debuglevel > 0);
366 /* Ignore debug above configured level. */
367 if (loglevel == LOG_DEBUG && debuglevel > pjdlog_debug_level)
372 switch (pjdlog_mode) {
373 case PJDLOG_MODE_STD:
378 * We send errors and warning to stderr and the rest to stdout.
394 assert(!"Invalid loglevel.");
395 abort(); /* XXX: gcc */
398 fprintf(out, "(%d) ", getpid());
399 fprintf(out, "[%s]", pjdlog_level_string(loglevel));
400 /* Attach debuglevel if this is debug log. */
401 if (loglevel == LOG_DEBUG)
402 fprintf(out, "[%d]", debuglevel);
403 fprintf(out, " %s", pjdlog_prefix);
404 vfprintf(out, fmt, ap);
406 fprintf(out, ": %s.", strerror(error));
411 case PJDLOG_MODE_SYSLOG:
416 len = snprintf(log, sizeof(log), "%s", pjdlog_prefix);
417 if ((size_t)len < sizeof(log))
418 len += vsnprintf(log + len, sizeof(log) - len, fmt, ap);
419 if (error != -1 && (size_t)len < sizeof(log)) {
420 (void)snprintf(log + len, sizeof(log) - len, ": %s.",
423 syslog(loglevel, "%s", log);
427 assert(!"Invalid mode.");
437 pjdlogv(int loglevel, const char *fmt, va_list ap)
440 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
442 /* LOG_DEBUG is invalid here, pjdlogv?_debug() should be used. */
443 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
444 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
445 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
446 loglevel == LOG_INFO);
448 pjdlogv_common(loglevel, 0, -1, fmt, ap);
455 pjdlog(int loglevel, const char *fmt, ...)
459 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
462 pjdlogv(loglevel, fmt, ap);
470 pjdlogv_debug(int debuglevel, const char *fmt, va_list ap)
473 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
475 pjdlogv_common(LOG_DEBUG, debuglevel, -1, fmt, ap);
482 pjdlog_debug(int debuglevel, const char *fmt, ...)
486 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
489 pjdlogv_debug(debuglevel, fmt, ap);
494 * Error logs with errno logging.
497 pjdlogv_errno(int loglevel, const char *fmt, va_list ap)
500 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
502 pjdlogv_common(loglevel, 0, errno, fmt, ap);
506 * Error logs with errno logging.
509 pjdlog_errno(int loglevel, const char *fmt, ...)
513 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
516 pjdlogv_errno(loglevel, fmt, ap);
521 * Log error, errno and exit.
524 pjdlogv_exit(int exitcode, const char *fmt, va_list ap)
527 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
529 pjdlogv_errno(LOG_ERR, fmt, ap);
535 * Log error, errno and exit.
538 pjdlog_exit(int exitcode, const char *fmt, ...)
542 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
545 pjdlogv_exit(exitcode, fmt, ap);
551 * Log error and exit.
554 pjdlogv_exitx(int exitcode, const char *fmt, va_list ap)
557 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
559 pjdlogv(LOG_ERR, fmt, ap);
565 * Log error and exit.
568 pjdlog_exitx(int exitcode, const char *fmt, ...)
572 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
575 pjdlogv_exitx(exitcode, fmt, ap);
581 * Log failure message and exit.
584 pjdlog_abort(const char *func, const char *file, int line,
585 const char *failedexpr, const char *fmt, ...)
589 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
592 * When there is no message we pass __func__ as 'fmt'.
593 * It would be cleaner to pass NULL or "", but gcc generates a warning
598 pjdlogv_critical(fmt, ap);
601 if (failedexpr == NULL) {
603 pjdlog_critical("Aborted at file %s, line %d.", file,
606 pjdlog_critical("Aborted at function %s, file %s, line %d.",
611 pjdlog_critical("Assertion failed: (%s), file %s, line %d.",
612 failedexpr, file, line);
614 pjdlog_critical("Assertion failed: (%s), function %s, file %s, line %d.",
615 failedexpr, func, file, line);