forked from M-Labs/artiq
browser: refactor log
This commit is contained in:
parent
b3e55b753b
commit
26e98a13d0
34
artiq/browser/log.py
Normal file
34
artiq/browser/log.py
Normal file
@ -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)
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user