properly handle va_list in log_append functions

the support of multiple appenders need to associate
one va_list per appender.
This is the purpose of this patch to copy the va_list
before to call the appender.

Signed-off-by: Michel Normand <normand@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
This commit is contained in:
Michel Normand 2009-05-15 10:43:51 +02:00 committed by Daniel Lezcano
parent 31c53c2e1a
commit 5fd8380be6
2 changed files with 17 additions and 10 deletions

View File

@ -45,7 +45,7 @@ lxc_log_define(lxc_log, lxc);
/*---------------------------------------------------------------------------*/
static int log_append_logfile(const struct lxc_log_appender *appender,
const struct lxc_log_event *event)
struct lxc_log_event *event)
{
char buffer[LXC_LOG_BUFFER_SIZE];
int n;
@ -62,10 +62,10 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
event->category);
n += vsnprintf(buffer + n, sizeof(buffer) - n, event->fmt,
event->va);
*event->vap);
if (n >= sizeof(buffer) - 1) {
WARN("truncated next event from %d to %d bytes", n,
WARN("truncated next event from %d to %zd bytes", n,
sizeof(buffer));
n = sizeof(buffer) - 1;
}

View File

@ -71,14 +71,13 @@ struct lxc_log_event {
struct timeval timestamp;
struct lxc_log_locinfo *locinfo;
const char *fmt;
va_list va;
va_list *vap;
};
/* log appender object */
struct lxc_log_appender {
const char* name;
int (*append)(const struct lxc_log_appender *,
const struct lxc_log_event *);
int (*append)(const struct lxc_log_appender *, struct lxc_log_event *);
/*
* appenders can be stacked
@ -148,17 +147,23 @@ static inline int lxc_log_priority_to_int(const char* name)
static inline void
__lxc_log_append(const struct lxc_log_appender *appender,
const struct lxc_log_event* event)
struct lxc_log_event* event)
{
va_list va, *va_keep;
va_keep = event->vap;
while (appender) {
va_copy(va, *va_keep);
event->vap = &va;
appender->append(appender, event);
appender = appender->next;
va_end(va);
}
}
static inline void
__lxc_log(const struct lxc_log_category* category,
const struct lxc_log_event* event)
struct lxc_log_event* event)
{
while (category) {
__lxc_log_append(category->appender, event);
@ -185,12 +190,14 @@ static inline void LXC_##PRIORITY(struct lxc_log_locinfo* locinfo, \
.fmt = format, \
.locinfo = locinfo \
}; \
va_list va_ref; \
\
gettimeofday(&evt.timestamp, NULL); \
\
va_start(evt.va, format); \
va_start(va_ref, format); \
evt.vap = &va_ref; \
__lxc_log(acategory, &evt); \
va_end(evt.va); \
va_end(va_ref); \
} \
}