From 3476038435057a6b7c51c31a12f252dea5ce6cf3 Mon Sep 17 00:00:00 2001 From: Michael Spang Date: Sat, 31 Jan 2009 01:05:41 -0500 Subject: [PATCH] Convert logging to strbufs --- src/Makefile | 2 +- src/strbuf.c | 23 +++++++++++++++++++++++ src/util.c | 45 +++++++++++++-------------------------------- src/util.h | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/Makefile b/src/Makefile index 5402b77..6eab9be 100644 --- a/src/Makefile +++ b/src/Makefile @@ -22,7 +22,7 @@ KRB5_PROGS := addmember addclub CONFIG_OBJECTS := config.o parser.o CONFIG_LDFLAGS := CONFIG_PROGS := $(OLDCEO_PROGS) $(LDAP_PROGS) $(KRB5_PROGS) $(NET_PROGS) -UTIL_OBJECTS := util.o +UTIL_OBJECTS := util.o strbuf.o UTIL_PROGS := config-test zfsaddhomedir $(CONFIG_PROGS) all: $(BIN_PROGS) $(LIB_PROGS) $(EXT_PROGS) diff --git a/src/strbuf.c b/src/strbuf.c index fff3a48..4d8bc44 100644 --- a/src/strbuf.c +++ b/src/strbuf.c @@ -312,6 +312,29 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...) strbuf_setlen(sb, sb->len + len); } +void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list args) +{ + int len; + va_list ap; + + va_copy(ap, args); + if (!strbuf_avail(sb)) + strbuf_grow(sb, 64); + len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); + + va_copy(ap, args); + if (len < 0) + die("your vsnprintf is broken"); + if (len > strbuf_avail(sb)) { + strbuf_grow(sb, len); + len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); + if (len > strbuf_avail(sb)) { + die("this should not happen, your snprintf is broken"); + } + } + strbuf_setlen(sb, sb->len + len); +} + void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context) { diff --git a/src/util.c b/src/util.c index 758e5c4..3d76ab9 100644 --- a/src/util.c +++ b/src/util.c @@ -7,8 +7,7 @@ #include #include "util.h" - -static char message[4096]; +#include "strbuf.h" static int log_stderr = 1; @@ -18,45 +17,27 @@ void init_log(const char *ident, int option, int facility) { } static void errmsg(int prio, const char *prefix, const char *fmt, va_list args) { - char *msgp = message; + struct strbuf msg = STRBUF_INIT; - msgp += snprintf(msgp, sizeof(message) - 2 - (msgp - message), "%s: ", prefix); - if (msgp - message > sizeof(message) - 2) - fatal("error message overflowed"); + strbuf_addf(&msg, "%s: ", prefix); + strbuf_vaddf(&msg, fmt, args); + strbuf_addch(&msg, '\n'); - msgp += vsnprintf(msgp, sizeof(message) - 2 - (msgp - message), fmt, args); - if (msgp - message > sizeof(message) - 2) - fatal("error message overflowed"); - - *msgp++ = '\n'; - *msgp++ = '\0'; - - syslog(prio, "%s", message); + syslog(prio, "%s", msg.buf); if (log_stderr) - fputs(message, stderr); + fputs(msg.buf, stderr); } static void errmsgpe(int prio, const char *prefix, const char *fmt, va_list args) { - char *msgp = message; + struct strbuf msg = STRBUF_INIT; - msgp += snprintf(msgp, sizeof(message) - 2 - (msgp - message), "%s: ", prefix); - if (msgp - message > sizeof(message) - 2) - fatal("error message overflowed"); + strbuf_addf(&msg, "%s: ", prefix); + strbuf_vaddf(&msg, fmt, args); + strbuf_addf(&msg, ": %s\n", strerror(errno)); - msgp += vsnprintf(msgp, sizeof(message) - 2 - (msgp - message), fmt, args); - if (msgp - message > sizeof(message) - 2) - fatal("error message overflowed"); - - msgp += snprintf(msgp, sizeof(message) - 2 - (msgp - message), ": %s", strerror(errno)); - if (msgp - message > sizeof(message) - 2) - fatal("error message overflowed"); - - *msgp++ = '\n'; - *msgp++ = '\0'; - - syslog(prio, "%s", message); + syslog(prio, "%s", msg.buf); if (log_stderr) - fputs(message, stderr); + fputs(msg.buf, stderr); } NORETURN static void die(int prio, const char *prefix, const char *msg, va_list args) { diff --git a/src/util.h b/src/util.h index 4bd00dc..3f282e2 100644 --- a/src/util.h +++ b/src/util.h @@ -38,6 +38,25 @@ static inline void *xmalloc(size_t size) { return alloc; } +static inline void *xrealloc(void *ptr, size_t size) { + void *alloc = realloc(ptr, size); + + if (alloc == NULL) + fatal("out of memory"); + + return alloc; +} + +static inline void *xcalloc(size_t nmemb, size_t size) { + void *alloc = calloc(nmemb, size); + + if (alloc == NULL) + fatal("out of memory"); + + return alloc; +} + + static inline char *xstrdup(const char *s) { char *dup = strdup(s);