From 797bf9830c6bc40ef74df30baedfdea0658826a6 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 17 Oct 2015 10:39:21 +0800 Subject: [PATCH] language: add basic logging initializer for experiments --- artiq/language/__init__.py | 4 +++- artiq/language/logging.py | 23 ++++++++++++++++++++ examples/master/repository/arguments_demo.py | 15 ++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 artiq/language/logging.py diff --git a/artiq/language/__init__.py b/artiq/language/__init__.py index 39edc6164..c7be4eb9b 100644 --- a/artiq/language/__init__.py +++ b/artiq/language/__init__.py @@ -1,10 +1,11 @@ # Copyright (C) 2014, 2015 Robert Jordens -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.environment import * from artiq.language.units import * from artiq.language.scan import * +from artiq.language.logging import * __all__ = [] @@ -12,3 +13,4 @@ __all__.extend(core.__all__) __all__.extend(environment.__all__) __all__.extend(units.__all__) __all__.extend(scan.__all__) +__all__.extend(logging.__all__) diff --git a/artiq/language/logging.py b/artiq/language/logging.py new file mode 100644 index 000000000..c64a3dbf6 --- /dev/null +++ b/artiq/language/logging.py @@ -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__) diff --git a/examples/master/repository/arguments_demo.py b/examples/master/repository/arguments_demo.py index ffb6e2ac2..a0114c211 100644 --- a/examples/master/repository/arguments_demo.py +++ b/examples/master/repository/arguments_demo.py @@ -1,5 +1,3 @@ -import logging - from artiq import * @@ -35,8 +33,10 @@ class SubComponent2(HasEnvironment): print(self.sc2_enum) -class ArgumentsDemo(EnvExperiment): +class ArgumentsDemo(EnvExperiment, LogExperiment): def build(self): + self.init_logger() + self.setattr_argument("free_value", FreeValue(None)) self.setattr_argument("number", NumberValue(42e-6, unit="s", scale=1e-6, @@ -53,11 +53,10 @@ class ArgumentsDemo(EnvExperiment): self.sc2 = SubComponent2(parent=self) def run(self): - logging.basicConfig(level=logging.DEBUG) - logging.error("logging test: error") - logging.warning("logging test: warning") - logging.info("logging test: info") - logging.debug("logging test: debug") + self.logger.error("logging test: error") + self.logger.warning("logging test: warning") + self.logger.info("logging test: info") + self.logger.debug("logging test: debug") print(self.free_value) print(self.boolean)