[PATCH] gnc-trace fixes and improvements

Chris Shoemaker c.shoemaker at cox.net
Fri Dec 31 16:50:11 EST 2004


 * Recent use of malloc in gnc-trace breaks my compile, use g_malloc
 * Fix leak of filename mem
 * add indenting of trace file according to ENTER/LEAVE stack depth
 * have ENTER report file name of function along with function name
-------------- next part --------------
Index: src/engine/gnc-trace.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-trace.c,v
retrieving revision 1.2.20.9
diff -u -r1.2.20.9 gnc-trace.c
--- src/engine/gnc-trace.c	30 Dec 2004 21:37:22 -0000	1.2.20.9
+++ src/engine/gnc-trace.c	31 Dec 2004 21:40:53 -0000
@@ -73,14 +73,21 @@
 static FILE *fout = NULL;
 static const int MAX_TRACE_FILENAME = 100;
 
+/* Don't be fooled: gnc_trace_num_spaces has external linkage and
+   static storage, but can't be defined with 'extern' because it has
+   an initializer, and can't be declared with 'static' because that
+   would give it internal linkage. */
+gint __attribute__ ((unused)) gnc_trace_num_spaces = 0;
+
 static void
 fh_printer (const gchar   *log_domain,
             GLogLevelFlags    log_level,
             const gchar   *message,
             gpointer    user_data)
 {
+  extern gint gnc_trace_num_spaces;
   FILE *fh = user_data;
-  fprintf (fh, "%s\n", message);
+  fprintf (fh, "%*s%s\n", gnc_trace_num_spaces, "", message);
   fflush(fh);
 }
 
@@ -91,9 +98,11 @@
 
    fout = fopen ("/tmp/gnucash.trace", "w");
 
-   if(!fout && (filename = (char *)malloc(MAX_TRACE_FILENAME))) {
-      snprintf(filename, MAX_TRACE_FILENAME-1, "/tmp/gnucash.trace.%d", getpid());
+   if(!fout && (filename = (char *)g_malloc(MAX_TRACE_FILENAME))) {
+      snprintf(filename, MAX_TRACE_FILENAME-1, "/tmp/gnucash.trace.%d", 
+	       getpid());
       fout = fopen (filename, "w");
+      g_free(filename);
    }
 
    if(!fout)
Index: src/engine/gnc-trace.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-trace.h,v
retrieving revision 1.3.4.6
diff -u -r1.3.4.6 gnc-trace.h
--- src/engine/gnc-trace.h	7 Jul 2004 04:10:29 -0000	1.3.4.6
+++ src/engine/gnc-trace.h	31 Dec 2004 21:40:53 -0000
@@ -83,6 +83,8 @@
 
 //extern gncLogLevel gnc_log_modules[MOD_LAST + 1];
 
+#define GNC_TRACE_INDENT_WIDTH 4
+
 /** Initialize the error logging subsystem */
 void gnc_log_init (void);
 
@@ -105,7 +107,7 @@
 const char * gnc_log_prettify (const char *name);
 
 /* We want logging decisions to be made inline, rather than through
- * a CPU-cucking subroutine call. Thus, this is a #define, not a
+ * a CPU-sucking subroutine call. Thus, this is a #define, not a
  * subroutine call.  The prototype would have been:
  * gboolean gnc_should_log (gncModuleType module, gncLogLevel log_level); 
  *
@@ -160,7 +162,8 @@
 #define PINFO(format, args...) {                   \
   if (gnc_should_log (module, GNC_LOG_INFO)) {     \
     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO,         \
-      "Info: %s(): " format, FUNK , ## args);      \
+      "Info: %s(): " format,                       \
+      FUNK , ## args);                             \
   }                                                \
 }
 
@@ -168,23 +171,30 @@
 #define DEBUG(format, args...) {                   \
   if (gnc_should_log (module, GNC_LOG_DEBUG)) {    \
     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,        \
-      "Debug: %s(): " format, FUNK , ## args);     \
+      "Debug: %s(): " format,                      \
+      FUNK , ## args);                             \
   }                                                \
 }
 
 /** Print an function entry debugging message */
 #define ENTER(format, args...) {                   \
+  extern gint gnc_trace_num_spaces;                \
   if (gnc_should_log (module, GNC_LOG_DEBUG)) {    \
     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,        \
-      "Enter: %s" format, FUNK , ## args);         \
+      "Enter in %s: %s()" format, __FILE__,        \
+      FUNK , ## args);                             \
+    gnc_trace_num_spaces += GNC_TRACE_INDENT_WIDTH;\
   }                                                \
 }
 
 /** Print an function exit debugging message */
 #define LEAVE(format, args...) {                   \
+  extern gint gnc_trace_num_spaces;                \
   if (gnc_should_log (module, GNC_LOG_DEBUG)) {    \
+    gnc_trace_num_spaces -= GNC_TRACE_INDENT_WIDTH;\
     g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,        \
-      "Leave: %s" format, FUNK , ## args);         \
+      "Leave: %s()" format,                        \
+      FUNK , ## args);                             \
   }                                                \
 }
 


More information about the gnucash-patches mailing list