Index: ossp-pkg/l2/l2.h RCS File: /v/ossp/cvs/ossp-pkg/l2/Attic/l2.h,v rcsdiff -q -kk '-r1.11' '-r1.12' -u '/v/ossp/cvs/ossp-pkg/l2/Attic/l2.h,v' 2>/dev/null --- l2.h 2001/09/03 12:16:44 1.11 +++ l2.h 2001/09/03 13:43:33 1.12 @@ -61,18 +61,22 @@ typedef struct l2_stream_st l2_stream_t; typedef struct l2_channel_st l2_channel_t; -/* list of logging levels */ +/* list of logging levels (high to low priority; low to high amount of logging) */ typedef enum { - L2_LEVEL_DEBUG, - L2_LEVEL_TRACE, - L2_LEVEL_INFO, - L2_LEVEL_NOTICE, - L2_LEVEL_WARNING, - L2_LEVEL_ERROR, - L2_LEVEL_CRITICAL, - L2_LEVEL_PANIC + L2_LEVEL_PANIC = (1 << 0), + L2_LEVEL_CRITICAL = (1 << 1), + L2_LEVEL_ERROR = (1 << 2), + L2_LEVEL_WARNING = (1 << 3), + L2_LEVEL_NOTICE = (1 << 4), + L2_LEVEL_INFO = (1 << 5), + L2_LEVEL_TRACE = (1 << 6), + L2_LEVEL_DEBUG = (1 << 7) } l2_level_t; +/* all levels from highest (PANIC) to and including a particular low level */ +#define L2_LEVEL_UPTO(level) \ + (((level)-1)|(level)) + /* list of return values */ typedef enum { L2_OK, @@ -154,14 +158,14 @@ /* channel operations */ l2_channel_t *l2_channel_create (l2_handler_t *h); -int l2_channel_configure(l2_channel_t *ch, const char *fmt, ...); -int l2_channel_open (l2_channel_t *ch); -int l2_channel_write (l2_channel_t *ch, const char *buf, size_t bufsize); -int l2_channel_flush (l2_channel_t *ch); -int l2_channel_close (l2_channel_t *ch); -int l2_channel_destroy (l2_channel_t *ch); -l2_channel_t *l2_channel_stack (l2_channel_t *ch1, l2_channel_t *ch2); -int l2_channel_setparams(l2_param_t p[], const char *fmt, va_list ap); +l2_result_t l2_channel_configure(l2_channel_t *ch, const char *fmt, ...); +l2_result_t l2_channel_open (l2_channel_t *ch); +l2_result_t l2_channel_write (l2_channel_t *ch, const char *buf, size_t bufsize); +l2_result_t l2_channel_flush (l2_channel_t *ch); +l2_result_t l2_channel_close (l2_channel_t *ch); +l2_result_t l2_channel_destroy (l2_channel_t *ch); +l2_result_t l2_channel_stack (l2_channel_t *ch, l2_channel_t *chTop); +l2_result_t l2_channel_setparams(l2_param_t p[], const char *fmt, va_list ap); /* stream operations */ l2_stream_t *l2_stream_create (void); Index: ossp-pkg/l2/l2_channel.c RCS File: /v/ossp/cvs/ossp-pkg/l2/l2_channel.c,v rcsdiff -q -kk '-r1.9' '-r1.10' -u '/v/ossp/cvs/ossp-pkg/l2/l2_channel.c,v' 2>/dev/null --- l2_channel.c 2001/09/03 11:50:25 1.9 +++ l2_channel.c 2001/09/03 13:43:33 1.10 @@ -52,20 +52,20 @@ return ch; } -l2_channel_t *l2_channel_stack(l2_channel_t *ch1, l2_channel_t *ch2) +l2_result_t l2_channel_stack(l2_channel_t *ch, l2_channel_t *chTop) { - if (ch1 == NULL || ch2 == NULL) - return NULL; - if ( ch1->state != L2_CHSTATE_CREATED - || ch2->state != L2_CHSTATE_CREATED) - return NULL; - ch1->downstream = ch2; - return ch1; + if (ch == NULL || chTop == NULL) + return L2_ERROR; + if ( ch->state != L2_CHSTATE_CREATED + || chTop->state != L2_CHSTATE_CREATED) + return L2_ERROR; + chTop->downstream = ch; + return L2_OK; } -int l2_channel_configure(l2_channel_t *ch, const char *fmt, ...) +l2_result_t l2_channel_configure(l2_channel_t *ch, const char *fmt, ...) { - int rv; + l2_result_t rv; va_list ap; if (ch == NULL || fmt == NULL) @@ -78,7 +78,7 @@ return rv; } -int l2_channel_open(l2_channel_t *ch) +l2_result_t l2_channel_open(l2_channel_t *ch) { l2_result_t rv; @@ -96,7 +96,7 @@ return rv; } -int l2_channel_write(l2_channel_t *ch, const char *buf, size_t bufsize) +l2_result_t l2_channel_write(l2_channel_t *ch, const char *buf, size_t bufsize) { l2_result_t rv; @@ -116,7 +116,7 @@ return rv; } -int l2_channel_flush(l2_channel_t *ch) +l2_result_t l2_channel_flush(l2_channel_t *ch) { l2_result_t rv; @@ -132,7 +132,7 @@ return rv; } -int l2_channel_close(l2_channel_t *ch) +l2_result_t l2_channel_close(l2_channel_t *ch) { l2_result_t rv; @@ -150,7 +150,7 @@ return rv; } -int l2_channel_destroy(l2_channel_t *ch) +l2_result_t l2_channel_destroy(l2_channel_t *ch) { l2_result_t rv; @@ -169,7 +169,7 @@ return rv; } -int l2_channel_setparams(l2_param_t pa[], const char *fmt, va_list ap) +l2_result_t l2_channel_setparams(l2_param_t pa[], const char *fmt, va_list ap) { const char *cpB, *cpE; const char *cpC, *cpG; Index: ossp-pkg/l2/l2_p.h RCS File: /v/ossp/cvs/ossp-pkg/l2/l2_p.h,v rcsdiff -q -kk '-r1.8' '-r1.9' -u '/v/ossp/cvs/ossp-pkg/l2/l2_p.h,v' 2>/dev/null --- l2_p.h 2001/09/03 12:16:44 1.8 +++ l2_p.h 2001/09/03 13:43:33 1.9 @@ -49,17 +49,22 @@ l2_handler_t handler; }; -typedef struct l2_format_st { +typedef struct { + l2_channel_t *ch; + unsigned int levelmask; +} l2_channel_entry_t; + +typedef struct { + l2_formatter_t cb; + void *ctx; char *name; - void *context; - l2_formatter_t callback; -} l2_format_t; +} l2_formatter_entry_t; struct l2_stream_st { - unsigned int levelmask; - char message[L2_MAX_MSGSIZE]; - l2_channel_t *channels[L2_MAX_CHANNELS]; - l2_format_t formatters[L2_MAX_FORMATTERS]; + unsigned int levelmask; + char message[L2_MAX_MSGSIZE]; + l2_channel_entry_t channels[L2_MAX_CHANNELS]; + l2_formatter_entry_t formatters[L2_MAX_FORMATTERS]; }; /* variable argument handling taking care on argument passing conventions */ Index: ossp-pkg/l2/l2_stream.c RCS File: /v/ossp/cvs/ossp-pkg/l2/Attic/l2_stream.c,v rcsdiff -q -kk '-r1.6' '-r1.7' -u '/v/ossp/cvs/ossp-pkg/l2/Attic/l2_stream.c,v' 2>/dev/null --- l2_stream.c 2001/09/03 12:16:44 1.6 +++ l2_stream.c 2001/09/03 13:43:33 1.7 @@ -40,9 +40,9 @@ return NULL; st->levelmask = 0; for (i = 0; i < L2_MAX_CHANNELS; i++) - st->channels[i] = NULL; + st->channels[i].ch = NULL; for (i = 0; i < L2_MAX_FORMATTERS; i++) - st->formatters[i].name = NULL; + st->formatters[i].cb = NULL; return st; } @@ -50,13 +50,14 @@ { int i; - if (st == NULL || ch == NULL) + if (st == NULL || ch == NULL || levelmask == 0) return L2_ERROR; - for (i = 0; i < L2_MAX_CHANNELS && st->channels[i] != NULL; i++) + for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) ; if (i == L2_MAX_CHANNELS) return L2_ERROR; - st->channels[i] = ch; + st->channels[i].ch = ch; + st->channels[i].levelmask = levelmask; return L2_OK; } @@ -66,13 +67,13 @@ if (st == NULL || name == NULL || cb != NULL) return L2_ERROR; - for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].name != NULL; i++) + 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].context = ctx; - st->formatters[i].callback = cb; + st->formatters[i].ctx = ctx; + st->formatters[i].cb = cb; return L2_OK; } @@ -86,36 +87,48 @@ return L2_OK; } -l2_result_t l2_stream_log(l2_stream_t *st, unsigned int log_level, const char *fmt, ...) +l2_result_t l2_stream_log(l2_stream_t *st, unsigned int level, const char *fmt, ...) { va_list ap; l2_result_t rv; - if (st == NULL || log_level == 0 || fmt == NULL) + if (st == NULL || level == 0 || fmt == NULL) return L2_ERROR; va_start(ap, fmt); - rv = l2_stream_vlog(st, log_level, fmt, ap); + rv = l2_stream_vlog(st, level, fmt, ap); va_end(ap); return rv; } -l2_result_t l2_stream_vlog(l2_stream_t *st, unsigned int log_level, const char *fmt, va_list ap) +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; - if (st == NULL || log_level == 0 || fmt == NULL || ap == NULL) + if (st == NULL || fmt == NULL || ap == NULL) return L2_ERROR; + /* make sure only a single level is specified */ + for (l = level, j = 0; l != 0; l = (l >> 1)) + j++; + if (j != 1) + return L2_ERROR; + + /* 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); rv = L2_OK; - for (i = 0; i < L2_MAX_CHANNELS && st->channels[i] != NULL; i++) { - /* XXX write only if st->levelmask contains log_level */ - if ((rv = l2_channel_write(st->channels[i], st->message, len)) != L2_OK) - break; + for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) { + if (st->channels[i].levelmask & level) { + if ((rv = l2_channel_write(st->channels[i].ch, st->message, len)) != L2_OK) + break; + } } return rv; } @@ -126,9 +139,9 @@ if (st == NULL) return L2_ERROR; - for (i = 0; i < L2_MAX_CHANNELS && st->channels[i] != NULL; i++) - l2_channel_destroy(st->channels[i]); - for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].name != NULL; i++) + 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; Index: ossp-pkg/l2/l2_test.c RCS File: /v/ossp/cvs/ossp-pkg/l2/l2_test.c,v rcsdiff -q -kk '-r1.5' '-r1.6' -u '/v/ossp/cvs/ossp-pkg/l2/l2_test.c,v' 2>/dev/null --- 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 #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; - } +} +