chiark / gitweb /
log: Introduce slilog_part; abolish log_if->logfn
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 11 May 2014 15:28:33 +0000 (16:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 15 May 2014 01:02:13 +0000 (02:02 +0100)
[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 <ijackson@chiark.greenend.org.uk>
log.c
secnet.h
util.c

diff --git a/log.c b/log.c
index d330113f0c1b8fe0255eab9ea60f87c37827686c..4aa12e753d47abaa8e5ef0a6979a13d275e15c6c 100644 (file)
--- 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)
index 29cbac79c6fdd3247c2d982da9db89e04847c829..44b180f043d04baa27ac6cc8155f13cbc69067e6 100644 (file)
--- 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 8c23485c059581cbb2aef2c1d8c40628dbaeecaa..9a20420e38de1de999f0016cb600ab90dc21799d 100644 (file)
--- 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);
+}