Skip to content

Instantly share code, notes, and snippets.

@lorenzoriano
Created December 19, 2012 19:03
Show Gist options
  • Save lorenzoriano/4339505 to your computer and use it in GitHub Desktop.
Save lorenzoriano/4339505 to your computer and use it in GitHub Desktop.
import inspect
import sys
import os
class DebugPrint(object):
def __init__(self, f):
self.f = f
def write(self, text):
frame = inspect.currentframe()
filename = frame.f_back.f_code.co_filename.rsplit(os.sep, 1)[-1]
lineno = frame.f_back.f_lineno
prefix = "[%s:%s] " % (filename, lineno)
if text == os.linesep:
self.f.write(text)
else:
self.f.write(prefix + text)
if not isinstance(sys.stdout, DebugPrint):
sys.stdout = DebugPrint(sys.stdout)
#to use:
#>>> import debugprint
#>>> print "Hello"
#[<stdin>:1] Hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment