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>
53 #define PJDLOG_NEVER_INITIALIZED 0
54 #define PJDLOG_NOT_INITIALIZED 1
55 #define PJDLOG_INITIALIZED 2
57 static int pjdlog_initialized = PJDLOG_NEVER_INITIALIZED;
58 static int pjdlog_mode, pjdlog_debug_level;
59 static char pjdlog_prefix[128];
63 pjdlog_printf_arginfo_humanized_number(const struct printf_info *pi __unused,
68 argt[0] = PA_INT | PA_FLAG_INTMAX;
73 pjdlog_printf_render_humanized_number(struct __printf_io *io,
74 const struct printf_info *pi, const void * const *arg)
80 num = *(const intmax_t *)arg[0];
81 humanize_number(buf, sizeof(buf), (int64_t)num, "", HN_AUTOSCALE,
82 HN_NOSPACE | HN_DECIMAL);
83 ret = __printf_out(io, pi, buf, strlen(buf));
89 pjdlog_printf_arginfo_sockaddr(const struct printf_info *pi __unused,
99 pjdlog_printf_render_sockaddr(struct __printf_io *io,
100 const struct printf_info *pi, const void * const *arg)
102 const struct sockaddr_storage *ss;
106 ss = *(const struct sockaddr_storage * const *)arg[0];
107 switch (ss->ss_family) {
110 char addr[INET_ADDRSTRLEN];
111 const struct sockaddr_in *sin;
114 sin = (const struct sockaddr_in *)ss;
115 port = ntohs(sin->sin_port);
116 if (inet_ntop(ss->ss_family, &sin->sin_addr, addr,
117 sizeof(addr)) == NULL) {
118 PJDLOG_ABORT("inet_ntop(AF_INET) failed: %s.",
121 snprintf(buf, sizeof(buf), "%s:%u", addr, port);
126 char addr[INET6_ADDRSTRLEN];
127 const struct sockaddr_in6 *sin;
130 sin = (const struct sockaddr_in6 *)ss;
131 port = ntohs(sin->sin6_port);
132 if (inet_ntop(ss->ss_family, &sin->sin6_addr, addr,
133 sizeof(addr)) == NULL) {
134 PJDLOG_ABORT("inet_ntop(AF_INET6) failed: %s.",
137 snprintf(buf, sizeof(buf), "[%s]:%u", addr, port);
141 snprintf(buf, sizeof(buf), "[unsupported family %hhu]",
145 ret = __printf_out(io, pi, buf, strlen(buf));
149 #endif /* __FreeBSD__ */
152 pjdlog_init(int mode)
156 assert(pjdlog_initialized == PJDLOG_NEVER_INITIALIZED ||
157 pjdlog_initialized == PJDLOG_NOT_INITIALIZED);
158 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
162 if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) {
165 register_printf_render_std("T");
166 register_printf_render('N',
167 pjdlog_printf_render_humanized_number,
168 pjdlog_printf_arginfo_humanized_number);
169 register_printf_render('S',
170 pjdlog_printf_render_sockaddr,
171 pjdlog_printf_arginfo_sockaddr);
175 if (mode == PJDLOG_MODE_SYSLOG)
176 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
178 pjdlog_debug_level = 0;
179 bzero(pjdlog_prefix, sizeof(pjdlog_prefix));
181 pjdlog_initialized = PJDLOG_INITIALIZED;
191 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
195 if (pjdlog_mode == PJDLOG_MODE_SYSLOG)
198 pjdlog_initialized = PJDLOG_NOT_INITIALIZED;
204 * Configure where the logs should go.
205 * By default they are send to stdout/stderr, but after going into background
206 * (eg. by calling daemon(3)) application is responsible for changing mode to
207 * PJDLOG_MODE_SYSLOG, so logs will be send to syslog.
210 pjdlog_mode_set(int mode)
214 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
215 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
217 if (pjdlog_mode == mode)
222 if (mode == PJDLOG_MODE_SYSLOG)
223 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
224 else /* if (mode == PJDLOG_MODE_STD) */
233 * Return current mode.
236 pjdlog_mode_get(void)
239 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
241 return (pjdlog_mode);
245 * Set debug level. All the logs above the level specified here will be
249 pjdlog_debug_set(int level)
252 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
255 pjdlog_debug_level = level;
259 * Return current debug level.
262 pjdlog_debug_get(void)
265 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
267 return (pjdlog_debug_level);
271 * Set prefix that will be used before each log.
272 * Setting prefix to NULL will remove it.
275 pjdlog_prefix_set(const char *fmt, ...)
279 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
282 pjdlogv_prefix_set(fmt, ap);
287 * Set prefix that will be used before each log.
288 * Setting prefix to NULL will remove it.
291 pjdlogv_prefix_set(const char *fmt, va_list ap)
295 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
300 vsnprintf(pjdlog_prefix, sizeof(pjdlog_prefix), fmt, ap);
306 * Convert log level into string.
309 pjdlog_level_string(int loglevel)
330 assert(!"Invalid log level.");
331 abort(); /* XXX: gcc */
335 * Common log routine.
338 pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt, ...)
342 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
345 pjdlogv_common(loglevel, debuglevel, error, fmt, ap);
350 * Common log routine, which can handle regular log level as well as debug
351 * level. We decide here where to send the logs (stdout/stderr or syslog).
354 pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
359 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
360 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
361 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
362 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
363 loglevel == LOG_INFO || loglevel == LOG_DEBUG);
364 assert(loglevel != LOG_DEBUG || debuglevel > 0);
367 /* Ignore debug above configured level. */
368 if (loglevel == LOG_DEBUG && debuglevel > pjdlog_debug_level)
373 switch (pjdlog_mode) {
374 case PJDLOG_MODE_STD:
379 * We send errors and warning to stderr and the rest to stdout.
395 assert(!"Invalid loglevel.");
396 abort(); /* XXX: gcc */
399 fprintf(out, "(%d) ", getpid());
400 fprintf(out, "[%s]", pjdlog_level_string(loglevel));
401 /* Attach debuglevel if this is debug log. */
402 if (loglevel == LOG_DEBUG)
403 fprintf(out, "[%d]", debuglevel);
404 fprintf(out, " %s", pjdlog_prefix);
405 vfprintf(out, fmt, ap);
407 fprintf(out, ": %s.", strerror(error));
412 case PJDLOG_MODE_SYSLOG:
417 len = snprintf(log, sizeof(log), "%s", pjdlog_prefix);
418 if ((size_t)len < sizeof(log))
419 len += vsnprintf(log + len, sizeof(log) - len, fmt, ap);
420 if (error != -1 && (size_t)len < sizeof(log)) {
421 (void)snprintf(log + len, sizeof(log) - len, ": %s.",
424 syslog(loglevel, "%s", log);
428 assert(!"Invalid mode.");
438 pjdlogv(int loglevel, const char *fmt, va_list ap)
441 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
443 /* LOG_DEBUG is invalid here, pjdlogv?_debug() should be used. */
444 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
445 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
446 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
447 loglevel == LOG_INFO);
449 pjdlogv_common(loglevel, 0, -1, fmt, ap);
456 pjdlog(int loglevel, const char *fmt, ...)
460 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
463 pjdlogv(loglevel, fmt, ap);
471 pjdlogv_debug(int debuglevel, const char *fmt, va_list ap)
474 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
476 pjdlogv_common(LOG_DEBUG, debuglevel, -1, fmt, ap);
483 pjdlog_debug(int debuglevel, const char *fmt, ...)
487 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
490 pjdlogv_debug(debuglevel, fmt, ap);
495 * Error logs with errno logging.
498 pjdlogv_errno(int loglevel, const char *fmt, va_list ap)
501 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
503 pjdlogv_common(loglevel, 0, errno, fmt, ap);
507 * Error logs with errno logging.
510 pjdlog_errno(int loglevel, const char *fmt, ...)
514 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
517 pjdlogv_errno(loglevel, fmt, ap);
522 * Log error, errno and exit.
525 pjdlogv_exit(int exitcode, const char *fmt, va_list ap)
528 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
530 pjdlogv_errno(LOG_ERR, fmt, ap);
536 * Log error, errno and exit.
539 pjdlog_exit(int exitcode, const char *fmt, ...)
543 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
546 pjdlogv_exit(exitcode, fmt, ap);
552 * Log error and exit.
555 pjdlogv_exitx(int exitcode, const char *fmt, va_list ap)
558 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
560 pjdlogv(LOG_ERR, fmt, ap);
566 * Log error and exit.
569 pjdlog_exitx(int exitcode, const char *fmt, ...)
573 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
576 pjdlogv_exitx(exitcode, fmt, ap);
582 * Log failure message and exit.
585 pjdlog_abort(const char *func, const char *file, int line,
586 const char *failedexpr, const char *fmt, ...)
590 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
593 * When there is no message we pass __func__ as 'fmt'.
594 * It would be cleaner to pass NULL or "", but gcc generates a warning
599 pjdlogv_critical(fmt, ap);
602 if (failedexpr == NULL) {
604 pjdlog_critical("Aborted at file %s, line %d.", file,
607 pjdlog_critical("Aborted at function %s, file %s, line %d.",
612 pjdlog_critical("Assertion failed: (%s), file %s, line %d.",
613 failedexpr, file, line);
615 pjdlog_critical("Assertion failed: (%s), function %s, file %s, line %d.",
616 failedexpr, func, file, line);