OSSP CVS Repository

ossp - Difference in ossp-pkg/l2/l2_stream.c versions 1.7 and 1.8
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/l2/l2_stream.c 1.7 -> 1.8

--- l2_stream.c  2001/09/03 13:43:33     1.7
+++ l2_stream.c  2001/09/04 13:52:59     1.8
@@ -61,17 +61,17 @@
     return L2_OK;
 }
 
-l2_result_t l2_stream_formatter(l2_stream_t *st, const char *name, l2_formatter_t cb, l2_context_t *ctx)
+l2_result_t l2_stream_formatter(l2_stream_t *st, char id, l2_formatter_t cb, l2_context_t *ctx)
 {
     int i;
 
-    if (st == NULL || name == NULL || cb != NULL)
+    if (st == NULL || id == '\0' || cb != NULL)
         return L2_ERROR;
     for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++)
         ;
     if (i == L2_MAX_FORMATTERS)
         return L2_ERROR;
-    st->formatters[i].name = strdup(name);
+    st->formatters[i].id   = id;
     st->formatters[i].ctx  = ctx;
     st->formatters[i].cb   = cb;
     return L2_OK;
@@ -100,12 +100,45 @@
     return rv;
 }
 
+static int l2_stream_vlog_flush(l2_util_format_t *vfmt)
+{
+    return -1;
+}
+
+static void l2_stream_vlog_format(
+    l2_util_format_t *vfmt,
+    char *cPrefix, char *cPad, char **cppOut, size_t *npOutLen,
+    char *cpBuf, int nBufLenMax, char *cpParam, char cId, va_list *apArgs)
+{
+    l2_stream_t *st = (l2_stream_t *)(vfmt->data[0].vp);
+    int i;
+
+    /* init result */
+    *cPrefix = '\0';
+    *cPad = ' ';
+    *cppOut = NULL;
+    *npOutLen = 0;
+
+    /* iterate over all configured formatters */
+    for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++) {
+        if (st->formatters[i].id == cId) {
+            if (st->formatters[i].cb(st->formatters[i].ctx, cId, cpParam, 
+                                     cpBuf, nBufLenMax, npOutLen, apArgs) == L2_OK) {
+                *cppOut = cpBuf;
+                break;
+            }
+        }
+    }
+    return;
+}
+
 l2_result_t l2_stream_vlog(l2_stream_t *st, unsigned int level, const char *fmt, va_list ap)
 {
     int i;
     int l, j;
     size_t len;
     l2_result_t rv;
+    l2_util_format_t vfmt;
 
     if (st == NULL || fmt == NULL || ap == NULL)
         return L2_ERROR;
@@ -119,9 +152,15 @@
     /* check whether level is globally enabled */
     if (!(st->levelmask & level))
         return L2_OK;
-
-    /* XXX use st->formatter!! XXX */
-    len = vsnprintf(st->message, L2_MAX_MSGSIZE, fmt, ap);
+    
+    /* format message */
+    vfmt.curpos = st->message;
+    vfmt.endpos = st->message+ L2_MAX_MSGSIZE;
+    vfmt.data[0].vp = st;
+    vfmt.flush  = l2_stream_vlog_flush;
+    vfmt.format = l2_stream_vlog_format;
+    if ((len = l2_util_format(&vfmt, fmt, ap)) == -1)
+        return L2_ERROR;
 
     rv = L2_OK;
     for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) {
@@ -141,8 +180,6 @@
         return L2_ERROR;
     for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++)
         l2_channel_destroy(st->channels[i].ch);
-    for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++)
-        free(st->formatters[i].name);
     free(st);
     return L2_OK;
 }

CVSTrac 2.0.1