head 1.22; access; symbols LMTP2NNTP_1_1_1:1.20 LMTP2NNTP_1_1_0:1.20 LMTP2NNTP_1_1b4:1.20 LMTP2NNTP_1_1b3:1.20 L2_CHANNEL_ONLY_REVAMPING_BEFORE:1.20 LMTP2NNTP_1_1b2:1.20 LMTP2NNTP_1_1b1:1.20 L2_0_1_0:1.18 L2NGATE:1.5.0.2 START_MICHAEL:1.5 L2_INITIAL:1.1.1.1 OSSP:1.1.1; locks; strict; comment @ * @; 1.22 date 2001.11.04.13.21.17; author rse; state dead; branches; next 1.21; 1.21 date 2001.11.03.22.51.36; author rse; state Exp; branches; next 1.20; 1.20 date 2001.09.13.19.20.48; author rse; state Exp; branches; next 1.19; 1.19 date 2001.09.13.16.18.42; author ms; state Exp; branches; next 1.18; 1.18 date 2001.09.13.12.35.52; author ms; state Exp; branches; next 1.17; 1.17 date 2001.09.13.11.58.05; author ms; state Exp; branches; next 1.16; 1.16 date 2001.09.12.13.50.46; author rse; state Exp; branches; next 1.15; 1.15 date 2001.09.12.09.42.34; author ms; state Exp; branches; next 1.14; 1.14 date 2001.09.06.14.37.53; author rse; state Exp; branches; next 1.13; 1.13 date 2001.09.05.14.10.00; author thl; state Exp; branches; next 1.12; 1.12 date 2001.09.05.13.33.55; author rse; state Exp; branches; next 1.11; 1.11 date 2001.09.05.07.38.04; author rse; state Exp; branches; next 1.10; 1.10 date 2001.09.04.14.56.25; author rse; state Exp; branches; next 1.9; 1.9 date 2001.09.04.14.17.11; author ms; state Exp; branches; next 1.8; 1.8 date 2001.09.04.13.52.59; author rse; state Exp; branches; next 1.7; 1.7 date 2001.09.03.13.43.33; author rse; state Exp; branches; next 1.6; 1.6 date 2001.09.03.12.16.44; author rse; state Exp; branches; next 1.5; 1.5 date 2001.08.15.10.36.03; author rse; state Exp; branches; next 1.4; 1.4 date 2001.05.26.08.02.55; author rse; state Exp; branches; next 1.3; 1.3 date 2001.05.22.18.47.31; author rse; state Exp; branches; next 1.2; 1.2 date 2001.05.17.14.43.26; author simons; state Exp; branches; next 1.1; 1.1 date 2001.05.10.19.46.01; author rse; state Exp; branches 1.1.1.1; next ; 1.1.1.1 date 2001.05.10.19.46.01; author rse; state Exp; branches; next ; desc @@ 1.22 log @Channel-Only Revamping Step 2: - moved code of l2_stream.c into (new) l2_env.c and l2_channel.c - created new l2_env_t and l2_env_xxx() - changed l2_xx_create() functions to also return l2_result_t - moved error handling into l2_env_t - replaced l2_channel_stack() with two new and more flexible l2_channel_link() and l2_channel_unlink() functions - rewritten test stuff in l2_test.c to use new structure - added new l2_channel_env() function for retriving l2_env_t Puhhh.... @ text @/* ** L2 - OSSP Logging Library ** Copyright (c) 2001 The OSSP Project (http://www.ossp.org/) ** Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/) ** ** This file is part of OSSP L2, a flexible logging library which ** can be found at http://www.ossp.org/pkg/l2/. ** ** Permission to use, copy, modify, and distribute this software for ** any purpose with or without fee is hereby granted, provided that ** the above copyright notice and this permission notice appear in all ** copies. ** ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. ** ** l2.c: internal C implementation ** */ #include "l2.h" #include "l2_p.h" /* create stream */ l2_stream_t *l2_stream_create(void) { l2_stream_t *st; int i; /* allocate stream structure */ if ((st = (l2_stream_t *)malloc(sizeof(l2_stream_t))) == NULL) return NULL; /* initialize stream structure */ st->levelmask = 0; st->flushmask = 0; for (i = 0; i < L2_MAX_CHANNELS; i++) st->channels[i].ch = NULL; for (i = 0; i < L2_MAX_FORMATTERS; i++) st->formatters[i].cb = NULL; return st; } /* attach channel to stream */ l2_result_t l2_stream_channel(l2_stream_t *st, l2_channel_t *ch, unsigned int levelmask, unsigned int flushmask) { #if 0 l2_channel_t *chC; l2_channel_t *chN; l2_chtype_t t; #endif int i; /* argument sanity check */ if (st == NULL || ch == NULL || levelmask == 0) return L2_ERR_ARG; /* make sure the stack of channels consists of zero or more filter channels followed by exactly one output channel */ #if 0 for (chC = ch; (chN = l2_channel_downstream(chC)) != NULL; chC = chN) if (l2_channel_type(chC, &t) == L2_OK && t != L2_CHANNEL_FILTER) return L2_ERR_USE; if (l2_channel_type(chC, &t) == L2_OK && t != L2_CHANNEL_OUTPUT) return L2_ERR_USE; #endif /* find next free channel position in channel array */ for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) ; if (i == L2_MAX_CHANNELS) return L2_ERR_MEM; /* attach channel to stream */ st->channels[i].ch = ch; st->channels[i].levelmask = levelmask; st->channels[i].flushmask = flushmask; return L2_OK; } /* attach formatter to stream */ l2_result_t l2_stream_formatter(l2_stream_t *st, char id, l2_formatter_t cb, l2_context_t *ctx) { int i; /* argument sanity check */ if (st == NULL || id == '\0' || cb == NULL) return L2_ERR_ARG; /* find next free formatter position in formatter array */ for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++) ; if (i == L2_MAX_FORMATTERS) return L2_ERR_MEM; /* attach formatter to stream */ st->formatters[i].id = id; st->formatters[i].ctx = ctx; st->formatters[i].cb = cb; return L2_OK; } /* set stream level mask */ l2_result_t l2_stream_levels(l2_stream_t *st, unsigned int levelmask, unsigned int flushmask) { /* argument sanity check */ if (st == NULL) return L2_ERR_ARG; /* override global level mask */ st->levelmask = levelmask; st->flushmask = flushmask; return L2_OK; } /* log a message to stream */ l2_result_t l2_stream_log(l2_stream_t *st, l2_level_t level, const char *fmt, ...) { va_list ap; l2_result_t rv; /* pass-through to va_list-based variant */ va_start(ap, fmt); rv = l2_stream_vlog(st, level, fmt, ap); va_end(ap); return rv; } /* indirect callback function from l2_stream_vlog for flushing */ static int l2_stream_vlog_flush(l2_util_format_t *vfmt) { /* we do no format buffer flushing */ return -1; } /* indirect callback function from l2_stream_vlog for formatting */ 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); l2_result_t rv; int i; /* init formatting result */ *cPrefix = '\0'; *cPad = ' '; *cppOut = NULL; *npOutLen = 0; /* iterate over all configured L2 formatters */ for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++) { if (st->formatters[i].id == cId) { rv = st->formatters[i].cb(st->formatters[i].ctx, cId, cpParam, cpBuf, nBufLenMax, npOutLen, apArgs); vfmt->data[1].i = (int)rv; if (rv == L2_OK) { *cppOut = cpBuf; break; } } } return; } /* log a message to stream (va_list-variant) */ l2_result_t l2_stream_vlog(l2_stream_t *st, l2_level_t level, const char *fmt, va_list ap) { int i; int l, j; size_t len; l2_result_t rv; l2_util_format_t vfmt; /* argument sanity check */ if (st == NULL || level == 0 || fmt == NULL || ap == NULL) return L2_ERR_ARG; /* make sure only a single level is specified */ for (l = level, j = 0; l != 0; l = (l >> 1)) if (l & 0x1) j++; if (j != 1) return L2_ERR_ARG; /* check whether global level mask already stops processing */ if (!(st->levelmask & level)) return L2_OK; /* format message */ vfmt.curpos = st->message; vfmt.endpos = st->message + L2_MAX_MSGSIZE; vfmt.data[0].vp = st; vfmt.data[1].i = L2_ERR_FMT; vfmt.flush = l2_stream_vlog_flush; vfmt.format = l2_stream_vlog_format; if ((len = l2_util_format(&vfmt, fmt, ap)) == -1) return (l2_result_t)(vfmt.data[1].i); if (len == 0) return L2_ERR_FMT; /* make sure a trailing newline exists */ if (st->message[len-1] != '\n') { if (len == L2_MAX_MSGSIZE) return L2_ERR_MEM; st->message[len++] = '\n'; st->message[len] = '\0'; } /* write message to zero or more channels */ rv = L2_OK; 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, level, st->message, len)) != L2_OK) break; if ((st->flushmask & level) || (st->channels[i].flushmask & level)) if ((rv = l2_channel_flush(st->channels[i].ch)) != L2_OK) break; } } return rv; } /* flush stream */ l2_result_t l2_stream_flush(l2_stream_t *st) { int i; l2_result_t rv; /* argument sanity check */ if (st == NULL) return L2_ERR_ARG; /* flush all attached channels */ for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) if ((rv = l2_channel_flush(st->channels[i].ch)) != L2_OK) return rv; return L2_OK; } /* destroy stream */ l2_result_t l2_stream_destroy(l2_stream_t *st) { int i; l2_result_t rv; /* argument sanity check */ if (st == NULL) return L2_ERR_ARG; /* destroy all attached channels */ for (i = 0; i < L2_MAX_CHANNELS && st->channels[i].ch != NULL; i++) if ((rv = l2_channel_destroy(st->channels[i].ch)) != L2_OK) return rv; /* free stream structure */ free(st); return L2_OK; } @ 1.21 log @Channel-Only Revamping Step 1: allow multiple downstream channels in order to approach the later tree-like channel-only structure. @ text @@ 1.20 log @For consistency reasons provide a l2_stream_flush() function which allows one to flush all channel (stacks) in one action. @ text @d58 1 d61 2 d71 1 d73 1 a73 1 if (l2_channel_type(chC) != L2_CHANNEL_FILTER) d75 1 a75 1 if (l2_channel_type(chC) != L2_CHANNEL_OUTPUT) d77 1 @ 1.19 log @Remove redundant runtime variable checks, added level variable check where it is needed most. @ text @d235 18 @ 1.18 log @Backed out changes for lmtp2nntp release testing. @ text @a130 4 /* argument sanity check */ if (st == NULL || level == 0 || fmt == NULL) return L2_ERR_ARG; d187 1 a187 1 if (st == NULL || fmt == NULL || ap == NULL) @ 1.17 log @Check that an incoming level argument is given, but only check arguments once - in l2_stream_vlog where it counts. @ text @d131 4 d191 1 a191 1 if (st == NULL || level == 0 || fmt == NULL || ap == NULL) @ 1.16 log @First cut for flush mask support. @ text @a130 4 /* argument sanity check */ if (st == NULL || level == 0 || fmt == NULL) return L2_ERR_ARG; d187 1 a187 1 if (st == NULL || fmt == NULL || ap == NULL) @ 1.15 log @Upgraded both stream and channel-level APIs to include new L2_LEVEL parameter design. @ text @d46 1 d56 1 a56 1 l2_result_t l2_stream_channel(l2_stream_t *st, l2_channel_t *ch, unsigned int levelmask) d83 1 d112 1 a112 1 l2_result_t l2_stream_levels(l2_stream_t *st, unsigned int levelmask) d120 1 d231 3 @ 1.14 log @Replace generic L2_ERROR with more granular L2_ERR_XXX and make sure that we always check with "!= L2_OK". @ text @d123 1 a123 1 l2_result_t l2_stream_log(l2_stream_t *st, unsigned int level, const char *fmt, ...) d179 1 a179 1 l2_result_t l2_stream_vlog(l2_stream_t *st, unsigned int level, const char *fmt, va_list ap) d226 1 a226 1 if ((rv = l2_channel_write(st->channels[i].ch, st->message, len)) != L2_OK) @ 1.13 log @fix bit-check @ text @d34 1 d40 1 d43 2 d50 1 d54 1 a56 1 int i; d59 1 d61 1 d63 1 a63 1 return L2_ERROR; d69 1 a69 1 return L2_ERROR; d71 1 a71 1 return L2_ERROR; d77 1 a77 1 return L2_ERROR; d79 1 a79 1 /* attach this channel */ d86 1 d91 1 d93 3 a95 1 return L2_ERROR; d99 3 a101 1 return L2_ERROR; d105 1 d109 1 d112 1 d114 3 a116 1 return L2_ERROR; d118 1 d122 1 d128 1 d130 3 a132 1 return L2_ERROR; d136 1 d140 1 d143 1 d147 1 d154 1 d157 1 a157 1 /* init result */ d163 1 a163 1 /* iterate over all configured formatters */ d166 4 a169 2 if (st->formatters[i].cb(st->formatters[i].ctx, cId, cpParam, cpBuf, nBufLenMax, npOutLen, apArgs) == L2_OK) { d178 1 d187 1 d189 1 a189 1 return L2_ERROR; d196 1 a196 1 return L2_ERROR; d198 1 a198 1 /* check whether level is globally enabled */ d206 1 d210 1 a210 1 return L2_ERROR; d212 1 a212 1 return L2_ERROR; d217 1 a217 1 return L2_ERROR; d222 1 d233 1 d237 1 d239 1 d241 3 a243 1 return L2_ERROR; d245 4 a248 1 l2_channel_destroy(st->channels[i].ch); d250 1 @ 1.12 log @make sure logging message has a trailing newline @ text @d161 2 a162 1 j++; d169 1 a169 1 @ 1.11 log @on stream attachment of a channel stack, make sure the stack of channels consists of zero or more filter channels followed by exactly one output channel. I knew that providing l2_channel_type() will be useful... ;) @ text @d171 1 a171 1 vfmt.endpos = st->message+ L2_MAX_MSGSIZE; d177 10 @ 1.10 log @cleanup level API (no need for retrieving old mask just in one function - either in all or in none). @ text @d52 2 d57 10 d71 2 d75 1 @ 1.9 log @Bugfixes and new format testing. @ text @d80 1 a80 1 l2_result_t l2_stream_levels(l2_stream_t *st, unsigned int levelmask, unsigned int *levelmaskold) a83 2 if (levelmaskold != NULL) *levelmaskold = st->levelmask; @ 1.8 log @Wohhooooo! Here comes the underlying message formatting support: 1. renamed l2_channel_setparam() to l2_util_setparam() because it is just a utility function and is not tied to any channel. 2. moved l2_util_setparam() to its own l2_ut_param.c source file. 3. added l2_ut_format.c which contains a slightly adjusted version of Str's str_format() stuff under the name l2_util_format(). 4. use l2_util_format() in l2_stream.c instead of vsnprintf() and this way finally support l2_formatter_t callbacks. 5. cleanup adjustments to the l2_stream_formatter() API. Let's rock... @ text @d68 1 a68 1 if (st == NULL || id == '\0' || cb != NULL) @ 1.7 log @- replace "int" with "l2_result_t" in L2 channel API - use a 2^n for L2_LEVEL_XXX in order to be able to create mask - remember loglevel for each channel - rewrite test suite @ text @d64 1 a64 1 l2_result_t l2_stream_formatter(l2_stream_t *st, const char *name, l2_formatter_t cb, l2_context_t *ctx) d68 1 a68 1 if (st == NULL || name == NULL || cb != NULL) d74 1 a74 1 st->formatters[i].name = strdup(name); d103 32 d141 1 d155 9 a163 3 /* XXX use st->formatter!! XXX */ len = vsnprintf(st->message, L2_MAX_MSGSIZE, fmt, ap); a182 2 for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].cb != NULL; i++) free(st->formatters[i].name); @ 1.6 log @revamped L2 stream API @ text @d43 1 a43 1 st->channels[i] = NULL; d45 1 a45 1 st->formatters[i].name = NULL; d53 1 a53 1 if (st == NULL || ch == NULL) d55 1 a55 1 for (i = 0; i < L2_MAX_CHANNELS && st->channels[i] != NULL; i++) d59 2 a60 1 st->channels[i] = ch; d70 1 a70 1 for (i = 0; i < L2_MAX_FORMATTERS && st->formatters[i].name != NULL; i++) d75 2 a76 2 st->formatters[i].context = ctx; st->formatters[i].callback = cb; d90 1 a90 1 l2_result_t l2_stream_log(l2_stream_t *st, unsigned int log_level, const char *fmt, ...) d95 1 a95 1 if (st == NULL || log_level == 0 || fmt == NULL) d98 1 a98 1 rv = l2_stream_vlog(st, log_level, fmt, ap); d103 1 a103 1 l2_result_t l2_stream_vlog(l2_stream_t *st, unsigned int log_level, const char *fmt, va_list ap) d106 1 d110 1 a110 1 if (st == NULL || log_level == 0 || fmt == NULL || ap == NULL) d113 10 d127 5 a131 4 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; d142 3 a144 3 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++) @ 1.5 log @Fix more ossp.com references by replacing with the correct domain name ossp.org. @ text @d37 1 d41 5 a45 1 memset(st, 0, sizeof(l2_stream_t)); d49 1 a49 1 l2_stream_t *l2_stream_channel(l2_stream_t *st, l2_channel_t *ch, unsigned int levelmask) d51 1 a51 1 size_t i; d53 3 a55 1 for (i = 0; i <= L2_MAX_CHANNELS && st->channels[i] != NULL; i++) d58 1 a58 1 return NULL; d60 1 a60 1 return st; d63 1 a63 1 l2_stream_t *l2_stream_formatter(l2_stream_t *st, l2_formatter_t *cb, l2_context_t *ctx) d65 12 a76 1 return NULL; d79 1 a79 1 unsigned int l2_stream_levels(l2_stream_t *st, unsigned int levelmask) d81 6 a86 1 return 0; d89 1 a89 1 void l2_stream_log(l2_stream_t *st, unsigned int log_level, const char *fmt, ...) d92 4 d97 1 a97 1 l2_stream_vlog(st, log_level, fmt, ap); d99 1 a99 1 return; d102 1 a102 1 void l2_stream_vlog(l2_stream_t *st, unsigned int log_level, const char *fmt, va_list ap) d104 3 a106 1 size_t i, len; d108 4 a112 10 for (i = 0; i <= L2_MAX_CHANNELS && st->channels[i] != NULL; i++) st->channels[i]->handler.write(&st->channels[i]->context, st->channels[i]->downstream, st->message, len); return; } void l2_stream_destroy(l2_stream_t *st) { size_t i; d114 16 a129 1 for (i = 0; i <= L2_MAX_CHANNELS && st->channels[i] != NULL; i++) d131 4 a134 3 for (i = 0; i <= L2_MAX_FORMATTERS && st->formatters[i] != NULL; i++) free(st->formatters[i]); return; @ 1.4 log @change terminology: below -> downstream @ text @d7 1 a7 1 ** can be found at http://www.ossp.com/pkg/l2/. @ 1.3 log @Simplify things by merging the parameter stuff into l2_channel.c and the l2_stream_t related things into l2_stream.c @ text @d82 1 a82 1 st->channels[i]->below, @ 1.2 log @Implemented l2_stream_create(), l2_stream_destroy(), and l2_stream_channel(). @ text @d34 2 a35 2 l2_stream_t* l2_stream_create(void) { d37 2 a38 2 st = (l2_stream_t *)malloc(sizeof(l2_stream_t)); if (!st) d42 1 a42 1 } d44 2 a45 2 void l2_stream_destroy(l2_stream_t* st) { a46 5 for (i = 0; i <= L2_MAX_CHANNELS && st->channels[i]; ++i) l2_channel_destroy(st->channels[i]); for (i = 0; i <= L2_MAX_FORMATTERS && st->formatters[i]; ++i) free(st->formatters[i]); } d48 2 a49 5 l2_stream_t* l2_stream_channel(l2_stream_t *st, l2_channel_t *ch, unsigned int levelmask) { size_t i; for (i = 0; i <= L2_MAX_CHANNELS && st->channels[i]; ++i) ; d51 1 a51 1 return 0; /* Array is full. */ d54 44 a97 1 } @ 1.1 log @Initial revision @ text @d34 29 @ 1.1.1.1 log @L2 initial source tree @ text @@