0

changed windows logging to be a special output stream

This commit is contained in:
Andrew Brown
2011-11-01 00:03:32 -04:00
parent 6a67841b44
commit 470440563c
2 changed files with 87 additions and 84 deletions

View File

@@ -107,20 +107,25 @@ helptext = """
%prog [OPTIONS] <World # / Name / Path to World> <tiles dest dir>""" %prog [OPTIONS] <World # / Name / Path to World> <tiles dest dir>"""
def configure_logger(): def configure_logger():
# Configure the root logger to our liking "Configures the root logger to our liking"
logger = logging.getLogger()
handler = util.OverviewerHandler(sys.stdout) outstream = sys.stderr
if platform.system() == 'Windows': if platform.system() == 'Windows':
# Our custom OverviewerHandler knows how to deal with select # Our custom output stream processor knows how to deal with select ANSI
# ANSI color escape sequences # color escape sequences
outstream = util.WindowsOutputStream()
formatter = util.ANSIColorFormatter() formatter = util.ANSIColorFormatter()
elif sys.stderr.isatty(): elif sys.stderr.isatty():
# terminal logging with ANSI color # terminal logging with ANSI color
formatter = util.ANSIColorFormatter() formatter = util.ANSIColorFormatter()
else: else:
# Let's not assume anything. Just text. # Let's not assume anything. Just text.
formatter = util.DumbFormatter() formatter = util.DumbFormatter()
logger = logging.getLogger()
handler = logging.StreamHandler(outstream)
handler.setFormatter(formatter) handler.setFormatter(formatter)
logger.addHandler(handler) logger.addHandler(handler)
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)

View File

@@ -113,13 +113,16 @@ HIGHLIGHT = {
} }
class OverviewerHandler(logging.Handler): class WindowsOutputStream(object):
def __init__(self, stream=sys.stderr): """A file-like object that proxies sys.stderr and interprets simple ANSI
logging.Handler.__init__(self) escape codes for color, translating them to the appropriate Windows calls.
self.stream = stream
"""
def __init__(self, stream=None):
assert platform.system() == 'Windows'
self.stream = stream or sys.stderr
# go go gadget ctypes # go go gadget ctypes
if platform.system() == 'Windows':
self.GetStdHandle = ctypes.windll.kernel32.GetStdHandle self.GetStdHandle = ctypes.windll.kernel32.GetStdHandle
self.SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute self.SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
self.STD_OUTPUT_HANDLE = ctypes.c_int(0xFFFFFFF5) self.STD_OUTPUT_HANDLE = ctypes.c_int(0xFFFFFFF5)
@@ -144,13 +147,10 @@ class OverviewerHandler(logging.Handler):
self.SetConsoleTextAttribute(self.output_handle, self.SetConsoleTextAttribute(self.output_handle,
ctypes.c_int(self.currentForeground | self.currentBackground | self.currentBold)) ctypes.c_int(self.currentForeground | self.currentBackground | self.currentBold))
def emit(self, record): def write(self, s):
msg = str(self.format(record))
msg_strm = StringIO(msg) msg_strm = StringIO(s)
# only on Windows do we do this fancy ANSI parsing magic
if platform.system() == 'Windows':
while (True): while (True):
c = msg_strm.read(1) c = msg_strm.read(1)
if c == '': break if c == '': break
@@ -214,8 +214,6 @@ class OverviewerHandler(logging.Handler):
else: else:
self.stream.write(c) self.stream.write(c)
else:
self.stream.write(msg)
self.stream.write("\n") self.stream.write("\n")
@@ -284,7 +282,7 @@ class DumbFormatter(HighlightingFormatter):
class ANSIColorFormatter(HighlightingFormatter): class ANSIColorFormatter(HighlightingFormatter):
"""Highlights and colorizes log entries with ANSI escape sequences """Uses ANSI escape sequences to enable GLORIOUS EXTRA-COLOR!
""" """
def highlight(self, record): def highlight(self, record):