- 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
This commit is contained in:
pjd 2011-03-07 10:38:18 +00:00
parent c22d549a36
commit 76c47e936b
3 changed files with 92 additions and 2 deletions
sbin

@ -19,6 +19,7 @@ SRCS+= subr.c
SRCS+= y.tab.h
MAN= hastctl.8
NO_WFORMAT=
CFLAGS+=-I${.CURDIR}/../hastd
CFLAGS+=-DINET
.if ${MK_INET6_SUPPORT} != "no"
@ -28,8 +29,8 @@ CFLAGS+=-DINET6
CFLAGS+=-DYY_NO_UNPUT
CFLAGS+=-DYY_NO_INPUT
DPADD= ${LIBL}
LDADD= -ll
DPADD= ${LIBL} ${LIBUTIL}
LDADD= -ll -lutil
.if ${MK_OPENSSL} != "no"
DPADD+= ${LIBCRYPTO}
LDADD+= -lcrypto

@ -19,6 +19,7 @@ SRCS+= token.l
SRCS+= y.tab.h
MAN= hastd.8 hast.conf.5
NO_WFORMAT=
CFLAGS+=-I${.CURDIR}
CFLAGS+=-DINET
.if ${MK_INET6_SUPPORT} != "no"

@ -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;