Author: julianfoad
Date: Fri Aug  3 19:49:28 2012
New Revision: 1369183

URL: http://svn.apache.org/viewvc?rev=1369183&view=rev
Log:
Make the SVN_DBG macro print the prefix before every line of output if the
formatted string contains embedded newline characters.  Also make a final
newline character optional so that the developer can write SVN_DBG(("..."))
instead of SVN_DBG(("...\n")).

* subversion/libsvn_subr/debug.c
  (debug_pool, debug_file, debug_line): New variables.
  (svn_dbg__preamble): Create a pool, the first time we're called. Just
    store the prefix information instead of printing it. (Note: this isn't
    thread-safe; it already wasn't.)
  (print_line): Rename to debug_vprintf. Prefix each line of output.
  (svn_dbg__printf, svn_dbg__print_props): Track the rename.

Modified:
    subversion/trunk/subversion/libsvn_subr/debug.c

Modified: subversion/trunk/subversion/libsvn_subr/debug.c
URL: 
http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/debug.c?rev=1369183&r1=1369182&r2=1369183&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/debug.c (original)
+++ subversion/trunk/subversion/libsvn_subr/debug.c Fri Aug  3 19:49:28 2012
@@ -26,6 +26,8 @@
    builds is that this code is not thread-safe. */
 #include <stdarg.h>
 
+#include <apr_pools.h>
+#include <apr_strings.h>
 #include "svn_types.h"
 #include "svn_string.h"
 
@@ -35,6 +37,9 @@
 #define DBG_FLAG "DBG: "
 
 /* This will be tweaked by the preamble code.  */
+static apr_pool_t *debug_pool = NULL;
+static const char *debug_file = NULL;
+static long debug_line = 0;
 static FILE * volatile debug_output = NULL;
 
 
@@ -48,6 +53,9 @@ quiet_mode(void)
 void
 svn_dbg__preamble(const char *file, long line, FILE *output)
 {
+  if (! debug_pool)
+    apr_pool_create(&debug_pool, NULL);
+
   debug_output = output;
 
   if (output != NULL && !quiet_mode())
@@ -57,25 +65,45 @@ svn_dbg__preamble(const char *file, long
 
       if (slash == NULL)
         slash = strrchr(file, '\\');
-      if (slash == NULL)
-        slash = file;
+      if (slash)
+        debug_file = slash + 1;
       else
-        ++slash;
-
-      fprintf(output, DBG_FLAG "%s:%4ld: ", slash, line);
+        debug_file = file;
     }
+  debug_line = line;
 }
 
 
+/* Print a formatted string using format FMT and argument-list AP,
+ * prefixing each line of output with a debug header. */
 static void
-print_line(const char *fmt, va_list ap)
+debug_vprintf(const char *fmt, va_list ap)
 {
   FILE *output = debug_output;
+  const char *prefix;
+  char *s;
 
   if (output == NULL || quiet_mode())
     return;
 
-  (void) vfprintf(output, fmt, ap);
+  prefix = apr_psprintf(debug_pool, DBG_FLAG "%s:%4ld: ",
+                        debug_file, debug_line);
+  s = apr_pvsprintf(debug_pool, fmt, ap);
+  do
+    {
+      char *newline = strchr(s, '\n');
+      if (newline)
+        *newline = '\0';
+
+      fputs(prefix, output);
+      fputs(s, output);
+      fputc('\n', output);
+
+      if (! newline)
+        break;
+      s = newline + 1;
+    }
+  while (*s);  /* print another line, except after a final newline */
 }
 
 
@@ -85,7 +113,7 @@ svn_dbg__printf(const char *fmt, ...)
   va_list ap;
 
   va_start(ap, fmt);
-  print_line(fmt, ap);
+  debug_vprintf(fmt, ap);
   va_end(ap);
 }
 
@@ -102,7 +130,7 @@ svn_dbg__print_props(apr_hash_t *props,
   va_list ap;
 
   va_start(ap, header_fmt);
-  print_line(header_fmt, ap);
+  debug_vprintf(header_fmt, ap);
   va_end(ap);
 
   if (props == NULL)


Reply via email to