[PATCH 1/8] log: Introduce slilog_part; abolish log_if->logfn
Ian Jackson
ijackson at chiark.greenend.org.uk
Wed May 14 21:00:21 BST 2014
[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 at chiark.greenend.org.uk>
---
log.c | 22 ++++------------------
secnet.h | 11 ++++++++++-
util.c | 27 +++++++++++++++++++++++++++
3 files changed, 41 insertions(+), 19 deletions(-)
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 d5663ff..58624a7 100644
--- a/secnet.h
+++ b/secnet.h
@@ -343,13 +343,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, ...)
@@ -357,6 +359,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);
+}
--
1.7.10.4
More information about the sgo-software-discuss
mailing list