OSSP CVS Repository

ossp - Check-in [864]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 864
Date: 2001-Sep-05 15:56:43 (local)
2001-Sep-05 13:56:43 (UTC)
User:rse
Branch:
Comment: Implemented prefix channel plus mini test.
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2_ch_prefix.c      1.7 -> 1.8     88 inserted, 18 deleted
ossp-pkg/l2/l2_test.c      1.11 -> 1.12     15 inserted, 4 deleted

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

--- l2_ch_prefix.c       2001/09/05 07:47:12     1.7
+++ l2_ch_prefix.c       2001/09/05 13:56:43     1.8
@@ -27,52 +27,122 @@
 **  l2_ch_prefix.c: prefixing channel implementation
 */
 
+#include <time.h>
+
 #include "l2.h"
 
+/* declare private channel configuration */
+typedef struct {
+    char *timefmt;
+    char *timezone;
+    char  timebuf[1024];
+} l2_ch_prefix_t;
+
+/* create channel */
 static l2_result_t hook_create(l2_context_t *ctx, l2_channel_t *ch)
 {
+    l2_ch_prefix_t *cfg;
+
+    /* allocate private channel configuration */
+    if ((cfg = (l2_ch_prefix_t *)malloc(sizeof(l2_ch_prefix_t))) == NULL)
+        return L2_ERROR;
+
+    /* initialize configuration with reasonable defaults */
+    cfg->timefmt  = NULL;
+    cfg->timezone = strdup("local");
+
+    /* link private channel configuration into channel context */
+    ctx->vp = cfg;
+
     return L2_OK;
 }
 
+/* configure channel */
 static l2_result_t hook_configure(l2_context_t *ctx, l2_channel_t *ch, const char *fmt, va_list ap)
 {
-    return L2_OK;
-}
+    l2_ch_prefix_t *cfg = (l2_ch_prefix_t *)ctx->vp;
+    l2_param_t pa[3]; 
+    l2_result_t rv;
+
+    /* feed and call generic parameter parsing engine */
+    L2_PARAM_SET(pa[0], timefmt,  STRING, &cfg->timefmt);
+    L2_PARAM_SET(pa[1], timezone, STRING, &cfg->timezone);
+    L2_PARAM_END(pa[2]);
+    rv = l2_util_setparams(pa, fmt, ap);
+
+    /* argument consistency check */
+    if (rv == L2_OK) {
+        if (cfg->timezone == NULL)
+            return L2_ERROR;
+        if (!(   strcmp(cfg->timezone, "local") == 0 
+              || strcmp(cfg->timezone, "utc")   == 0))
+            return L2_ERROR;
+    }
 
-static l2_result_t hook_open(l2_context_t *ctx, l2_channel_t *ch)
-{
-    return L2_OK;
+    return rv;
 }
 
+/* write to channel */
 static l2_result_t hook_write(l2_context_t *ctx, l2_channel_t *ch, 
-                      const char *buf, size_t buf_size)
+                              const char *buf, size_t buf_size)
 {
-    return L2_OK;
-}
+    l2_ch_prefix_t *cfg;
+    l2_channel_t *downstream;
+    time_t t;
+    struct tm *tm;
+    size_t n;
+    l2_result_t rv;
+
+    /* get environment */
+    cfg = (l2_ch_prefix_t *)ctx->vp;
+    downstream = l2_channel_downstream(ch);
+
+    /* optionally provide prefix */
+    if (cfg->timefmt != NULL) {
+        t = time(NULL);
+        if (strcmp(cfg->timezone, "local") == 0)
+            tm = localtime(&t);
+        else if (strcmp(cfg->timezone, "utc") == 0)
+            tm = gmtime(&t);
+        else
+            return L2_ERROR;
+        if ((n = strftime(cfg->timebuf, sizeof(cfg->timebuf), cfg->timefmt, tm)) == 0)
+            return L2_ERROR;
+        if ((rv = l2_channel_write(downstream, cfg->timebuf, n)) != L2_OK)
+            return rv;
+    }
+
+    /* write to downstream channel */
+    if ((rv = l2_channel_write(downstream, buf, buf_size)) != L2_OK)
+        return rv;
 
-static l2_result_t hook_flush(l2_context_t *ctx, l2_channel_t *ch)
-{
-    return L2_OK;
-}
-
-static l2_result_t hook_close(l2_context_t *ctx, l2_channel_t *ch)
-{
     return L2_OK;
 }
 
+/* destroy channel */
 static l2_result_t hook_destroy(l2_context_t *ctx, l2_channel_t *ch)
 {
+    l2_ch_prefix_t *cfg = (l2_ch_prefix_t *)ctx->vp;
+
+    if (cfg->timefmt != NULL)
+        free(cfg->timefmt);
+    if (cfg->timezone != NULL)
+        free(cfg->timezone);
+    free(cfg);
+    ctx->vp = NULL;
+
     return L2_OK;
 }
 
+/* exported channel handler structure */
 l2_handler_t l2_handler_prefix = {
     L2_CHANNEL_FILTER,
     hook_create,
     hook_configure,
-    hook_open,
+    NULL,
     hook_write,
-    hook_flush,
-    hook_close,
+    NULL,
+    NULL,
     hook_destroy
 };
 


ossp-pkg/l2/l2_test.c 1.11 -> 1.12

--- l2_test.c    2001/09/04 19:35:20     1.11
+++ l2_test.c    2001/09/05 13:56:43     1.12
@@ -62,6 +62,7 @@
 
 int main(int argc, char *argv[])
 {
+    l2_channel_t *chPrefix;
     l2_channel_t *chBuf;
     l2_channel_t *chFile;
     l2_channel_t *chSock;
@@ -94,6 +95,13 @@
     if (l2_stream_channel(st, chSock, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
         die("failed to attach first channel into stream");
 */
+
+    if ((chPrefix  = l2_channel_create(&l2_handler_prefix)) == NULL) /* Prefix */
+        die("failed to create prefix channel");
+
+    if (l2_channel_configure(chPrefix, "timefmt,timezone", "[%d-%m-%Y/%H:%M:%S] ", "local") != L2_OK)
+        die("failed to configure prefix channel");
+
     if ((chBuf  = l2_channel_create(&l2_handler_buffer)) == NULL) /* Buffer */
         die("failed to create buffer channel");
 
@@ -108,12 +116,15 @@
 
     if (l2_channel_stack(chFile, chBuf) != L2_OK)
         die("failed to stack buffer channel on top of file channel");
+    
+    if (l2_channel_stack(chBuf, chPrefix) != L2_OK)
+        die("failed to stack prefix channel on top of buffer channel");
 
-    if (l2_channel_open(chBuf) != L2_OK)
-        die("failed to open buffer channel");
+    if (l2_channel_open(chPrefix) != L2_OK)
+        die("failed to open channel stack");
 
-    if (l2_stream_channel(st, chBuf, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
-        die("failed to attach second channel into stream");
+    if (l2_stream_channel(st, chPrefix, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
+        die("failed to attach channel stack into stream");
 
     if (l2_stream_levels(st, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
         die("failed to set global logging level");

CVSTrac 2.0.1