OSSP CVS Repository

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

ossp-pkg/l2/l2_test.c 1.5 -> 1.6

--- l2_test.c    2001/09/02 15:38:52     1.5
+++ l2_test.c    2001/09/03 13:43:33     1.6
@@ -30,67 +30,61 @@
 #include <stdio.h>
 #include "l2.h"
 
+static void die(char *fmt, ...)
+{
+    va_list ap;
+
+    va_start(ap, fmt);
+    fprintf(stderr, "l2_test:ERROR: ");
+    vfprintf(stderr, fmt, ap);
+    fprintf(stderr, "\n");
+    va_end(ap);
+    exit(1);
+}
+
 int main(int argc, char *argv[])
-    {
-    l2_channel_t* log_channel_buf;
-    l2_channel_t* log_channel_file;
-    l2_stream_t*  log_stream;
-
-    /* Create the channel. */
-
-    setbuf(stdout, 0);
-    printf("Creating channel ... ");
-    log_channel_buf = l2_channel_create(&l2_handler_buffer);
-    log_channel_file = l2_channel_create(&l2_handler_file);
-    if ((log_channel_buf == 0) || (log_channel_file == 0))
-        {
-        fprintf(stderr, "Can't create my log channels!\n");
-        return 1;
-        }
-    printf("done\n");
-
-    l2_channel_configure(log_channel_buf, "size", 100);
-    l2_channel_configure(log_channel_file, "path", "myfilelog", "append",\
-        TRUE, "perm", 0x644);
-
-    l2_channel_open(log_channel_buf);
-    l2_channel_open(log_channel_file);
-
-    /* Create the stream and attach the channel to it. */
-
-    printf("Creating stream ... ");
-    log_stream = l2_stream_create();
-    if (log_stream == 0)
-        {
-        fprintf(stderr, "Can't create my log stream!\n");
-        return 1;
-        }
-    printf("done\n");
-    printf("Attaching channel to stream ... ");
-    if (!l2_stream_channel(log_stream, log_channel_buf, -1))
-        {
-        fprintf(stderr, "Can't attach buffer channel to log stream!\n");
-        return 1;
-        }
-    if (!l2_stream_channel(log_stream, log_channel_file, -1))
-        {
-        fprintf(stderr, "Can't attach file channel to log stream!\n");
-        return 1;
-        }
-    printf("done\n");
-
-    /* Log an example message. */
-
-    printf("Writing log message ... ");
-    l2_stream_log(log_stream, -1, "%cstream = 0x%lx, channel = 0x%lx%c\n", '"',\
-        log_stream, log_channel_buf, '"');
-    printf("done\n");
-
-    /* Clean up. */
-
-    printf("Destroying stream ... ");
-    l2_stream_destroy(log_stream);
-    printf("done\n");
+{
+    l2_channel_t *chBuf;
+    l2_channel_t *chFile;
+    l2_stream_t *st;
+
+    /*
+     * Typical steps to use a buffered file logging stream
+     */
+
+    if ((chBuf  = l2_channel_create(&l2_handler_buffer)) == NULL)
+        die("failed to create buffer channel");
+
+    if ((chFile = l2_channel_create(&l2_handler_file)) == NULL)
+        die("failed to create file channel");
+
+    if (l2_channel_configure(chBuf, "size", 100) != L2_OK)
+        die("failed to configure buffer channel");
+
+    if (l2_channel_configure(chFile, "path,append,perm", "l2_test.log", TRUE, 0644) != L2_OK)
+        die("failed to configure file channel");
+
+    if (l2_channel_stack(chFile, chBuf) != L2_OK)
+        die("failed to stack buffer channel on top of file channel");
+
+    if (l2_channel_open(chBuf) != L2_OK)
+        die("failed to open buffer channel");
+
+    if ((st = l2_stream_create()) == NULL)
+        die("failed to create stream");
+
+    if (l2_stream_channel(st, chBuf, L2_LEVEL_UPTO(L2_LEVEL_WARNING)) != L2_OK)
+        die("failed to attach channel into stream");
+
+    if (l2_stream_levels(st, L2_LEVEL_UPTO(L2_LEVEL_WARNING), NULL) != L2_OK)
+        die("failed to set global logging level");
+    
+    if (l2_stream_log(st, L2_LEVEL_PANIC, "test %s", "foo") != L2_OK)
+        die("failed to log message to stream");
+
+    if (l2_stream_destroy(st) != L2_OK)
+        die("failed to destroy stream");
 
     return 0;
-    }
+}
+

CVSTrac 2.0.1