Convert logging to strbufs
This commit is contained in:
parent
f5a71b6b32
commit
3476038435
|
@ -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)
|
||||
|
|
23
src/strbuf.c
23
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)
|
||||
{
|
||||
|
|
45
src/util.c
45
src/util.c
|
@ -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;
|
||||
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) {
|
||||
|
|
19
src/util.h
19
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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue