Add stdout and stderr to the logs

This commit is contained in:
Marco Pesenti Gritti 2006-10-25 15:24:40 +02:00
parent ebb896cb5b
commit 96b150d2bb

View File

@ -25,6 +25,9 @@ from sugar import env
_log_writer = None _log_writer = None
STDOUT_LEVEL = 1000
STDERR_LEVEL = 2000
class LogWriter: class LogWriter:
def __init__(self, module_id): def __init__(self, module_id):
self._module_id = module_id self._module_id = module_id
@ -45,6 +48,10 @@ class LogWriter:
level_txt = 'DEBUG' level_txt = 'DEBUG'
elif level == logging.INFO: elif level == logging.INFO:
level_txt = 'INFO' level_txt = 'INFO'
elif level == STDERR_LEVEL:
level_txt = 'STDERR'
elif level == STDOUT_LEVEL:
level_txt = 'STDOUT'
fmt = "%s - %s\n" % (level_txt, msg) fmt = "%s - %s\n" % (level_txt, msg)
fmt = fmt.encode("utf8") fmt = fmt.encode("utf8")
@ -60,6 +67,16 @@ class Handler(logging.Handler):
def emit(self, record): def emit(self, record):
self._writer.write_record(record) self._writer.write_record(record)
class StdoutCatcher:
def write(self, txt):
_log_writer.write(STDOUT_LEVEL, txt)
sys.__stdout__.write(txt)
class StderrCatcher:
def write(self, txt):
_log_writer.write(STDERR_LEVEL, txt)
sys.__stderr__.write(txt)
def __exception_handler(typ, exc, tb): def __exception_handler(typ, exc, tb):
trace = StringIO() trace = StringIO()
traceback.print_exception(typ, exc, tb, None, trace) traceback.print_exception(typ, exc, tb, None, trace)
@ -80,6 +97,9 @@ def start(module_id):
root_logger.setLevel(logging.DEBUG) root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(Handler(log_writer)) root_logger.addHandler(Handler(log_writer))
sys.stdout = StdoutCatcher()
sys.stderr = StderrCatcher()
global _log_writer global _log_writer
_log_writer = log_writer _log_writer = log_writer
sys.excepthook = __exception_handler sys.excepthook = __exception_handler