Last active
December 11, 2017 23:50
-
-
Save ilblackdragon/7749a630b01ba8b8d12999edb37fb2d1 to your computer and use it in GitHub Desktop.
Exception Hook for not print debugging
This file contains hidden or 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 sys | |
import traceback | |
def _format_value(key, value, first_n=20, last_n=20): | |
s = repr(value) | |
s = s.replace('\n', ' ').strip() | |
if len(s) > first_n + last_n + 3: | |
s = s[:first_n] + "..." + s[-last_n:] | |
return "%s: %s" % (key, s) | |
def _format_locals(values): | |
result = [] | |
for key, value in values.items(): | |
result.append(_format_value(key, value)) | |
return '\n'.join(result) | |
def excepthook(type, value, tb): | |
traceback.print_exception(type, value, tb) | |
while tb.tb_next: | |
tb = tb.tb_next | |
print(_format_locals(tb.tb_frame.f_locals)) | |
sys.excepthook = excepthook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment