27 Feb
2015
27 Feb
'15
3:51 p.m.
# If we're on Win32 or MacOS, we don't use the console; instead,
# we create 'debug.log', redirect all output there, and set the
# console logging handler level to DEBUG. To test this on Linux,
# set CHIRP_DEBUG_LOG in the environment.
console_stream = None
console_format = '%(levelname)s: %(message)s'
if hasattr(sys, "frozen") or not os.isatty(0) \
or os.getenv("CHIRP_DEBUG_LOG"):
p = platform.get_platform()
log = file(p.config_file("debug.log"), "w", 0)
sys.stdout = log
sys.stderr = log
console_stream = log
console_format = self.log_format
self.early_level = logging.DEBUG
self.console = logging.StreamHandler(console_stream)
self.console.setLevel(self.early_level)
self.console.setFormatter(logging.Formatter(console_format))
I think there is a better way to do this:
class LogWriter(object): def __init__(self): self._log = logging.getLogger('console') self._log.setLevel(logging.DEBUG)
def write(self, message): self._log.debug(message)
sys.stdout = LogWriter() sys.stderr = sys.stdout
With that, if I do a "print", it gets logged properly, as module "console" at debug level. If we only do that when the console logger is actually logging to the debug.log file, then things are simpler and the log is consistently formatted. Obviously continuing with converting the print statements to log statements still helps so we get the actual modules correct in the log statements.
Thoughts?
--Dan