]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/debug.c
from jon@symas.com - misc cleanup
[openldap] / libraries / liblutil / debug.c
index 1e3a51f8b5ab22c7516df9d1793660af616cbaeb..ef58ef271be227b33d32f98a3120fad0bdd5b5c7 100644 (file)
@@ -3,26 +3,11 @@
  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
-/*
- * Copyright (c) 1996, 1998 by Internet Software Consortium.
- *
- * 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.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
- * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
- * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
- * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
- * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
- * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
- * SOFTWARE.
- */
 
 #include "portable.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #include <ac/stdarg.h>
 #include <ac/string.h>
 #include "ldap_defaults.h"
 #include "lber.h"
 
-static FILE *log_file;
-
-struct M2S
-{
-       char *mnemonic;
-       int  subsys;
-};
-
 struct DEBUGLEVEL
 {
        char *subsystem;
@@ -49,23 +26,38 @@ struct DEBUGLEVEL
 static struct DEBUGLEVEL **levelArray;
 static long   numLevels = 0;
 
-int global_level = 0;
+static FILE *log_file = NULL;
+static int global_level = 0;
+
+#if 0
+#ifdef LDAP_SYSLOG
+static int use_syslog = 0;
+
+static int debug2syslog(int l) {
+       switch (l) {
+       /* insert mapping cases here */
+       default:
+       }
+       return LOG_DEBUG
+}
+#endif
+#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;
 }
@@ -73,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 )
@@ -102,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,31 +110,45 @@ int lutil_debug_file( FILE *file )
        return 0;
 }
 
-void lutil_log_int(FILE* file, char *subsys, int level, const char *fmt, va_list vl )
+void lutil_log_int(
+       FILE* file,
+       const char *subsys, int level,
+       const char *fmt, va_list vl )
 {
-       char buffer[4096];
        time_t now;
        struct tm *today;
        int i;
 
-        if ( levelArray == NULL ) return; /* logging isn't set up */
-        /*
-         * Look for the subsystem in the level array.  When we find it, break out of the
-         * loop.
-         */
-       for( i = 0; i < numLevels; i++ )
-       {
-               if ( levelArray[i] == NULL ) return
+       if ( levelArray == NULL ) return; /* logging isn't set up */
+
+       /*
+        * Look for the subsystem in the level array.  When we find it,
+        * break out of the loop.
+        */
+       for( i = 0; i < numLevels; i++ ) {
+               if ( levelArray[i] == NULL ) break
                if ( ! strcasecmp( levelArray[i]->subsystem, subsys ) ) break;
        }
 
-        /*
-         * If we didn't find the subsystem, or the set level is less than
-         * the requested output level, don't output it.
-         */
-       if ( (level > global_level) && 
-             ((i > numLevels ) || ( level > levelArray[i]->level )) )
+       /*
+        * If we didn't find the subsystem, or the set level is less than
+        * the requested output level, don't output it.
+        */
+       if ( (level > global_level) &&
+               ((i > numLevels ) || (levelArray[i] == NULL) || ( level > levelArray[i]->level )) )
+       {
                return;
+       }
+
+#if 0
+#ifdef LDAP_SYSLOG
+       /* we're configured to use syslog */
+       if( use_syslog ) {
+               vsyslog( debug2syslog(level), fmt, vl );
+               return;
+       }
+#endif
+#endif
 
 #if 0
 #ifdef HAVE_WINSOCK
@@ -157,64 +166,40 @@ void lutil_log_int(FILE* file, char *subsys, int level, const char *fmt, va_list
 #endif
 #endif
 
-        /*
-         * Stick the time in the buffer to output.  Kurt doesn't like
-         * doing this here, but NT can't pipe to a timestamp program
-         * like Unix can, and I don't think it costs much.
-         */
+       if( file == NULL ) {
+               /*
+                * Use stderr unless file was specified via:
+                *   ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file)
+                */
+               file = stderr;
+       }
+
+#ifdef HAVE_WINSOCK
+       /*
+        * Stick the time in the buffer to output when using Winsock
+        * as NT can't pipe to a timestamp program like Unix can.
+        * This, of course, makes some logs hard to read.
+     */
        time( &now );
        today = localtime( &now );
-       sprintf( buffer, "%4d%02d%02d:%02d:%02d:%02d ",
+       fprintf( file, "%4d%02d%02d:%02d:%02d:%02d ",
                today->tm_year + 1900, today->tm_mon + 1,
                today->tm_mday, today->tm_hour,
                today->tm_min, today->tm_sec );
-
-        /*
-         * format the output data.
-         */
-#ifdef HAVE_VSNPRINTF
-       vsnprintf( &buffer[18], sizeof(buffer)-18, fmt, vl );
-#else
-       vsprintf( &buffer[18], fmt, vl );
 #endif
-       buffer[sizeof(buffer)-1] = '\0';
 
-        /*
-         * If the user set up a file using 
-         * ber_set_option( NULL, LBER_OPT_LOG_PRINT_FILE, file), use
-         * it.  Otherwise, just output to stderr.
-         */
-       if( file != NULL ) {
-               fputs( buffer, file );
-               fflush( file );
-       }
-        else
-        {
-            fputs( buffer, stderr );
-        }
-
-/*
- * Kurt or someone needs to decide what to do about this.  This
- * code will log to syslog if the level is less than a normal
- * debug level (meaning a warning or error of some kind).  However,
- * having the code here means that ldap_syslog has to be defined.
- */
-#if 0
-#ifdef LDAP_SYSLOG
-       if ( level < LDAP_LEVEL_ENTRY && level >= ldap_syslog )
-       {
-               syslog( level, buffer );
-       }
-#endif
-#endif
+       /*
+        * format the output data.
+        */
+       vfprintf( file, fmt, vl );
 }
 
 /*
- * 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;
@@ -240,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;
+           }
+       }
     }
 }