From 26e98a13d080ab5eaacc5520aa296d00a213d214 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Wed, 25 May 2016 15:14:04 +0200 Subject: [PATCH] browser: refactor log --- artiq/browser/log.py | 34 +++++++++++++++++++++++++++++++ artiq/frontend/artiq_browser.py | 36 ++------------------------------- 2 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 artiq/browser/log.py diff --git a/artiq/browser/log.py b/artiq/browser/log.py new file mode 100644 index 000000000..f660acf58 --- /dev/null +++ b/artiq/browser/log.py @@ -0,0 +1,34 @@ +import logging + +from artiq.protocols.logging import SourceFilter + + +class LogBufferHandler(logging.Handler): + def __init__(self, log, *args, **kwargs): + logging.Handler.__init__(self, *args, **kwargs) + self.log = log + self.setFormatter(logging.Formatter("%(name)s:%(message)s")) + + def emit(self, record): + if self.log.model is not None: + self.log.model.append((record.levelno, record.source, + record.created, self.format(record))) + + +def init_log(args, log): + root_logger = logging.getLogger() + root_logger.setLevel(logging.NOTSET) # we use our custom filter only + flt = SourceFilter(logging.WARNING + args.quiet*10 - args.verbose*10, + "browser") + handlers = [] + console_handler = logging.StreamHandler() + console_handler.setFormatter(logging.Formatter( + "%(levelname)s:%(source)s:%(name)s:%(message)s")) + handlers.append(console_handler) + + buffer_handler = LogBufferHandler(log) + handlers.append(buffer_handler) + + for handler in handlers: + handler.addFilter(flt) + root_logger.addHandler(handler) diff --git a/artiq/frontend/artiq_browser.py b/artiq/frontend/artiq_browser.py index 6678a9db8..01fb731da 100755 --- a/artiq/frontend/artiq_browser.py +++ b/artiq/frontend/artiq_browser.py @@ -12,8 +12,7 @@ from quamash import QEventLoop from artiq import __artiq_dir__ as artiq_dir from artiq.tools import verbosity_args, atexit_register_coroutine from artiq.gui import state, applets, models, log -from artiq.browser import datasets, files, experiments -from artiq.protocols.logging import SourceFilter +from artiq.browser import datasets, files, experiments, log as browser_log logger = logging.getLogger(__name__) @@ -74,7 +73,7 @@ def main(): atexit.register(loop.close) log_sub = models.LocalModelManager(log.Model) - init_log(args, log_sub) + browser_log.init_log(args, log_sub) log_sub.init([]) smgr = state.StateManager(args.db_file) @@ -140,36 +139,5 @@ def main(): loop.run_until_complete(main_window.exit_request.wait()) -class LogBufferHandler(logging.Handler): - def __init__(self, log, *args, **kwargs): - logging.Handler.__init__(self, *args, **kwargs) - self.log = log - self.setFormatter(logging.Formatter("%(name)s:%(message)s")) - - def emit(self, record): - if self.log.model is not None: - self.log.model.append((record.levelno, record.source, - record.created, self.format(record))) - - -def init_log(args, log): - root_logger = logging.getLogger() - root_logger.setLevel(logging.NOTSET) # we use our custom filter only - flt = SourceFilter(logging.WARNING + args.quiet*10 - args.verbose*10, - "browser") - handlers = [] - console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter( - "%(levelname)s:%(source)s:%(name)s:%(message)s")) - handlers.append(console_handler) - - buffer_handler = LogBufferHandler(log) - handlers.append(buffer_handler) - - for handler in handlers: - handler.addFilter(flt) - root_logger.addHandler(handler) - - if __name__ == "__main__": main()