Created
January 20, 2014 20:49
-
-
Save cbeach/8528841 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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