summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2011-02-03 19:27:25 +0000
committerLars Wirzenius <liw@liw.fi>2011-02-03 19:27:25 +0000
commit629ee8e18094101301b263a345e2b418cfda03df (patch)
tree3d34511d969eb1f27989d031849b7c7cb5f12372
downloadpython-tracing-629ee8e18094101301b263a345e2b418cfda03df.tar.gz
Initial commit.
-rw-r--r--example.py37
-rw-r--r--setup.py26
-rw-r--r--tracing.py68
3 files changed, 131 insertions, 0 deletions
diff --git a/example.py b/example.py
new file mode 100644
index 0000000..d7666d7
--- /dev/null
+++ b/example.py
@@ -0,0 +1,37 @@
+# Copyright (C) 2011 Lars Wirzenius <liw@liw.fi>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+import logging
+import tracing
+
+
+class Foo(object):
+
+ def foo(self):
+ self.bar()
+
+ def bar(self):
+ tracing.trace('this is output')
+
+f = Foo()
+
+logging.basicConfig(level=logging.DEBUG)
+tracing.trace_add_pattern('example')
+f.foo()
+tracing.trace_clear_patterns()
+f.foo()
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..be898e6
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,26 @@
+#!/usr/bin/python
+# Copyright (C) 2011 Lars Wirzenius <liw@liw.fi>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+from distutils.core import setup, Extension
+
+setup(name='tracing',
+ version='0.0',
+ description='debug log/trace messages',
+ author='Lars Wirzenius',
+ author_email='liw@liw.fi',
+ py_modules=['tracing'],
+ )
diff --git a/tracing.py b/tracing.py
new file mode 100644
index 0000000..f80ec54
--- /dev/null
+++ b/tracing.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2011 Lars Wirzenius <liw@liw.fi>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+
+'''Python debubbing log messages.
+
+This module provides a couple of functions for logging debug messages.
+It is sometimes practical to add a lot of debugging log messages to a
+program, but having them enabled all the time results in very large
+log files. Also, logging that much takes quite a bit of time.
+
+This module provides a way to turn such debugging or tracing messages
+on and off, based on the filename they occur in. For example:
+
+ import tracing
+
+ tracing.trace_add_pattern('foobar')
+ tracing.trace_add_pattern('yeehaa')
+
+ ...
+
+ tracing.trace('start procedure')
+ tracing.trace('arg1=%s' % arg1)
+ tracing.trace('arg2=%s' % arg2)
+
+Only calls that happen in files whose names contain 'foobar' or
+'yeehaa' will actually be logged. Pattern matching is based on
+substring checking only, no globbing or regexps, sorry.
+
+'''
+
+
+import logging
+import traceback
+
+
+trace_patterns = []
+
+
+def trace_add_pattern(pattern):
+ trace_patterns.append(pattern)
+
+
+def trace_clear_patterns():
+ del trace_patterns[:]
+
+
+def trace(msg):
+ frames = traceback.extract_stack(limit=2)
+ filename, lineno, funcname, text = frames[0]
+ for pattern in trace_patterns:
+ if pattern in filename:
+ logging.debug('%s:%s:%s: %s' % (filename, lineno, funcname, msg))
+ break
+