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/socket.h>
35 #include <netinet/in.h>
50 #define PJDLOG_NEVER_INITIALIZED 0
51 #define PJDLOG_NOT_INITIALIZED 1
52 #define PJDLOG_INITIALIZED 2
54 static int pjdlog_initialized = PJDLOG_NEVER_INITIALIZED;
55 static int pjdlog_mode, pjdlog_debug_level;
56 static char pjdlog_prefix[128];
59 pjdlog_printf_arginfo_humanized_number(const struct printf_info *pi __unused,
64 argt[0] = PA_INT | PA_FLAG_INTMAX;
69 pjdlog_printf_render_humanized_number(struct __printf_io *io,
70 const struct printf_info *pi, const void * const *arg)
76 num = *(const intmax_t *)arg[0];
77 humanize_number(buf, sizeof(buf), (int64_t)num, "", HN_AUTOSCALE,
78 HN_NOSPACE | HN_DECIMAL);
79 ret = __printf_out(io, pi, buf, strlen(buf));
85 pjdlog_printf_arginfo_sockaddr(const struct printf_info *pi __unused,
95 pjdlog_printf_render_sockaddr(struct __printf_io *io,
96 const struct printf_info *pi, const void * const *arg)
98 const struct sockaddr_storage *ss;
102 ss = *(const struct sockaddr_storage * const *)arg[0];
103 switch (ss->ss_family) {
106 const struct sockaddr_in *sin;
110 sin = (const struct sockaddr_in *)ss;
111 ip = ntohl(sin->sin_addr.s_addr);
112 port = ntohs(sin->sin_port);
114 snprintf(buf, sizeof(buf), "%u.%u.%u.%u:%u",
115 ((ip >> 24) & 0xff), ((ip >> 16) & 0xff),
116 ((ip >> 8) & 0xff), (ip & 0xff), port);
120 snprintf(buf, sizeof(buf), "[unsupported family %u]",
121 (unsigned int)ss->ss_family);
124 ret = __printf_out(io, pi, buf, strlen(buf));
130 pjdlog_init(int mode)
133 assert(pjdlog_initialized == PJDLOG_NEVER_INITIALIZED ||
134 pjdlog_initialized == PJDLOG_NOT_INITIALIZED);
135 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
137 if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) {
139 register_printf_render_std("T");
140 register_printf_render('N',
141 pjdlog_printf_render_humanized_number,
142 pjdlog_printf_arginfo_humanized_number);
143 register_printf_render('S',
144 pjdlog_printf_render_sockaddr,
145 pjdlog_printf_arginfo_sockaddr);
148 if (mode == PJDLOG_MODE_SYSLOG)
149 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
151 pjdlog_debug_level = 0;
152 bzero(pjdlog_prefix, sizeof(pjdlog_prefix));
154 pjdlog_initialized = PJDLOG_INITIALIZED;
161 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
163 if (pjdlog_mode == PJDLOG_MODE_SYSLOG)
166 pjdlog_initialized = PJDLOG_NOT_INITIALIZED;
170 * Configure where the logs should go.
171 * By default they are send to stdout/stderr, but after going into background
172 * (eg. by calling daemon(3)) application is responsible for changing mode to
173 * PJDLOG_MODE_SYSLOG, so logs will be send to syslog.
176 pjdlog_mode_set(int mode)
179 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
180 assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG);
182 if (pjdlog_mode == mode)
185 if (mode == PJDLOG_MODE_SYSLOG)
186 openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON);
187 else /* if (mode == PJDLOG_MODE_STD) */
194 * Return current mode.
197 pjdlog_mode_get(void)
200 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
202 return (pjdlog_mode);
206 * Set debug level. All the logs above the level specified here will be
210 pjdlog_debug_set(int level)
213 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
216 pjdlog_debug_level = level;
220 * Return current debug level.
223 pjdlog_debug_get(void)
226 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
228 return (pjdlog_debug_level);
232 * Set prefix that will be used before each log.
233 * Setting prefix to NULL will remove it.
236 pjdlog_prefix_set(const char *fmt, ...)
240 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
243 pjdlogv_prefix_set(fmt, ap);
248 * Set prefix that will be used before each log.
249 * Setting prefix to NULL will remove it.
252 pjdlogv_prefix_set(const char *fmt, va_list ap)
255 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
258 vsnprintf(pjdlog_prefix, sizeof(pjdlog_prefix), fmt, ap);
262 * Convert log level into string.
265 pjdlog_level_string(int loglevel)
286 assert(!"Invalid log level.");
287 abort(); /* XXX: gcc */
291 * Common log routine.
294 pjdlog_common(int loglevel, int debuglevel, int error, const char *fmt, ...)
298 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
301 pjdlogv_common(loglevel, debuglevel, error, fmt, ap);
306 * Common log routine, which can handle regular log level as well as debug
307 * level. We decide here where to send the logs (stdout/stderr or syslog).
310 pjdlogv_common(int loglevel, int debuglevel, int error, const char *fmt,
314 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
315 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
316 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
317 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
318 loglevel == LOG_INFO || loglevel == LOG_DEBUG);
319 assert(loglevel != LOG_DEBUG || debuglevel > 0);
322 /* Ignore debug above configured level. */
323 if (loglevel == LOG_DEBUG && debuglevel > pjdlog_debug_level)
326 switch (pjdlog_mode) {
327 case PJDLOG_MODE_STD:
332 * We send errors and warning to stderr and the rest to stdout.
348 assert(!"Invalid loglevel.");
349 abort(); /* XXX: gcc */
352 fprintf(out, "[%s]", pjdlog_level_string(loglevel));
353 /* Attach debuglevel if this is debug log. */
354 if (loglevel == LOG_DEBUG)
355 fprintf(out, "[%d]", debuglevel);
356 fprintf(out, " %s", pjdlog_prefix);
357 vfprintf(out, fmt, ap);
359 fprintf(out, ": %s.", strerror(error));
364 case PJDLOG_MODE_SYSLOG:
369 len = snprintf(log, sizeof(log), "%s", pjdlog_prefix);
370 if ((size_t)len < sizeof(log))
371 len += vsnprintf(log + len, sizeof(log) - len, fmt, ap);
372 if (error != -1 && (size_t)len < sizeof(log)) {
373 (void)snprintf(log + len, sizeof(log) - len, ": %s.",
376 syslog(loglevel, "%s", log);
380 assert(!"Invalid mode.");
388 pjdlogv(int loglevel, const char *fmt, va_list ap)
391 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
393 /* LOG_DEBUG is invalid here, pjdlogv?_debug() should be used. */
394 assert(loglevel == LOG_EMERG || loglevel == LOG_ALERT ||
395 loglevel == LOG_CRIT || loglevel == LOG_ERR ||
396 loglevel == LOG_WARNING || loglevel == LOG_NOTICE ||
397 loglevel == LOG_INFO);
399 pjdlogv_common(loglevel, 0, -1, fmt, ap);
406 pjdlog(int loglevel, const char *fmt, ...)
410 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
413 pjdlogv(loglevel, fmt, ap);
421 pjdlogv_debug(int debuglevel, const char *fmt, va_list ap)
424 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
426 pjdlogv_common(LOG_DEBUG, debuglevel, -1, fmt, ap);
433 pjdlog_debug(int debuglevel, const char *fmt, ...)
437 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
440 pjdlogv_debug(debuglevel, fmt, ap);
445 * Error logs with errno logging.
448 pjdlogv_errno(int loglevel, const char *fmt, va_list ap)
451 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
453 pjdlogv_common(loglevel, 0, errno, fmt, ap);
457 * Error logs with errno logging.
460 pjdlog_errno(int loglevel, const char *fmt, ...)
464 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
467 pjdlogv_errno(loglevel, fmt, ap);
472 * Log error, errno and exit.
475 pjdlogv_exit(int exitcode, const char *fmt, va_list ap)
478 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
480 pjdlogv_errno(LOG_ERR, fmt, ap);
486 * Log error, errno and exit.
489 pjdlog_exit(int exitcode, const char *fmt, ...)
493 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
496 pjdlogv_exit(exitcode, fmt, ap);
502 * Log error and exit.
505 pjdlogv_exitx(int exitcode, const char *fmt, va_list ap)
508 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
510 pjdlogv(LOG_ERR, fmt, ap);
516 * Log error and exit.
519 pjdlog_exitx(int exitcode, const char *fmt, ...)
523 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
526 pjdlogv_exitx(exitcode, fmt, ap);
532 * Log failure message and exit.
535 pjdlog_abort(const char *func, const char *file, int line,
536 const char *failedexpr, const char *fmt, ...)
540 assert(pjdlog_initialized == PJDLOG_INITIALIZED);
543 * When there is no message we pass __func__ as 'fmt'.
544 * It would be cleaner to pass NULL or "", but gcc generates a warning
549 pjdlogv_critical(fmt, ap);
552 if (failedexpr == NULL) {
554 pjdlog_critical("Aborted at file %s, line %d.", file,
557 pjdlog_critical("Aborted at function %s, file %s, line %d.",
562 pjdlog_critical("Assertion failed: (%s), file %s, line %d.",
563 failedexpr, file, line);
565 pjdlog_critical("Assertion failed: (%s), function %s, file %s, line %d.",
566 failedexpr, func, file, line);