Skip to content

Instantly share code, notes, and snippets.

@jlinoff
Last active March 9, 2021 14:47
Show Gist options
  • Save jlinoff/c5d2fd19b756167ab475 to your computer and use it in GitHub Desktop.
Save jlinoff/c5d2fd19b756167ab475 to your computer and use it in GitHub Desktop.
Simple debug function that uses introspection to display the file name, function name and line number of the caller.
def debug(msg):
'''
Display a debug message with the file name, function
name and line number.
'''
import inspect
parent_frame = inspect.currentframe().f_back
lineno = parent_frame.f_lineno
fctname = parent_frame.f_code.co_name
filename = parent_frame.f_code.co_filename
sys.stderr.write('DEBUG:{}:{}:{} {}\n'.format(filename, fctname, lineno, msg))
def fct1():
'''
Dummy test function.
'''
debug('a debug message')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment