[pypy-svn] r68866 - in pypy/branch/logging2/pypy: config rlib rlib/test rpython/lltypesystem translator/c/src translator/c/test

arigo at codespeak.net arigo at codespeak.net
Fri Oct 30 15:22:00 CET 2009


Author: arigo
Date: Fri Oct 30 15:21:57 2009
New Revision: 68866

Modified:
   pypy/branch/logging2/pypy/config/translationoption.py
   pypy/branch/logging2/pypy/rlib/debug.py
   pypy/branch/logging2/pypy/rlib/test/test_debug.py
   pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py
   pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py
   pypy/branch/logging2/pypy/translator/c/src/debug.h
   pypy/branch/logging2/pypy/translator/c/test/test_standalone.py
Log:
Add debug_level(), a function/operation to read the debug level from a program.


Modified: pypy/branch/logging2/pypy/config/translationoption.py
==============================================================================
--- pypy/branch/logging2/pypy/config/translationoption.py	(original)
+++ pypy/branch/logging2/pypy/config/translationoption.py	Fri Oct 30 15:21:57 2009
@@ -40,6 +40,9 @@
                      },
                  cmdline="-b --backend"),
 
+    BoolOption("log", "Include debug prints in the translation (PYPYLOG=...)",
+               default=True, cmdline="--log"),
+
     # gc
     ChoiceOption("gc", "Garbage Collection Strategy",
                  ["boehm", "ref", "marksweep", "semispace", "statistics",

Modified: pypy/branch/logging2/pypy/rlib/debug.py
==============================================================================
--- pypy/branch/logging2/pypy/rlib/debug.py	(original)
+++ pypy/branch/logging2/pypy/rlib/debug.py	Fri Oct 30 15:21:57 2009
@@ -57,6 +57,37 @@
         hop.genop(fn.__name__, vlist)
 
 
+def debug_level():
+    """Returns the current debug level.  It is:
+         0: all calls to debug_print/debug_start/debug_stop are ignored
+         1: profiling: debug_start/debug_stop handled, but debug_print ignored
+         2: all handled
+    The value returned depend on the runtime presence of the PYPYLOG env var,
+    unless debuglevel == 0 in the config.
+    """
+    return 2
+
+class Entry(ExtRegistryEntry):
+    _about_ = debug_level
+
+    def compute_result_annotation(self):
+        from pypy.annotation import model as annmodel
+        t = self.bookkeeper.annotator.translator
+        if t.config.translation.log:
+            return annmodel.SomeInteger()
+        else:
+            return self.bookkeeper.immutablevalue(0)
+
+    def specialize_call(self, hop):
+        from pypy.rpython.lltypesystem import lltype
+        t = hop.rtyper.annotator.translator
+        hop.exception_cannot_occur()
+        if t.config.translation.log:
+            return hop.genop('debug_level', [], resulttype=lltype.Signed)
+        else:
+            return hop.inputconst(lltype.Signed, 0)
+
+
 def llinterpcall(RESTYPE, pythonfunction, *args):
     """When running on the llinterp, this causes the llinterp to call to
     the provided Python function with the run-time value of the given args.

Modified: pypy/branch/logging2/pypy/rlib/test/test_debug.py
==============================================================================
--- pypy/branch/logging2/pypy/rlib/test/test_debug.py	(original)
+++ pypy/branch/logging2/pypy/rlib/test/test_debug.py	Fri Oct 30 15:21:57 2009
@@ -1,7 +1,7 @@
 
 import py
 from pypy.rlib.debug import check_annotation, make_sure_not_resized
-from pypy.rlib.debug import debug_print, debug_start, debug_stop
+from pypy.rlib.debug import debug_print, debug_start, debug_stop, debug_level
 from pypy.rpython.test.test_llinterp import interpret
 
 def test_check_annotation():
@@ -50,11 +50,13 @@
             debug_start("mycat")
             debug_print("foo", 2, "bar", x)
             debug_stop("mycat")
+            return debug_level()
 
         olderr = sys.stderr
         try:
             sys.stderr = c = StringIO()
-            f(3)
+            res = f(3)
+            assert res == 2
         finally:
             sys.stderr = olderr
         assert 'mycat' in c.getvalue()
@@ -62,7 +64,8 @@
 
         try:
             sys.stderr = c = StringIO()
-            self.interpret(f, [3])
+            res = self.interpret(f, [3])
+            assert res == 2
         finally:
             sys.stderr = olderr
         assert 'mycat' in c.getvalue()
@@ -71,8 +74,8 @@
 
 class TestLLType(DebugTests):
     def interpret(self, f, args):
-        interpret(f, args, type_system='lltype')
+        return interpret(f, args, type_system='lltype')
 
 class TestOOType(DebugTests):
     def interpret(self, f, args):
-        interpret(f, args, type_system='ootype')
+        return interpret(f, args, type_system='ootype')

Modified: pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py
==============================================================================
--- pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py	(original)
+++ pypy/branch/logging2/pypy/rpython/lltypesystem/lloperation.py	Fri Oct 30 15:21:57 2009
@@ -525,6 +525,7 @@
     'debug_print':          LLOp(canrun=True),
     'debug_start':          LLOp(canrun=True),
     'debug_stop':           LLOp(canrun=True),
+    'debug_level':          LLOp(canrun=True),
     'debug_pdb':            LLOp(),
     'debug_assert':         LLOp(tryfold=True),
     'debug_fatalerror':     LLOp(),

Modified: pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py
==============================================================================
--- pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py	(original)
+++ pypy/branch/logging2/pypy/rpython/lltypesystem/opimpl.py	Fri Oct 30 15:21:57 2009
@@ -429,6 +429,9 @@
 def op_debug_stop(category):
     debug.debug_stop(_normalize(category))
 
+def op_debug_level():
+    return debug.debug_level()
+
 def op_gc_stack_bottom():
     pass       # marker for trackgcroot.py
 

Modified: pypy/branch/logging2/pypy/translator/c/src/debug.h
==============================================================================
--- pypy/branch/logging2/pypy/translator/c/src/debug.h	(original)
+++ pypy/branch/logging2/pypy/translator/c/src/debug.h	Fri Oct 30 15:21:57 2009
@@ -11,21 +11,33 @@
 
 
 /* macros used by the generated code */
-#define PYPY_DEBUG_ENABLED     (pypy_debug_full && pypy_debug_is_ready())
-#define PYPY_DEBUG_FILE        pypy_debug_file
-#define PYPY_DEBUG_START(cat)  if (pypy_debug_profiling) pypy_debug_start(cat)
-#define PYPY_DEBUG_STOP(cat)   if (pypy_debug_profiling) pypy_debug_stop(cat)
+#define PYPY_DEBUG_ENABLED        \
+           (pypy_debug_level >= PYDEBUG_FULL && pypy_debug_is_ready_full())
+#define PYPY_DEBUG_FILE           \
+           pypy_debug_file
+#define PYPY_DEBUG_START(cat)     \
+           if (pypy_debug_level >= PYDEBUG_PROFILE) pypy_debug_start(cat)
+#define PYPY_DEBUG_STOP(cat)      \
+           if (pypy_debug_level >= PYDEBUG_PROFILE) pypy_debug_stop(cat)
+#define OP_DEBUG_LEVEL(r)         \
+           if (pypy_debug_level == PYDEBUG_UNINITIALIZED) pypy_debug_open(); \
+           r = pypy_debug_level
 
 
 /************************************************************/
 
+#define PYDEBUG_OFF             0
+#define PYDEBUG_PROFILE         1
+#define PYDEBUG_FULL            2
+#define PYDEBUG_UNINITIALIZED   3
+
 /* prototypes (internal use only) */
-bool_t pypy_debug_is_ready(void);
+void pypy_debug_open(void);
+bool_t pypy_debug_is_ready_full(void);
 void pypy_debug_start(const char *category);
 void pypy_debug_stop(const char *category);
 
-extern bool_t pypy_debug_full;
-extern bool_t pypy_debug_profiling;
+extern int pypy_debug_level;
 extern FILE *pypy_debug_file;
 
 
@@ -34,15 +46,13 @@
 #ifndef PYPY_NOT_MAIN_FILE
 #include <sys/time.h>
 
-static bool_t pypy_debug_initialized = 0;
-bool_t pypy_debug_full = 1;      /* set to 0 if PYPYLOG is not defined
-                                    or starts with 'prof:'             */
-bool_t pypy_debug_profiling = 1; /* set to 0 if PYPYLOG is not defined */
+int pypy_debug_level = PYDEBUG_UNINITIALIZED;
 FILE *pypy_debug_file;
 
-static void pypy_debug_open(void)
+void pypy_debug_open(void)
 {
   char *filename = getenv("PYPYLOG");
+  pypy_debug_level = PYDEBUG_FULL;
   if (filename && filename[0])
     {
       if (filename[0] == 'p' &&
@@ -51,7 +61,7 @@
           filename[3] == 'f' &&
           filename[4] == ':')
         {
-          pypy_debug_full = 0;
+          pypy_debug_level = PYDEBUG_PROFILE;
           filename += 5;
         }
       if (filename[0] == '-' && filename[1] == 0)
@@ -63,16 +73,15 @@
     {
       pypy_debug_file = NULL;
     }
-  pypy_debug_full      &= (pypy_debug_file != NULL);
-  pypy_debug_profiling &= (pypy_debug_file != NULL);
-  pypy_debug_initialized = 1;
+  if (pypy_debug_file == NULL)
+    pypy_debug_level = PYDEBUG_OFF;
 }
 
-bool_t pypy_debug_is_ready(void)
+bool_t pypy_debug_is_ready_full(void)
 {
-  if (!pypy_debug_initialized)
+  if (pypy_debug_level == PYDEBUG_UNINITIALIZED)
     pypy_debug_open();
-  return pypy_debug_full;
+  return pypy_debug_level == PYDEBUG_FULL;
 }
 
 
@@ -84,9 +93,9 @@
 static void pypy_debug_category(const char *start, const char *category)
 {
   long long timestamp;
-  if (!pypy_debug_initialized)
+  if (!pypy_debug_level == PYDEBUG_UNINITIALIZED)
     pypy_debug_open();
-  if (!pypy_debug_profiling)
+  if (pypy_debug_level < PYDEBUG_PROFILE)
     return;
   READ_TIMESTAMP(timestamp);
   fprintf(pypy_debug_file, "{%llx} -%s- %s\n", timestamp, start, category);

Modified: pypy/branch/logging2/pypy/translator/c/test/test_standalone.py
==============================================================================
--- pypy/branch/logging2/pypy/translator/c/test/test_standalone.py	(original)
+++ pypy/branch/logging2/pypy/translator/c/test/test_standalone.py	Fri Oct 30 15:21:57 2009
@@ -2,7 +2,8 @@
 import sys, os, re
 
 from pypy.rlib.rarithmetic import r_longlong
-from pypy.rlib.debug import ll_assert, debug_print, debug_start, debug_stop
+from pypy.rlib.debug import ll_assert
+from pypy.rlib.debug import debug_print, debug_start, debug_stop, debug_level
 from pypy.translator.translator import TranslationContext
 from pypy.translator.backendopt import all
 from pypy.translator.c.genc import CStandaloneBuilder, ExternalCompilationInfo
@@ -257,6 +258,7 @@
 
     def test_debug_print_start_stop(self):
         def entry_point(argv):
+            os.write(1, str(debug_level()) + '\n')
             debug_start("mycat")
             debug_print("foo", 2, "bar", 3)
             debug_stop("mycat")
@@ -264,21 +266,21 @@
         t, cbuilder = self.compile(entry_point)
         # check with PYPYLOG undefined
         out, err = cbuilder.cmdexec("", err=True, env={})
-        assert not out
+        assert out.strip() == '0'
         assert not err
         # check with PYPYLOG defined to an empty string (same as undefined)
         out, err = cbuilder.cmdexec("", err=True, env={'PYPYLOG': ''})
-        assert not out
+        assert out.strip() == '0'
         assert not err
         # check with PYPYLOG=- (means print to stderr)
         out, err = cbuilder.cmdexec("", err=True, env={'PYPYLOG': '-'})
-        assert not out
+        assert out.strip() == '2'
         assert 'mycat' in err
         assert 'foo 2 bar 3' in err
         # check with PYPYLOG=somefilename
         path = udir.join('test_debug_xxx.log')
         out, err = cbuilder.cmdexec("", err=True, env={'PYPYLOG': str(path)})
-        assert not out
+        assert out.strip() == '2'
         assert not err
         assert path.check(file=1)
         assert 'mycat' in path.read()
@@ -287,7 +289,7 @@
         path = udir.join('test_debug_xxx_prof.log')
         out, err = cbuilder.cmdexec("", err=True,
                                     env={'PYPYLOG': 'prof:%s' % path})
-        assert not out
+        assert out.strip() == '1'
         assert not err
         assert path.check(file=1)
         assert 'mycat' in path.read()



More information about the Pypy-commit mailing list