Skip to content

Instantly share code, notes, and snippets.

@cbeach
Created January 20, 2014 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbeach/8528841 to your computer and use it in GitHub Desktop.
Save cbeach/8528841 to your computer and use it in GitHub Desktop.
def lineno():
"""Returns the current line number in our program."""
return inspect.currentframe().f_back.f_lineno
class ReRedirectSTDOutToFiledirectSTDOutToFile:
def __init__(self, file_name, mode='w'):
self.output_file = open(file_name, mode)
def __enter__(self):
self.old_stdout = sys.stdout
sys.stdout = self.output_file
def __exit__(self, *args, **kwargs):
sys.stdout = self.old_stdout
# The in the file that you're trying to debug/profile
import objgraph
obj.show_growth(limit=10)
with RedirectSTDOutToFile('/home/<home_dir>/some_file.log'):
print 'this line is in file {}:{}'.format(__name__, lineno())
obj.show_growth(limit=10)
#Which will save the output of show growth to ~/some_file.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment