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>"""
def configure_logger():
# Configure the root logger to our liking
logger = logging.getLogger()
handler = util.OverviewerHandler(sys.stdout)
"Configures the root logger to our liking"
outstream = sys.stderr
if platform.system() == 'Windows':
# Our custom OverviewerHandler knows how to deal with select
# ANSI color escape sequences
# Our custom output stream processor knows how to deal with select ANSI
# color escape sequences
outstream = util.WindowsOutputStream()
formatter = util.ANSIColorFormatter()
elif sys.stderr.isatty():
# terminal logging with ANSI color
formatter = util.ANSIColorFormatter()
else:
# Let's not assume anything. Just text.
formatter = util.DumbFormatter()
logger = logging.getLogger()
handler = logging.StreamHandler(outstream)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

View File

@@ -113,13 +113,16 @@ HIGHLIGHT = {
}
class OverviewerHandler(logging.Handler):
def __init__(self, stream=sys.stderr):
logging.Handler.__init__(self)
self.stream = stream
class WindowsOutputStream(object):
"""A file-like object that proxies sys.stderr and interprets simple ANSI
escape codes for color, translating them to the appropriate Windows calls.
"""
def __init__(self, stream=None):
assert platform.system() == 'Windows'
self.stream = stream or sys.stderr
# go go gadget ctypes
if platform.system() == 'Windows':
self.GetStdHandle = ctypes.windll.kernel32.GetStdHandle
self.SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
self.STD_OUTPUT_HANDLE = ctypes.c_int(0xFFFFFFF5)
@@ -144,13 +147,10 @@ class OverviewerHandler(logging.Handler):
self.SetConsoleTextAttribute(self.output_handle,
ctypes.c_int(self.currentForeground | self.currentBackground | self.currentBold))
def emit(self, record):
msg = str(self.format(record))
def write(self, s):
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):
c = msg_strm.read(1)
if c == '': break
@@ -214,8 +214,6 @@ class OverviewerHandler(logging.Handler):
else:
self.stream.write(c)
else:
self.stream.write(msg)
self.stream.write("\n")
@@ -284,7 +282,7 @@ class DumbFormatter(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):