1. Stephen Tu
  2. mino-pypy

Commits

Armin Rigo  committed bc03770

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

  • Participants
  • Parent commits d841f7a
  • Branches stm-thread-2

Comments (0)

Files changed (4)

File pypy/translator/c/funcgen.py

View file
  • Ignore whitespace
     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

File pypy/translator/c/src/debug_print.c

View file
  • Ignore whitespace
 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;
 
           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);
     }
 }
 
       /* 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);
 }

File pypy/translator/c/src/debug_print.h

View file
  • Ignore whitespace
 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)

File pypy/translator/c/test/test_standalone.py

View file
  • Ignore whitespace
         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]