Created
July 28, 2018 14:58
-
-
Save jbcurtin/327dea365b49cda5eed4928a8b15abe6 to your computer and use it in GitHub Desktop.
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 codecs | |
import inspect | |
import logging | |
import os | |
import uuid | |
logger = logging.getLogger(__name__) | |
STACK_PRINT_DIR = '/tmp/stacks' | |
STACK_DEPTH = 50 | |
_STACK_PRINTS = [] | |
if not os.path.exists(STACK_PRINT_DIR): | |
os.makedirs(STACK_PRINT_DIR) | |
def print_stack(file_name=None): | |
file_name = file_name or str(uuid.uuid4()) | |
file_path = os.path.join(STACK_PRINT_DIR, file_name) | |
stack_trace = inspect.stack() | |
stack_trace.reverse() | |
with codecs.open(file_path, 'w', 'utf-8') as stream: | |
for stack in stack_trace[-STACK_DEPTH:]: | |
frame = stack[0] | |
file_name = stack[1] | |
code_line = stack[2] | |
identity = stack[3] | |
lexical_code_line = str(stack[4]) | |
other = stack[5] | |
stream.write(', '.join([file_name, str(code_line), lexical_code_line, identity, str(other)])) | |
stream.write('\n') | |
_STACK_PRINTS.append(file_path) | |
logger.info('Printed Stack Frame[%s]' % file_path) | |
def print_printed_stacks(): | |
for file_path in _STACK_PRINTS: | |
logger.info('Printed Stack Frame[%s]' % file_path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To list the stack-traces in order,