Skip to content

Instantly share code, notes, and snippets.

@ngbrown
Forked from wh13371/output.debug.string.py
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngbrown/d38064a844426a00fdaa to your computer and use it in GitHub Desktop.
Save ngbrown/d38064a844426a00fdaa to your computer and use it in GitHub Desktop.
#! /usr/bin/python
# logging to DbgView with OutputDebugString
# from https://gist.github.com/ngbrown/d38064a844426a00fdaa and https://gist.github.com/wh13371/92df4715fc17eb74299d
import logging
import ctypes
# output "logging" messages to DbgView via OutputDebugString (Windows only!)
OutputDebugStringW = ctypes.windll.kernel32.OutputDebugStringW
OutputDebugStringW.argtypes = [ctypes.c_wchar_p]
class DbgViewHandler(logging.Handler):
def emit(self, record):
OutputDebugStringW(self.format(record))
def config_logging():
# format
fmt = logging.Formatter(fmt='%(levelname)-8s %(name)s:%(lineno)d %(funcName)s() - %(message)s', datefmt='%Y:%m:%d %H:%M:%S')
rootLogger = logging.getLogger('')
rootLogger.setLevel(logging.NOTSET)
# "OutputDebugString\DebugView"
ods = DbgViewHandler()
ods.setLevel(logging.NOTSET)
ods.setFormatter(fmt)
rootLogger.addHandler(ods)
# "Console"
con = logging.StreamHandler()
con.setLevel(logging.DEBUG)
con.setFormatter(fmt)
rootLogger.addHandler(con)
# in each file you want to use, do this:
import logging
log = logging.getLogger(__name__)
#log.addHandler(logging.NullHandler()) # include if you don't want a "No handlers could be found" message when config_logging() isn't called
def main():
log.debug("debug message")
log.info("info message")
log.warn("warn message")
log.error("error message")
log.critical("critical message")
if __name__ == '__main__':
config_logging()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment