Author: Armin Rigo <[email protected]>
Branch: stm-thread-2
Changeset: r59403:bc037707e578
Date: 2012-12-11 17:01 +0100
http://bitbucket.org/pypy/pypy/changeset/bc037707e578/

Log:    Prefix all lines printed in PYPYLOG with a thread number (in color
        too, if the output is a tty).

diff --git a/pypy/translator/c/funcgen.py b/pypy/translator/c/funcgen.py
--- a/pypy/translator/c/funcgen.py
+++ b/pypy/translator/c/funcgen.py
@@ -745,8 +745,8 @@
     def OP_DEBUG_PRINT(self, op):
         # XXX
         from pypy.rpython.lltypesystem.rstr import STR
-        format = []
-        argv = []
+        format = ['%s']
+        argv = ['pypy_debug_threadid']
         free_line = ""
         for arg in op.args:
             T = arg.concretetype
diff --git a/pypy/translator/c/src/debug_print.c 
b/pypy/translator/c/src/debug_print.c
--- a/pypy/translator/c/src/debug_print.c
+++ b/pypy/translator/c/src/debug_print.c
@@ -21,8 +21,9 @@
 FILE *pypy_debug_file = NULL;   /* XXX make it thread-local too? */
 static unsigned char debug_ready = 0;
 static unsigned char debug_profile = 0;
-__thread char *debug_start_colors_1 = NULL;
-__thread char *debug_start_colors_2 = NULL;
+__thread char debug_start_colors_1[32];
+__thread char debug_start_colors_2[28];
+__thread char pypy_debug_threadid[16];
 static char *debug_stop_colors = "";
 static char *debug_prefix = NULL;
 
@@ -136,38 +137,38 @@
           debug_stop_colors);
 }
 
+typedef Unsigned revision_t;
 #ifdef RPY_STM
-typedef Unsigned revision_t;
-#include <src_stm/atomic_ops.h>
-static volatile revision_t threadcolor = 0;
+# include <src_stm/atomic_ops.h>
+#else
+# define bool_cas(vp, o, n) (*(vp)=(n), 1)
 #endif
+static volatile revision_t threadcounter = 0;
 
 static void _prepare_display_colors(void)
 {
+    revision_t counter;
+    char *p;
+    while (1) {
+        counter = threadcounter;
+        if (bool_cas(&threadcounter, counter, counter + 1))
+            break;
+    }
     if (debug_stop_colors[0] == 0) {
-        debug_start_colors_1 = "";
-        debug_start_colors_2 = "";
+        /* not a tty output: no colors */
+        sprintf(debug_start_colors_1, "%d# ", (int)counter);
+        sprintf(debug_start_colors_2, "%d# ", (int)counter);
+        sprintf(pypy_debug_threadid, "%d#", (int)counter);
     }
     else {
-#ifndef RPY_STM
-        debug_start_colors_1 = "\033[1m\033[31m";
-        debug_start_colors_2 = "\033[31m";
-#else
-        revision_t color;
-        char *p;
-        while (1) {
-            color = threadcolor;
-            if (bool_cas(&threadcolor, color, color + 1))
-                break;
-        }
-        color = 31 + (color % 7);
-        p = malloc(20);    /* leak */
-        sprintf(p, "\033[1m\033[%dm", (int)color);
-        debug_start_colors_1 = p;
-        p = malloc(16);
-        sprintf(p, "\033[%dm", (int)color);
-        debug_start_colors_2 = p;
-#endif
+        /* tty output */
+        int color = 31 + (int)(counter % 7);
+        sprintf(debug_start_colors_1, "\033[%dm%d# \033[1m",
+                color, (int)counter);
+        sprintf(debug_start_colors_2, "\033[%dm%d# ",
+                color, (int)counter);
+        sprintf(pypy_debug_threadid, "\033[%dm%d#\033[0m",
+                color, (int)counter);
     }
 }
 
@@ -190,7 +191,7 @@
       /* else make this subsection active */
       pypy_have_debug_prints |= 1;
     }
-  if (!debug_start_colors_1)
+  if (!debug_start_colors_1[0])
     _prepare_display_colors();
   display_startstop("{", "", category, debug_start_colors_1);
 }
diff --git a/pypy/translator/c/src/debug_print.h 
b/pypy/translator/c/src/debug_print.h
--- a/pypy/translator/c/src/debug_print.h
+++ b/pypy/translator/c/src/debug_print.h
@@ -39,6 +39,7 @@
 long pypy_debug_offset(void);
 
 extern __thread long pypy_have_debug_prints;
+extern __thread char pypy_debug_threadid[];
 extern FILE *pypy_debug_file;
 
 #define OP_LL_READ_TIMESTAMP(val) READ_TIMESTAMP(val)
diff --git a/pypy/translator/c/test/test_standalone.py 
b/pypy/translator/c/test/test_standalone.py
--- a/pypy/translator/c/test/test_standalone.py
+++ b/pypy/translator/c/test/test_standalone.py
@@ -435,7 +435,7 @@
         out, err = cbuilder.cmdexec("foo bar", err=True, env={'PYPYLOG': ':-'})
         lines = err.splitlines()
         assert '{foo' in lines[0]
-        assert 'bar' == lines[1]
+        assert '0# bar' == lines[1]
         assert 'foo}' in lines[2]
 
 
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to