diff options
author | pjd <pjd@FreeBSD.org> | 2011-03-07 10:38:18 +0000 |
---|---|---|
committer | pjd <pjd@FreeBSD.org> | 2011-03-07 10:38:18 +0000 |
commit | 76c47e936ba76108f5c9de55dc5c108264be0a3e (patch) | |
tree | eff79e52e39056b898c7ca3f57a73abe6ef739ca /sbin/hastd/pjdlog.c | |
parent | c22d549a366f24694675b1e30c79ba1beea2ae36 (diff) | |
download | FreeBSD-src-76c47e936ba76108f5c9de55dc5c108264be0a3e.zip FreeBSD-src-76c47e936ba76108f5c9de55dc5c108264be0a3e.tar.gz |
- Turn on printf extentions.
- Load support for %T for pritning time.
- Add support for %N for printing number in human readable form.
- Add support for %S for printing sockaddr structure (currently only AF_INET
family is supported, as this is all we need in HAST).
- Disable gcc compile-time format checking as this will no longer work.
MFC after: 2 weeks
Diffstat (limited to 'sbin/hastd/pjdlog.c')
-rw-r--r-- | sbin/hastd/pjdlog.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/sbin/hastd/pjdlog.c b/sbin/hastd/pjdlog.c index 664bd13..24c5ad4 100644 --- a/sbin/hastd/pjdlog.c +++ b/sbin/hastd/pjdlog.c @@ -31,9 +31,15 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#include <sys/socket.h> +#include <netinet/in.h> + #include <assert.h> #include <errno.h> +#include <libutil.h> +#include <printf.h> #include <stdarg.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -49,6 +55,77 @@ static int pjdlog_initialized = PJDLOG_NEVER_INITIALIZED; static int pjdlog_mode, pjdlog_debug_level; static char pjdlog_prefix[128]; +static int +pjdlog_printf_arginfo_humanized_number(const struct printf_info *pi __unused, + size_t n, int *argt) +{ + + assert(n >= 1); + argt[0] = PA_INT | PA_FLAG_INTMAX; + return (1); +} + +static int +pjdlog_printf_render_humanized_number(struct __printf_io *io, + const struct printf_info *pi, const void * const *arg) +{ + char buf[5]; + intmax_t num; + int ret; + + num = *(const intmax_t *)arg[0]; + humanize_number(buf, sizeof(buf), (int64_t)num, "", HN_AUTOSCALE, + HN_NOSPACE | HN_DECIMAL); + ret = __printf_out(io, pi, buf, strlen(buf)); + __printf_flush(io); + return (ret); +} + +static int +pjdlog_printf_arginfo_sockaddr(const struct printf_info *pi __unused, + size_t n, int *argt) +{ + + assert(n >= 1); + argt[0] = PA_POINTER; + return (1); +} + +static int +pjdlog_printf_render_sockaddr(struct __printf_io *io, + const struct printf_info *pi, const void * const *arg) +{ + const struct sockaddr *sa; + char buf[64]; + int ret; + + sa = *(const struct sockaddr * const *)arg[0]; + switch (sa->sa_family) { + case AF_INET: + { + const struct sockaddr_in *sin; + in_addr_t ip; + unsigned int port; + + sin = (const struct sockaddr_in *)sa; + ip = ntohl(sin->sin_addr.s_addr); + port = ntohs(sin->sin_port); + + snprintf(buf, sizeof(buf), "%u.%u.%u.%u:%u", + ((ip >> 24) & 0xff), ((ip >> 16) & 0xff), + ((ip >> 8) & 0xff), (ip & 0xff), port); + break; + } + default: + snprintf(buf, sizeof(buf), "[unsupported family %u]", + (unsigned int)sa->sa_family); + break; + } + ret = __printf_out(io, pi, buf, strlen(buf)); + __printf_flush(io); + return (ret); +} + void pjdlog_init(int mode) { @@ -57,6 +134,17 @@ pjdlog_init(int mode) pjdlog_initialized == PJDLOG_NOT_INITIALIZED); assert(mode == PJDLOG_MODE_STD || mode == PJDLOG_MODE_SYSLOG); + if (pjdlog_initialized == PJDLOG_NEVER_INITIALIZED) { + __use_xprintf = 1; + register_printf_render_std("T"); + register_printf_render('N', + pjdlog_printf_render_humanized_number, + pjdlog_printf_arginfo_humanized_number); + register_printf_render('S', + pjdlog_printf_render_sockaddr, + pjdlog_printf_arginfo_sockaddr); + } + if (mode == PJDLOG_MODE_SYSLOG) openlog(NULL, LOG_PID | LOG_NDELAY, LOG_DAEMON); pjdlog_mode = mode; |