diff --git a/sugar/logger.py b/sugar/logger.py index 7fe1223b..b4b2e0c5 100644 --- a/sugar/logger.py +++ b/sugar/logger.py @@ -25,6 +25,9 @@ from sugar import env _log_writer = None +STDOUT_LEVEL = 1000 +STDERR_LEVEL = 2000 + class LogWriter: def __init__(self, module_id): self._module_id = module_id @@ -45,6 +48,10 @@ class LogWriter: level_txt = 'DEBUG' elif level == logging.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 = fmt.encode("utf8") @@ -60,6 +67,16 @@ class Handler(logging.Handler): def emit(self, 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): trace = StringIO() traceback.print_exception(typ, exc, tb, None, trace) @@ -80,6 +97,9 @@ def start(module_id): root_logger.setLevel(logging.DEBUG) root_logger.addHandler(Handler(log_writer)) + sys.stdout = StdoutCatcher() + sys.stderr = StderrCatcher() + global _log_writer _log_writer = log_writer sys.excepthook = __exception_handler