From: Ian Jackson Date: Sun, 11 May 2014 15:28:33 +0000 (+0100) Subject: log: Introduce slilog_part; abolish log_if->logfn X-Git-Tag: debian/0.3.2_beta1~18 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=commitdiff_plain;h=ff1dcd860a6176d24ad1dd7c3b8756c685ca90c6 log: Introduce slilog_part; abolish log_if->logfn [v]Message provides a facility for sending messages to the system log which are assembled out of pieces. This is quite useful. Generalise it to other logger interfaces too: * Move the functionality out of vMessage into a new function vslilog_part. Provide slilog_part too. * Move the assembly buffer: it was a static variable (used only for the system log); now it is a member of the log_if. (Yes, the log_if, not the private state, because it applies for all loggers and is used by common code ie vslilog_part.) * Initialise log_if->buff[0] everywhere. * Rename LOG_MESSAGE_BUFLEN from MESSAGE_BUFLEN since now it has to live in secnet.h. Also, remove log_if->logfn. All the call sites use vlogfn. Doing this in this patch makes it easy to see that we haven't missed any places where we should be initialising log_if->buff[0]. We currently have -Wunused ie -Wunused-functions, so for now we leave the pointless definitions of the various loggers' logfns. Signed-off-by: Ian Jackson --- diff --git a/log.c b/log.c index d330113..4aa12e7 100644 --- a/log.c +++ b/log.c @@ -30,24 +30,10 @@ static void vMessageFallback(uint32_t class, const char *message, va_list args) static void vMessage(uint32_t class, const char *message, va_list args) { -#define MESSAGE_BUFLEN 1023 - static char buff[MESSAGE_BUFLEN+1]={0,}; - size_t bp; - char *nlp; if (system_log) { /* Messages go to the system log interface */ - bp=strlen(buff); - assert(bp < MESSAGE_BUFLEN); - vsnprintf(buff+bp,MESSAGE_BUFLEN-bp,message,args); - buff[sizeof(buff)-2] = '\n'; - buff[sizeof(buff)-1] = '\0'; - /* Each line is sent separately */ - while ((nlp=strchr(buff,'\n'))) { - *nlp=0; - slilog(system_log,class,"%s",buff); - memmove(buff,nlp+1,strlen(nlp+1)+1); - } + vslilog_part(system_log, class, message, args); } else { vMessageFallback(class,message,args); } @@ -248,8 +234,8 @@ struct log_if *init_log(list_t *ll) } r=safe_malloc(sizeof(*r), "init_log"); r->st=l; - r->logfn=log_multi; r->vlogfn=log_vmulti; + r->buff[0]=0; return r; } @@ -363,8 +349,8 @@ static list_t *logfile_apply(closure_t *self, struct cloc loc, dict_t *context, st->cl.apply=NULL; st->cl.interface=&st->ops; st->ops.st=st; - st->ops.logfn=logfile_log; st->ops.vlogfn=logfile_vlog; + st->ops.buff[0]=0; st->loc=loc; st->f=NULL; @@ -485,8 +471,8 @@ static list_t *syslog_apply(closure_t *self, struct cloc loc, dict_t *context, st->cl.apply=NULL; st->cl.interface=&st->ops; st->ops.st=st; - st->ops.logfn=syslog_log; st->ops.vlogfn=syslog_vlog; + st->ops.buff[0]=0; item=list_elem(args,0); if (!item || item->type!=t_dict) diff --git a/secnet.h b/secnet.h index 29cbac7..44b180f 100644 --- a/secnet.h +++ b/secnet.h @@ -346,13 +346,15 @@ struct comm_if { /* LOG interface */ +#define LOG_MESSAGE_BUFLEN 1023 + typedef void log_msg_fn(void *st, int class, const char *message, ...); typedef void log_vmsg_fn(void *st, int class, const char *message, va_list args); struct log_if { void *st; - log_msg_fn *logfn; /* Do not call these directly - you don't get */ log_vmsg_fn *vlogfn; /* printf format checking. Use [v]slilog instead */ + char buff[LOG_MESSAGE_BUFLEN+1]; }; /* (convenience functions, defined in util.c) */ extern void slilog(struct log_if *lf, int class, const char *message, ...) @@ -360,6 +362,13 @@ FORMAT(printf,3,4); extern void vslilog(struct log_if *lf, int class, const char *message, va_list) FORMAT(printf,3,0); +/* Versions which take (parts of) (multiple) messages, using \n to + * distinguish one message from another. */ +extern void slilog_part(struct log_if *lf, int class, const char *message, ...) +FORMAT(printf,3,4); +extern void vslilog_part(struct log_if *lf, int class, const char *message, + va_list) FORMAT(printf,3,0); + /* SITE interface */ /* Pretty much a placeholder; allows starting and stopping of processing, diff --git a/util.c b/util.c index 8c23485..9a20420 100644 --- a/util.c +++ b/util.c @@ -435,3 +435,30 @@ int32_t calculate_max_start_pad(void) transform_max_start_pad + comm_max_start_pad; } + +void vslilog_part(struct log_if *lf, int priority, const char *message, va_list ap) +{ + char *buff=lf->buff; + size_t bp; + char *nlp; + + bp=strlen(buff); + assert(bp < LOG_MESSAGE_BUFLEN); + vsnprintf(buff+bp,LOG_MESSAGE_BUFLEN-bp,message,ap); + buff[LOG_MESSAGE_BUFLEN-1] = '\n'; + buff[LOG_MESSAGE_BUFLEN] = '\0'; + /* Each line is sent separately */ + while ((nlp=strchr(buff,'\n'))) { + *nlp=0; + slilog(lf,priority,"%s",buff); + memmove(buff,nlp+1,strlen(nlp+1)+1); + } +} + +extern void slilog_part(struct log_if *lf, int priority, const char *message, ...) +{ + va_list ap; + va_start(ap,message); + vslilog_part(lf,priority,message,ap); + va_end(ap); +}