Convert logging to strbufs

pull/5/head
Michael Spang 14 years ago
parent f5a71b6b32
commit 3476038435
  1. 2
      src/Makefile
  2. 23
      src/strbuf.c
  3. 45
      src/util.c
  4. 19
      src/util.h

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

@ -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)
{

@ -7,8 +7,7 @@
#include <errno.h>
#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;
msgp += snprintf(msgp, sizeof(message) - 2 - (msgp - message), "%s: ", prefix);
if (msgp - message > sizeof(message) - 2)
fatal("error message overflowed");
msgp += vsnprintf(msgp, sizeof(message) - 2 - (msgp - message), fmt, args);
if (msgp - message > sizeof(message) - 2)
fatal("error message overflowed");
struct strbuf msg = STRBUF_INIT;
*msgp++ = '\n';
*msgp++ = '\0';
strbuf_addf(&msg, "%s: ", prefix);
strbuf_vaddf(&msg, fmt, args);
strbuf_addch(&msg, '\n');
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;
msgp += snprintf(msgp, sizeof(message) - 2 - (msgp - message), "%s: ", prefix);
if (msgp - message > sizeof(message) - 2)
fatal("error message overflowed");
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");
struct strbuf msg = STRBUF_INIT;
*msgp++ = '\n';
*msgp++ = '\0';
strbuf_addf(&msg, "%s: ", prefix);
strbuf_vaddf(&msg, fmt, args);
strbuf_addf(&msg, ": %s\n", strerror(errno));
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) {

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

Loading…
Cancel
Save