OSSP CVS Repository

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

Check-in Number: 871
Date: 2001-Sep-06 13:56:15 (local)
2001-Sep-06 11:56:15 (UTC)
User:rse
Branch:
Comment: Add two cool extra formatter functions ;)

l2_util_fmt_string: This can be used like %s, but instead of fetching only a "char *" from the var-args stack, it fetches a "char *" plus a "size_t" and this way allows one to log only a sub-string of a larger string without the need for any temporary buffers, etc.

l2_util_fmt_dump: This can be used as "%{type}X" for dumping arbitrary octets. The parameter "type" can be either "text" (the default if only "%X" is used) for dumping the octets as text but with non-printable characters replaced by "\xXX" constructs; "hex" for dumping the octets in hexadecimal as "XX:XX:XX:XX" or "base64" for dumping the octets Base64 encoded. All three are intended for making it easier to produce reasonable L2_LEVEL_DEBUG messages without having to fiddle around with temporary buffers and having to care with non-printable characters.

For instance, using...

      :
  l2_stream_formatter(st, 'D', l2_util_fmt_dump, NULL);
      :
  l2_stream_vlog(st, L2_LEVEL_DEBUG, "%{text}D %{hex}D %{base64}D\n",
                 "foo", 12345, "foo\1bar", 7, "foo\1bar", 7, "foo\1bar", 7);
      :

...produces "foo\x01bar 66:6f:6f:01:62:61:72 Zm9vAWJhcg==" in the output.

Tickets:
Inspections:
Files:
ossp-pkg/l2/Makefile.in      1.15 -> 1.16     2 inserted, 1 deleted
ossp-pkg/l2/l2.h      1.18 -> 1.19     2 inserted, 2 deleted
ossp-pkg/l2/l2_test.c      1.12 -> 1.13     4 inserted, 1 deleted
ossp-pkg/l2/l2_ut_fmtcb.c      added-> 1.1

ossp-pkg/l2/Makefile.in 1.15 -> 1.16

--- Makefile.in  2001/09/05 13:33:26     1.15
+++ Makefile.in  2001/09/06 11:56:15     1.16
@@ -77,7 +77,8 @@
     l2_ut_format.lo \
     l2_ut_param.lo \
     l2_ut_pcre.lo \
-    l2_ut_level.lo
+    l2_ut_level.lo \
+    l2_ut_fmtcb.lo
 
 #   file containing the official version information
 _VERSION_FILE = \


ossp-pkg/l2/l2.h 1.18 -> 1.19

--- l2.h 2001/09/05 13:32:04     1.18
+++ l2.h 2001/09/06 11:56:15     1.19
@@ -164,8 +164,6 @@
 extern l2_handler_t l2_handler_prefix;
 extern l2_handler_t l2_handler_buffer;
 
-/* level operations */
-
 /* channel operations */
 l2_channel_t *l2_channel_create     (l2_handler_t *h);
 l2_result_t   l2_channel_configure  (l2_channel_t *ch, const char *fmt, ...);
@@ -191,6 +189,8 @@
 l2_result_t   l2_util_setparams    (l2_param_t p[], const char *fmt, va_list ap);
 l2_result_t   l2_util_l2s          (char *string,       size_t maxlen, int sep, unsigned int  levelmask);
 l2_result_t   l2_util_s2l          (const char *string, size_t maxlen, int sep, unsigned int *levelmask);
+l2_result_t   l2_util_fmt_string   (l2_context_t *, const char, const char *, char *, size_t, size_t *, va_list *);
+l2_result_t   l2_util_fmt_dump     (l2_context_t *, const char, const char *, char *, size_t, size_t *, va_list *);
 
 #endif /* __L2_H__ */
 


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

--- l2_test.c    2001/09/05 13:56:43     1.12
+++ l2_test.c    2001/09/06 11:56:15     1.13
@@ -132,7 +132,10 @@
     if (l2_stream_formatter(st, 'k', formatter, NULL) != L2_OK)
         die("failed to configure formatter for %%x");
 
-    if (l2_stream_log(st, L2_LEVEL_PANIC, "Checking localhost %s %{myparm}k\n", "foo", 12345) != L2_OK)
+    if (l2_stream_formatter(st, 'S', l2_util_fmt_dump, NULL) != L2_OK)
+        die("failed to configure formatter for %%S");
+
+    if (l2_stream_log(st, L2_LEVEL_PANIC, "Checking localhost %s %{myparm}k <%{text}S><%{hex}S><%{base64}S>\n", "foo", 12345, "foo\1bar", 7, "foo\1bar", 7, "foo\1bar", 7) != L2_OK)
         die("failed to log message to stream");
 
     if (l2_stream_destroy(st) != L2_OK)


ossp-pkg/l2/l2_ut_fmtcb.c -> 1.1

*** /dev/null    Fri Apr 26 19:55:00 2024
--- -    Fri Apr 26 19:56:05 2024
***************
*** 0 ****
--- 1,173 ----
+ /*
+ **  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_format.c: extra formatters
+ */
+ 
+ #include <ctype.h>
+ 
+ #include "l2.h"
+ 
+ l2_result_t 
+ l2_util_fmt_string(
+     l2_context_t *ctx, const char id, const char *param, 
+     char *bufptr, size_t bufsize, size_t *buflen, va_list *ap)
+ {
+     char *cpStr;
+     size_t nStr;
+ 
+     cpStr = va_arg(*ap, char *);
+     nStr  = (size_t)va_arg(*ap, int);
+     if (nStr > bufsize)
+         return L2_ERROR;
+     memcpy(bufptr, cpStr, nStr);
+     *buflen = nStr;
+     return L2_OK;
+ }
+ 
+ l2_result_t 
+ l2_util_fmt_dump(
+     l2_context_t *ctx, const char id, const char *param, 
+     char *bufptr, size_t bufsize, size_t *buflen, va_list *ap)
+ {
+     static const char hex_table[]    = "0123456789abcdef";
+     static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
+                                        "abcdefghijklmnopqrstuvwxyz"
+                                        "0123456789+/";
+     unsigned char base64_in[3];
+     unsigned char base64_out[4];
+     size_t nData;
+     unsigned char *ucpData;
+     unsigned char uc;
+     unsigned char *cpO;
+     unsigned char *cpI;
+     int i;
+     int n;
+ 
+     /* fetch data pointer and data size from argument list */
+     ucpData = va_arg(*ap, unsigned char *);
+     nData   = (size_t)va_arg(*ap, int);
+ 
+     /* 
+      * textual dumping: "foo\1bar" -> "foo\x01bar"
+      */
+     if (strcmp(param, "text") == 0 || param[0] == '\0') {
+         cpI = ucpData;
+         cpO = (unsigned char *)bufptr;
+         while (cpI < (ucpData+nData)) {
+             uc = *cpI++;
+             if (isprint((int)uc)) {
+                 if ((char *)cpO >= (bufptr+bufsize))
+                     return L2_ERROR;
+                 *cpO++ = uc;
+             }
+             else if (uc == '\\') {
+                 if ((char *)(cpO+1) >= (bufptr+bufsize))
+                     return L2_ERROR;
+                 *cpO++ = '\\';
+                 *cpO++ = '\\';
+             }
+             else {
+                 if ((char *)(cpO+3) >= (bufptr+bufsize))
+                     return L2_ERROR;
+                 *cpO++ = '\\';
+                 *cpO++ = 'x';
+                 *cpO++ = hex_table[(uc >> 4) & 0xf];
+                 *cpO++ = hex_table[uc & 0xf];
+             }
+         }
+         *buflen = ((char *)cpO - bufptr);
+     }
+ 
+     /* 
+      * hexadecimal dumping: "foo\1bar" -> "66:6f:6f:01:62:61:72"
+      */
+     else if (strcmp(param, "hex") == 0) {
+         if (((nData * 3) - 1) > bufsize)
+             return L2_ERROR;
+         cpO = (unsigned char *)bufptr;
+         for (i = 0; i < nData; i++) {
+             uc = *ucpData++;
+             *cpO++ = hex_table[(uc >> 4) & 0xf];
+             *cpO++ = hex_table[uc & 0xf];
+             if (i < (nData - 1))
+                 *cpO++ = ':';
+         }
+         *buflen = (nData * 3) - 1;
+     }
+ 
+     /*
+      * Base64 dumping: "foo\1bar" -> "Zm9vAWJhcg=="
+      */
+     else if (strcmp(param, "base64") == 0) {
+         /* bulk encoding */
+         n = 0;
+         while (nData >= 3) {
+             base64_in[0] = *ucpData++;
+             base64_in[1] = *ucpData++;
+             base64_in[2] = *ucpData++;
+             nData -= 3;
+             base64_out[0] = ( base64_in[0] >> 2);
+             base64_out[1] = ((base64_in[0] & 0x03) << 4) + (base64_in[1] >> 4);
+             base64_out[2] = ((base64_in[1] & 0x0f) << 2) + (base64_in[2] >> 6);
+             base64_out[3] = ( base64_in[2] & 0x3f);
+             if (n + 4 > bufsize)
+                 return L2_ERROR;
+             bufptr[n++] = base64_table[base64_out[0]];
+             bufptr[n++] = base64_table[base64_out[1]];
+             bufptr[n++] = base64_table[base64_out[2]];
+             bufptr[n++] = base64_table[base64_out[3]];
+         }
+         /* now worry about padding with remaining 1 or 2 bytes */
+         if (nData != 0) {
+             base64_in[0] = base64_in[1] = base64_in[2] = '\0';
+             for (i = 0; i < nData; i++)
+                 base64_in[i] = *ucpData++;
+             base64_out[0] = ( base64_in[0] >> 2);
+             base64_out[1] = ((base64_in[0] & 0x03) << 4) + (base64_in[1] >> 4);
+             base64_out[2] = ((base64_in[1] & 0x0f) << 2) + (base64_in[2] >> 6);
+             if (n + 4 > bufsize)
+                 return L2_ERROR;
+             bufptr[n++] = base64_table[base64_out[0]];
+             bufptr[n++] = base64_table[base64_out[1]];
+             if (nData == 1)
+                 bufptr[n++] = '=';
+             else
+                 bufptr[n++] = base64_table[base64_out[2]];
+             bufptr[n++] = '=';
+         }
+         if (n >= bufsize)
+             return L2_ERROR;
+         *buflen = n;
+     }
+ 
+     /* anything else is an unknown dumping method... */
+     else
+         return L2_ERROR;
+ 
+     return L2_OK;
+ }
+ 

CVSTrac 2.0.1