/* ** 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_ut_level.c: level mask transformations */ #include "l2.h" #include "l2_p.h" #include #include static struct { l2_level_t level; char *string; } l2s_table[] = { { L2_LEVEL_PANIC, "panic" }, { L2_LEVEL_CRITICAL, "critical" }, { L2_LEVEL_ERROR, "error" }, { L2_LEVEL_WARNING, "warning" }, { L2_LEVEL_NOTICE, "notice" }, { L2_LEVEL_INFO, "info" }, { L2_LEVEL_TRACE, "trace" }, { L2_LEVEL_DEBUG, "debug" }, { 0, NULL } }; l2_result_t l2_util_l2s(char *string, size_t maxlen, l2_level_t level) { int len, i, j, l; string[0] = '\0'; len = i = j = l = 0; for (i = 0; l2s_table[i].level != NULL; i++) { /* loop through levels */ if (level & l2s_table[i].level) { if (j != 0) return L2_ERR_ARG; /* level has matched more than one string */ else j = i; /* index the given level to its corresponding string */ } l = strlen(l2s_table[i].string); if (l > len) len = l; /* len is the size of the largest level string */ } if (len + 1 > maxlen) return L2_ERR_MEM; else sprintf(string, "%s", l2s_table[j].string); for (i = 0; string[i] != '\0'; i++) /* process string to dynamically pad */ ; /* with spaces in order to line up */ while (i < len){ /* small level string text with the */ string[i++] = ' '; /* larger ones */ } string[i] = '\0'; /* don't forget to put back the EOL */ return L2_OK; } static unsigned int hexval(const char *cpB, const char *cpE) { unsigned int hv; unsigned int nibble; hv = 0; while (cpB < cpE) { nibble = tolower((unsigned int)(*cpB++)); if (isdigit(nibble)) nibble = nibble - '0'; else nibble = nibble - 'a'; hv = ((hv << 4) | nibble); } return hv; } static int myishexnumber(int c) { if (isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) return 1; return 0; } l2_result_t l2_util_s2l(const char *string, size_t maxlen, l2_level_t *level) { int bFound; int i; *level = 0; bFound = 0; for (i = 0; l2s_table[i].level != NULL; i++) { if (strcasecmp(string, l2s_table[i].string) == 0) { *level = l2s_table[i].level; bFound = 1; break; } } if (!bFound) { if (strncasecmp(string, "0x", 2) == 0 && myishexnumber((int)(*(string+2)))) { *level = hexval(string+2, string + strlen(string)); } else return L2_ERR_ARG; } return L2_OK; }