Last active
August 17, 2018 16:32
-
-
Save ColdFire87/501a63eab0b79f70928d1043e9181c84 to your computer and use it in GitHub Desktop.
Hook STDOUT writes (this includes the print function) and print a stacktrace to see where the call originated.
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
import inspect | |
import sys | |
def hook_stdout(): | |
""" | |
Hook STDOUT writes & print stacktrace. | |
Useful for figuring out where a printed statement comes from. | |
:return: | |
""" | |
def add_stacktrace(func): | |
def decorated(*args): | |
# Get info about stack frames | |
curframe = inspect.currentframe() | |
calframe = inspect.getouterframes(curframe) | |
# Create a stacktrace (do not include `decorated` function) | |
stacktrace = list(map(lambda c: '{} - {} - {}'.format(c.filename, c.lineno, c.function), calframe))[1:] | |
stacktrace = 'Stacktrace\n{}\n{}\n\n'.format('_' * 100, '\n'.join(stacktrace)) | |
# Indicate original input | |
original_input = ' '.join([*args]) | |
original_input = 'Original Input -> {}{}'.format(original_input, '\n' * 4) | |
# Decorate original input with stacktrace | |
decorated_input = '{}{}'.format(stacktrace, original_input) | |
# Run original function with decorated input | |
return func(decorated_input) | |
return decorated | |
sys.stdout.write = add_stacktrace(sys.stdout.write) | |
if __name__ == '__main__': | |
hook_stdout() | |
def func_a(): | |
def func_b(): | |
def func_c(): | |
# NOTE: `print` calls sys.stdout.write twice: | |
# - once for text content | |
# - once for newline character (`\n`) | |
print('Hello World!') | |
func_c() | |
func_b() | |
func_a() | |
""" | |
Example output: | |
Stacktrace | |
____________________________________________________________________________________________________ | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 45 - func_c | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 47 - func_b | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 49 - func_a | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 52 - <module> | |
Original Input -> Hello World! | |
Stacktrace | |
____________________________________________________________________________________________________ | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 45 - func_c | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 47 - func_b | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 49 - func_a | |
C:/Users/Stefan/Desktop/Python/STDOUT/hook_stdout.py - 52 - <module> | |
Original Input -> | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment