OSSP CVS Repository

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

Check-in Number: 956
Date: 2001-Sep-12 15:05:39 (local)
2001-Sep-12 13:05:39 (UTC)
User:rse
Branch:
Comment: add support for %L (level) and %P (pid) to prefix channel
Tickets:
Inspections:
Files:
ossp-pkg/l2/l2_ch_prefix.c      1.12 -> 1.13     63 inserted, 9 deleted
ossp-pkg/l2/l2_test.c      1.17 -> 1.18     1 inserted, 1 deleted

ossp-pkg/l2/l2_ch_prefix.c 1.12 -> 1.13

--- l2_ch_prefix.c       2001/09/12 09:42:34     1.12
+++ l2_ch_prefix.c       2001/09/12 13:05:39     1.13
@@ -28,14 +28,15 @@
 */
 
 #include <time.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "l2.h"
 
 /* declare private channel configuration */
 typedef struct {
-    char *timefmt;
+    char *prefix;
     char *timezone;
-    char  timebuf[1024];
 } l2_ch_prefix_t;
 
 /* create channel */
@@ -48,7 +49,7 @@
         return L2_ERR_MEM;
 
     /* initialize configuration with reasonable defaults */
-    cfg->timefmt  = NULL;
+    cfg->prefix   = NULL;
     cfg->timezone = strdup("local");
 
     /* link private channel configuration into channel context */
@@ -65,7 +66,7 @@
     l2_result_t rv;
 
     /* feed and call generic parameter parsing engine */
-    L2_PARAM_SET(pa[0], timefmt,  STRING, &cfg->timefmt);
+    L2_PARAM_SET(pa[0], prefix,  STRING, &cfg->prefix);
     L2_PARAM_SET(pa[1], timezone, STRING, &cfg->timezone);
     L2_PARAM_END(pa[2]);
     rv = l2_util_setparams(pa, fmt, ap);
@@ -82,6 +83,52 @@
     return rv;
 }
 
+static l2_result_t expand_specials(char *buf, size_t bufsize, l2_level_t level)
+{
+    char *cpSC;
+    char *cpSE;
+    char *cpBE;
+    char caBuf[128];
+    size_t nBuf;
+    l2_level_t rv;
+    int bSubst;
+
+    cpSC = buf;             /* string current pointer */
+    cpSE = buf+strlen(buf); /* string end pointer     */
+    cpBE = buf+bufsize;     /* buffer end pointer     */
+    while (cpSC < cpSE) {
+        if ((cpSC+1) < cpSE && *cpSC == '%') {
+            bSubst = FALSE;
+            switch (*(cpSC+1)) {
+                case 'L': {
+                    if ((rv = l2_util_l2s(caBuf, sizeof(caBuf), '\0', level)) != L2_OK)
+                        return rv;
+                    bSubst = TRUE;
+                    break;
+                }
+                case 'P': {
+                    sprintf(caBuf, "%lu", (unsigned long)getpid());
+                    bSubst = TRUE;
+                    break;
+                }
+            }
+            if (bSubst) {
+                nBuf = strlen(caBuf);
+                if ((cpBE-cpSE)+2 < nBuf)
+                    return L2_ERR_MEM;
+                memmove(cpSC+nBuf, cpSC+2, cpSE-(cpSC+2)+1);
+                memmove(cpSC, caBuf, nBuf);
+                cpSC += nBuf;
+                cpSE += (-2+nBuf);
+                continue;
+            }
+        }
+        cpSC++;
+    }
+
+    return L2_OK;
+}
+
 /* write to channel */
 static l2_result_t hook_write(l2_context_t *ctx, l2_channel_t *ch,
                               l2_level_t level, const char *buf, size_t buf_size)
@@ -92,9 +139,16 @@
     struct tm *tm;
     size_t n;
     l2_result_t rv;
+    char buf1[1024];
+    char buf2[1024];
 
     /* optionally provide prefix */
-    if (cfg->timefmt != NULL) {
+    if (cfg->prefix != NULL) {
+        if (strlen(cfg->prefix) >= sizeof(buf1))
+            return L2_ERR_MEM;
+        strcpy(buf1, cfg->prefix);
+        if ((rv = expand_specials(buf1, sizeof(buf1), level)) != L2_OK)
+            return rv;
         t = time(NULL);
         if (strcmp(cfg->timezone, "local") == 0)
             tm = localtime(&t);
@@ -102,9 +156,9 @@
             tm = gmtime(&t);
         else
             return L2_ERR_ARG;
-        if ((n = strftime(cfg->timebuf, sizeof(cfg->timebuf), cfg->timefmt, tm)) == 0)
+        if ((n = strftime(buf2, sizeof(buf2), buf1, tm)) == 0)
             return L2_ERR_SYS;
-        if ((rv = l2_channel_write(downstream, level, cfg->timebuf, n)) != L2_OK)
+        if ((rv = l2_channel_write(downstream, level, buf2, n)) != L2_OK)
             return rv;
     }
 
@@ -123,8 +177,8 @@
     l2_result_t rv;
 
     /* free prefix structure */
-    if (cfg->timefmt != NULL)
-        free(cfg->timefmt);
+    if (cfg->prefix != NULL)
+        free(cfg->prefix);
     if (cfg->timezone != NULL)
         free(cfg->timezone);
     free(cfg);


ossp-pkg/l2/l2_test.c 1.17 -> 1.18

--- l2_test.c    2001/09/10 14:34:11     1.17
+++ l2_test.c    2001/09/12 13:05:39     1.18
@@ -107,7 +107,7 @@
     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)
+    if (l2_channel_configure(chPrefix, "prefix,timezone", "[%d-%m-%Y/%H:%M:%S] %L test[%P]: ", "local") != L2_OK)
         die("failed to configure prefix channel");
 
     if ((chBuf  = l2_channel_create(&l2_handler_buffer)) == NULL) /* Buffer */

CVSTrac 2.0.1