Skip to content

Instantly share code, notes, and snippets.

@jbcurtin
Created July 28, 2018 14:58
Show Gist options
  • Save jbcurtin/327dea365b49cda5eed4928a8b15abe6 to your computer and use it in GitHub Desktop.
Save jbcurtin/327dea365b49cda5eed4928a8b15abe6 to your computer and use it in GitHub Desktop.
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)
@jbcurtin
Copy link
Author

To list the stack-traces in order,

cd /tmp/stacks
ls -ltra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment