]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/debug.c
from jon@symas.com - misc cleanup
[openldap] / libraries / liblutil / debug.c
index 60230519bf7066c5ee16fd82c2bb69580015979d..ef58ef271be227b33d32f98a3120fad0bdd5b5c7 100644 (file)
 #include "ldap_defaults.h"
 #include "lber.h"
 
-struct M2S
-{
-       char *mnemonic;
-       int  subsys;
-};
-
 struct DEBUGLEVEL
 {
        char *subsystem;
@@ -50,20 +44,20 @@ static int debug2syslog(int l) {
 #endif
 
 static char *lutil_levels[] = {"emergency", "alert", "critical",
-                           "error", "warning", "notice",
-                           "information", "entry", "args",
-                           "results", "detail1", "detail2",
-                           NULL};
+                          "error", "warning", "notice",
+                          "information", "entry", "args",
+                          "results", "detail1", "detail2",
+                          NULL};
 
-int lutil_mnem2level( char *level )
+int lutil_mnem2level( const char *level )
 {
     int i;
     for( i = 0; lutil_levels[i] != NULL; i++ )
     {
-        if ( !strcasecmp( level, lutil_levels[i] ) )
-        {
-            return i;
-        }
+       if ( !strcasecmp( level, lutil_levels[i] ) )
+       {
+           return i;
+       }
     }
     return 0;
 }
@@ -71,6 +65,9 @@ int lutil_mnem2level( char *level )
 static void addSubsys( const char *subsys, int level )
 {
        int i, j;
+
+       if ( !strcasecmp( subsys, "global") ) global_level = level;
+
        for( i = 0; i < numLevels; i++ )
        {
                if ( levelArray[i] == NULL )
@@ -100,9 +97,9 @@ static void addSubsys( const char *subsys, int level )
        return;
 }
 
-void lutil_set_debug_level( char* subsys, int level )
+void lutil_set_debug_level( const char* subsys, int level )
 {
-       addSubsys( subsys, level );
+    addSubsys( subsys, level );
 }
 
 int lutil_debug_file( FILE *file )
@@ -115,7 +112,7 @@ int lutil_debug_file( FILE *file )
 
 void lutil_log_int(
        FILE* file,
-       char *subsys, int level,
+       const char *subsys, int level,
        const char *fmt, va_list vl )
 {
        time_t now;
@@ -129,7 +126,7 @@ void lutil_log_int(
         * break out of the loop.
         */
        for( i = 0; i < numLevels; i++ ) {
-               if ( levelArray[i] == NULL ) return
+               if ( levelArray[i] == NULL ) break
                if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
        }
 
@@ -138,7 +135,7 @@ void lutil_log_int(
         * the requested output level, don't output it.
         */
        if ( (level > global_level) &&
-               ((i > numLevels ) || ( level > levelArray[i]->level )) )
+               ((i > numLevels ) || (levelArray[i] == NULL) || ( level > levelArray[i]->level )) )
        {
                return;
        }
@@ -198,11 +195,11 @@ void lutil_log_int(
 }
 
 /*
- * The primary logging routine.  Takes the subsystem being logged from, the
+ * The primary logging routine.         Takes the subsystem being logged from, the
  * level of the log output and the format and data.  Send this on to the
  * internal routine with the print file, if any.
  */
-void lutil_log( char *subsys, int level, const char *fmt, ... )
+void lutil_log( const char *subsys, int level, const char *fmt, ... )
 {
        FILE* outfile = NULL;
        va_list vl;
@@ -228,27 +225,32 @@ void lutil_log_initialize(int argc, char **argv)
      */
     for( i = 0; i < argc; i++ )
     {
-        char *next = argv[i];
-        if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
-        {
-            char subsys[64];
-            int level;
-            char *optarg = argv[i+1];
-            char *index = strchr( optarg, '=' );
-            if ( index != NULL )
-            {
-                *index = 0;
-                strcpy ( subsys, optarg );
-                level = atoi( index+1 );
-                if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
-                lutil_set_debug_level( subsys, level );
-                *index = '=';
-            }
-            else
-            {
-                global_level = atoi( optarg );
-            }
-        }
+       char *next = argv[i];
+       if ( i < argc-1 && next[0] == '-' && next[1] == 'd' )
+       {
+           char subsys[64];
+           int level;
+           char *optarg = argv[i+1];
+           char *index = strchr( optarg, '=' );
+           if ( index != NULL )
+           {
+               *index = 0;
+               strcpy ( subsys, optarg );
+               level = atoi( index+1 );
+               if ( level <= 0 ) level = lutil_mnem2level( index + 1 );
+               lutil_set_debug_level( subsys, level );
+               *index = '=';
+           }
+           else
+           {
+               global_level = atoi( optarg );
+               /* 
+                * if a negative number was used, make the global level the
+                * maximum sane level.
+                */
+               if ( global_level < 0 ) global_level = 65535;
+           }
+       }
     }
 }