Fix ndrdump to use a common setup_logging() API
[ira/wip.git] / source3 / lib / debug.c
index 49ec40ae848f0b72cfbd0b2c8e5d427575797094..e7dcfb4fdf3f6b2e887224a7055f00612f5016b1 100644 (file)
@@ -90,10 +90,11 @@ bool    AllowDebugChange = True;
 */
 bool    override_logfile;
 
+static TALLOC_CTX *tmp_debug_ctx;
 
 /*
  * This is to allow assignment to DEBUGLEVEL before the debug
- * system has been initialised.
+ * system has been initialized.
  */
 static int debug_all_class_hack = 1;
 static bool debug_all_class_isset_hack = True;
@@ -182,6 +183,8 @@ static char **classname_table = NULL;
  Free memory pointed to by global pointers.
 ****************************************************************************/
 
+static bool initialized;
+
 void gfree_debugsyms(void)
 {
        int i;
@@ -193,11 +196,23 @@ void gfree_debugsyms(void)
                SAFE_FREE( classname_table );
        }
 
-       if ( DEBUGLEVEL_CLASS != &debug_all_class_hack )
+       if ( DEBUGLEVEL_CLASS != &debug_all_class_hack ) {
                SAFE_FREE( DEBUGLEVEL_CLASS );
+               DEBUGLEVEL_CLASS = &debug_all_class_hack;
+       }
 
-       if ( DEBUGLEVEL_CLASS_ISSET != &debug_all_class_isset_hack )
+       if ( DEBUGLEVEL_CLASS_ISSET != &debug_all_class_isset_hack ) {
                SAFE_FREE( DEBUGLEVEL_CLASS_ISSET );
+               DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack;
+       }
+
+       SAFE_FREE(format_bufr);
+
+       debug_num_classes = 0;
+
+       debug_level = DEBUGLEVEL_CLASS;
+
+       initialized = false;
 }
 
 /****************************************************************************
@@ -426,8 +441,9 @@ static bool debug_parse_params(char **params)
 
        /* Fill in new debug class levels */
        for (; i < debug_num_classes && params[i]; i++) {
-               if ((class_name=strtok(params[i],":")) &&
-                       (class_level=strtok(NULL, "\0")) &&
+               char *saveptr;
+               if ((class_name = strtok_r(params[i],":", &saveptr)) &&
+                       (class_level = strtok_r(NULL, "\0", &saveptr)) &&
             ((ndx = debug_lookup_classname(class_name)) != -1)) {
                                DEBUGLEVEL_CLASS[ndx] = atoi(class_level);
                                DEBUGLEVEL_CLASS_ISSET[ndx] = True;
@@ -456,14 +472,14 @@ bool debug_parse_levels(const char *params_str)
        if (AllowDebugChange == False)
                return True;
 
-       params = str_list_make(params_str, NULL);
+       params = str_list_make_v3(talloc_tos(), params_str, NULL);
 
        if (debug_parse_params(params)) {
                debug_dump_status(5);
-               str_list_free(&params);
+               TALLOC_FREE(params);
                return True;
        } else {
-               str_list_free(&params);
+               TALLOC_FREE(params);
                return False;
        }
 }
@@ -472,7 +488,7 @@ bool debug_parse_levels(const char *params_str)
  Receive a "set debug level" message.
 ****************************************************************************/
 
-static void debug_message(struct messaging_context *msg_ctx,
+void debug_message(struct messaging_context *msg_ctx,
                          void *private_data, 
                          uint32_t msg_type, 
                          struct server_id src,
@@ -526,18 +542,17 @@ Init debugging (one time stuff)
 
 void debug_init(void)
 {
-       static bool initialised = False;
        const char **p;
 
-       if (initialised)
+       if (initialized)
                return;
 
-       initialised = True;
+       initialized = true;
 
        for(p = default_classname_table; *p; p++) {
                debug_add_class(*p);
        }
-       format_bufr = SMB_MALLOC(FORMAT_BUFR_SIZE);
+       format_bufr = (char *)SMB_MALLOC(FORMAT_BUFR_SIZE);
        if (!format_bufr) {
                smb_panic("debug_init: unable to create buffer");
        }
@@ -563,7 +578,9 @@ void setup_logging(const char *pname, bool interactive)
        stdout_logging = False;
        if (dbf) {
                x_fflush(dbf);
-               (void) x_fclose(dbf);
+                if (dbf != x_stdout) {
+                        (void) x_fclose(dbf);
+                }
        }
 
        dbf = NULL;
@@ -588,6 +605,15 @@ void setup_logging(const char *pname, bool interactive)
 #endif
 }
 
+/**
+   Just run logging to stdout for this program 
+*/
+_PUBLIC_ void setup_logging_stdout(void)
+{
+       setup_logging(NULL, True);
+}
+
+
 /***************************************************************************
  Set the logfile name.
 **************************************************************************/
@@ -663,8 +689,8 @@ bool reopen_logs( void )
        force_check_log_size();
        (void)umask(oldumask);
 
-       /* Take over stderr to catch ouput into logs */
-       if (dbf && sys_dup2(x_fileno(dbf), 2) == -1) {
+       /* Take over stderr to catch output into logs */
+       if (dbf && dup2(x_fileno(dbf), 2) == -1) {
                close_low_fds(True); /* Close stderr too, if dup2 can't point it
                                        at the logfile */
        }
@@ -722,7 +748,7 @@ void check_log_size( void )
 
        maxlog = lp_max_log_size() * 1024;
 
-       if( sys_fstat( x_fileno( dbf ), &st ) == 0 && st.st_size > maxlog ) {
+       if( sys_fstat( x_fileno( dbf ), &st ) == 0 && st.st_ex_size > maxlog ) {
                (void)reopen_logs();
                if( dbf && get_file_size( debugf ) > maxlog ) {
                        char *name = NULL;
@@ -784,13 +810,13 @@ void check_log_size( void )
                        (void)x_vfprintf( dbf, format_str, ap );
                va_end( ap );
                errno = old_errno;
-               return( 0 );
+               goto done;
        }
 
        /* prevent recursion by checking if reopen_logs() has temporaily
           set the debugf string to NULL */
        if( debugf == NULL)
-               return( 0 );
+               goto done;
 
 #ifdef WITH_SYSLOG
        if( !lp_syslog_only() )
@@ -805,7 +831,7 @@ void check_log_size( void )
                                x_setbuf( dbf, NULL );
                        } else {
                                errno = old_errno;
-                               return(0);
+                               goto done;
                        }
                }
        }
@@ -815,7 +841,7 @@ void check_log_size( void )
                /* map debug levels to syslog() priorities
                 * note that not all DEBUG(0, ...) calls are
                 * necessarily errors */
-               static int priority_map[] = {
+               static const int priority_map[4] = {
                        LOG_ERR,     /* 0 */
                        LOG_WARNING, /* 1 */
                        LOG_NOTICE,  /* 2 */
@@ -823,17 +849,18 @@ void check_log_size( void )
                };
                int     priority;
                char *msgbuf = NULL;
+               int ret;
 
-               if( syslog_level >= ( sizeof(priority_map) / sizeof(priority_map[0]) ) || syslog_level < 0)
+               if( syslog_level >= ARRAY_SIZE(priority_map) || syslog_level < 0)
                        priority = LOG_DEBUG;
                else
                        priority = priority_map[syslog_level];
 
                va_start(ap, format_str);
-               vasprintf(&msgbuf, format_str, ap);
+               ret = vasprintf(&msgbuf, format_str, ap);
                va_end(ap);
 
-               if (msgbuf) {
+               if (ret != -1) {
                        syslog(priority, "%s", msgbuf);
                }
                SAFE_FREE(msgbuf);
@@ -854,6 +881,9 @@ void check_log_size( void )
                        (void)x_fflush( dbf );
        }
 
+ done:
+       TALLOC_FREE(tmp_debug_ctx);
+
        errno = old_errno;
 
        return( 0 );
@@ -894,6 +924,10 @@ static void format_debug_text( const char *msg )
        size_t i;
        bool timestamp = (!stdout_logging && (lp_timestamp_logs() || !(lp_loaded())));
 
+       if (!format_bufr) {
+               debug_init();
+       }
+
        for( i = 0; msg[i]; i++ ) {
                /* Indent two spaces at each new line. */
                if(timestamp && 0 == format_pos) {
@@ -959,7 +993,7 @@ void dbgflush( void )
 
 ****************************************************************************/
 
-bool dbghdr(int level, int cls, const char *file, const char *func, int line)
+bool dbghdrclass(int level, int cls, const char *location, const char *func)
 {
        /* Ensure we don't lose any real errno value. */
        int old_errno = errno;
@@ -1017,12 +1051,14 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
                /* Print it all out at once to prevent split syslog output. */
                if( lp_debug_prefix_timestamp() ) {
                    (void)Debug1( "[%s, %2d%s] ",
-                       current_timestring(lp_debug_hires_timestamp()), level,
-                       header_str);
+                       current_timestring(debug_ctx(),
+                                          lp_debug_hires_timestamp()),
+                       level, header_str);
                } else {
-                   (void)Debug1( "[%s, %2d%s] %s:%s(%d)\n",
-                       current_timestring(lp_debug_hires_timestamp()), level,
-                       header_str, file, func, line );
+                   (void)Debug1( "[%s, %2d%s] %s(%s)\n",
+                       current_timestring(debug_ctx(),
+                                          lp_debug_hires_timestamp()),
+                       level, header_str, location, func );
                }
        }
 
@@ -1030,6 +1066,12 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
        return( True );
 }
 
+bool dbghdr(int level, const char *location, const char *func)
+{
+       /* For compatibility with Samba 4, which doesn't have debug classes */
+       return dbghdrclass(level, 0, location, func);
+}
+
 /***************************************************************************
  Add text to the body of the "current" debug message via the format buffer.
 
@@ -1048,12 +1090,13 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
        va_list ap;
        char *msgbuf = NULL;
        bool ret = true;
+       int res;
 
        va_start(ap, format_str);
-       vasprintf(&msgbuf, format_str, ap);
+       res = vasprintf(&msgbuf, format_str, ap);
        va_end(ap);
 
-       if (msgbuf) {
+       if (res != -1) {
                format_debug_text(msgbuf);
        } else {
                ret = false;
@@ -1061,3 +1104,14 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
        SAFE_FREE(msgbuf);
        return ret;
 }
+
+/*
+ * Get us a temporary talloc context usable just for DEBUG arguments
+ */
+TALLOC_CTX *debug_ctx(void)
+{
+        if (tmp_debug_ctx == NULL) {
+                tmp_debug_ctx = talloc_named_const(NULL, 0, "debug_ctx");
+        }
+        return tmp_debug_ctx;
+}