language: add basic logging initializer for experiments

This commit is contained in:
Sebastien Bourdeauducq 2015-10-17 10:39:21 +08:00
parent 04a049b93d
commit 797bf9830c
3 changed files with 33 additions and 9 deletions

View File

@ -1,10 +1,11 @@
# Copyright (C) 2014, 2015 Robert Jordens <jordens@gmail.com> # Copyright (C) 2014, 2015 Robert Jordens <jordens@gmail.com>
from artiq.language import core, environment, units, scan from artiq.language import core, environment, units, scan, logging
from artiq.language.core import * from artiq.language.core import *
from artiq.language.environment import * from artiq.language.environment import *
from artiq.language.units import * from artiq.language.units import *
from artiq.language.scan import * from artiq.language.scan import *
from artiq.language.logging import *
__all__ = [] __all__ = []
@ -12,3 +13,4 @@ __all__.extend(core.__all__)
__all__.extend(environment.__all__) __all__.extend(environment.__all__)
__all__.extend(units.__all__) __all__.extend(units.__all__)
__all__.extend(scan.__all__) __all__.extend(scan.__all__)
__all__.extend(logging.__all__)

23
artiq/language/logging.py Normal file
View File

@ -0,0 +1,23 @@
from artiq.language.environment import *
import logging
__all__ = ["LogExperiment"]
class LogExperiment:
def init_logger(self):
"""Call this from build() to add a logging level enumeration
widget, initialize logging globally, and create a logger.
Your class must also derive from ``HasEnvironment`` (or
``EnvExperiment``).
The created logger is called ``self.logger``."""
level = self.get_argument("log_level", EnumerationValue(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]))
if level is not None:
logging.basicConfig(level=getattr(logging, level))
self.logger = logging.getLogger(self.__class__.__name__)

View File

@ -1,5 +1,3 @@
import logging
from artiq import * from artiq import *
@ -35,8 +33,10 @@ class SubComponent2(HasEnvironment):
print(self.sc2_enum) print(self.sc2_enum)
class ArgumentsDemo(EnvExperiment): class ArgumentsDemo(EnvExperiment, LogExperiment):
def build(self): def build(self):
self.init_logger()
self.setattr_argument("free_value", FreeValue(None)) self.setattr_argument("free_value", FreeValue(None))
self.setattr_argument("number", NumberValue(42e-6, self.setattr_argument("number", NumberValue(42e-6,
unit="s", scale=1e-6, unit="s", scale=1e-6,
@ -53,11 +53,10 @@ class ArgumentsDemo(EnvExperiment):
self.sc2 = SubComponent2(parent=self) self.sc2 = SubComponent2(parent=self)
def run(self): def run(self):
logging.basicConfig(level=logging.DEBUG) self.logger.error("logging test: error")
logging.error("logging test: error") self.logger.warning("logging test: warning")
logging.warning("logging test: warning") self.logger.info("logging test: info")
logging.info("logging test: info") self.logger.debug("logging test: debug")
logging.debug("logging test: debug")
print(self.free_value) print(self.free_value)
print(self.boolean) print(self.boolean)