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)
153 assert(pjdlog_initialized == PJDLOG_NEVER_INITIALIZED ||
154 pjdlog_initialized == PJDLOG_NOT_INITIALIZED);
155 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
159 if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) {
161 register_printf_render_std("T");
162 register_printf_render('N',
163 pjdlog_printf_render_humanized_number,
164 pjdlog_printf_arginfo_humanized_number);
165 register_printf_render('S',
166 pjdlog_printf_render_sockaddr,
167 pjdlog_printf_arginfo_sockaddr);
170 if (mode == PJDLOG_MODE_SYSLOG)
171 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
173 pjdlog_debug_level = 0;
174 bzero(pjdlog_prefix, sizeof(pjdlog_prefix));
176 pjdlog_initialized = PJDLOG_INITIALIZED;
186 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
190 if (pjdlog_mode == PJDLOG_MODE_SYSLOG)
193 pjdlog_initialized = PJDLOG_NOT_INITIALIZED;
199 * Configure where the logs should go.
200 * By default they are send to stdout/stderr, but after going into background
201 * (eg. by calling daemon(3)) application is responsible for changing mode to
202 * PJDLOG_MODE_SYSLOG, so logs will be send to syslog.
205 pjdlog_mode_set(int mode)
209 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
210 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
212 if (pjdlog_mode == mode)
217 if (mode == PJDLOG_MODE_SYSLOG)
218 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
219 else /* if (mode == PJDLOG_MODE_STD) */
228 * Return current mode.
231 pjdlog_mode_get(void)
234 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
236 return (pjdlog_mode);
240 * Set debug level. All the logs above the level specified here will be
244 pjdlog_debug_set(int level)
247 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
250 pjdlog_debug_level = level;
254 * Return current debug level.
257 pjdlog_debug_get(void)
260 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
262 return (pjdlog_debug_level);
266 * Set prefix that will be used before each log.
267 * Setting prefix to NULL will remove it.
270 pjdlog_prefix_set(const char *fmt, ...)
274 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
277 pjdlogv_prefix_set(fmt, ap);
282 * Set prefix that will be used before each log.
283 * Setting prefix to NULL will remove it.
286 pjdlogv_prefix_set(const char *fmt, va_list ap)
290 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
295 vsnprintf(pjdlog_prefix, sizeof(pjdlog_prefix), fmt, ap);
301 * Convert log level into string.
304 pjdlog_level_string(int loglevel)
325 assert(!"Invalid log level.");
326 abort(); /* XXX: gcc */
330 * Common log routine.
333 pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt, ...)
337 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
340 pjdlogv_common(loglevel, debuglevel, error, fmt, ap);
345 * Common log routine, which can handle regular log level as well as debug
346 * level. We decide here where to send the logs (stdout/stderr or syslog).
349 pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
354 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
355 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
356 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
357 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
358 loglevel == LOG_INFO || loglevel == LOG_DEBUG);
359 assert(loglevel != LOG_DEBUG || debuglevel > 0);
362 /* Ignore debug above configured level. */
363 if (loglevel == LOG_DEBUG && debuglevel > pjdlog_debug_level)
368 switch (pjdlog_mode) {
369 case PJDLOG_MODE_STD:
374 * We send errors and warning to stderr and the rest to stdout.
390 assert(!"Invalid loglevel.");
391 abort(); /* XXX: gcc */
394 fprintf(out, "[%s]", pjdlog_level_string(loglevel));
395 /* Attach debuglevel if this is debug log. */
396 if (loglevel == LOG_DEBUG)
397 fprintf(out, "[%d]", debuglevel);
398 fprintf(out, " %s", pjdlog_prefix);
399 vfprintf(out, fmt, ap);
401 fprintf(out, ": %s.", strerror(error));
406 case PJDLOG_MODE_SYSLOG:
411 len = snprintf(log, sizeof(log), "%s", pjdlog_prefix);
412 if ((size_t)len < sizeof(log))
413 len += vsnprintf(log + len, sizeof(log) - len, fmt, ap);
414 if (error != -1 && (size_t)len < sizeof(log)) {
415 (void)snprintf(log + len, sizeof(log) - len, ": %s.",
418 syslog(loglevel, "%s", log);
422 assert(!"Invalid mode.");
432 pjdlogv(int loglevel, const char *fmt, va_list ap)
435 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
437 /* LOG_DEBUG is invalid here, pjdlogv?_debug() should be used. */
438 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
439 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
440 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
441 loglevel == LOG_INFO);
443 pjdlogv_common(loglevel, 0, -1, fmt, ap);
450 pjdlog(int loglevel, const char *fmt, ...)
454 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
457 pjdlogv(loglevel, fmt, ap);
465 pjdlogv_debug(int debuglevel, const char *fmt, va_list ap)
468 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
470 pjdlogv_common(LOG_DEBUG, debuglevel, -1, fmt, ap);
477 pjdlog_debug(int debuglevel, const char *fmt, ...)
481 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
484 pjdlogv_debug(debuglevel, fmt, ap);
489 * Error logs with errno logging.
492 pjdlogv_errno(int loglevel, const char *fmt, va_list ap)
495 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
497 pjdlogv_common(loglevel, 0, errno, fmt, ap);
501 * Error logs with errno logging.
504 pjdlog_errno(int loglevel, const char *fmt, ...)
508 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
511 pjdlogv_errno(loglevel, fmt, ap);
516 * Log error, errno and exit.
519 pjdlogv_exit(int exitcode, const char *fmt, va_list ap)
522 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
524 pjdlogv_errno(LOG_ERR, fmt, ap);
530 * Log error, errno and exit.
533 pjdlog_exit(int exitcode, const char *fmt, ...)
537 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
540 pjdlogv_exit(exitcode, fmt, ap);
546 * Log error and exit.
549 pjdlogv_exitx(int exitcode, const char *fmt, va_list ap)
552 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
554 pjdlogv(LOG_ERR, fmt, ap);
560 * Log error and exit.
563 pjdlog_exitx(int exitcode, const char *fmt, ...)
567 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
570 pjdlogv_exitx(exitcode, fmt, ap);
576 * Log failure message and exit.
579 pjdlog_abort(const char *func, const char *file, int line,
580 const char *failedexpr, const char *fmt, ...)
584 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
587 * When there is no message we pass __func__ as 'fmt'.
588 * It would be cleaner to pass NULL or "", but gcc generates a warning
593 pjdlogv_critical(fmt, ap);
596 if (failedexpr == NULL) {
598 pjdlog_critical("Aborted at file %s, line %d.", file,
601 pjdlog_critical("Aborted at function %s, file %s, line %d.",
606 pjdlog_critical("Assertion failed: (%s), file %s, line %d.",
607 failedexpr, file, line);
609 pjdlog_critical("Assertion failed: (%s), function %s, file %s, line %d.",
610 failedexpr, func, file, line);