mirror of https://github.com/m-labs/artiq.git
move logging setup from example_driver into cli frontend
This commit is contained in:
parent
c699f5e704
commit
786fb5f449
|
@ -4,6 +4,10 @@ import struct
|
||||||
import numpy.random
|
import numpy.random
|
||||||
from artiq.language.units import dB, check_unit, Quantity
|
from artiq.language.units import dB, check_unit, Quantity
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ExampleARTIQDevice:
|
class ExampleARTIQDevice:
|
||||||
"""Example code demonstrating how to write an device controller for ARTIQ.
|
"""Example code demonstrating how to write an device controller for ARTIQ.
|
||||||
|
|
||||||
|
@ -25,9 +29,6 @@ class ExampleARTIQDevice:
|
||||||
"""
|
"""
|
||||||
# TODO handle problem that /dev/ttyUSBx is not unique ((2g))
|
# TODO handle problem that /dev/ttyUSBx is not unique ((2g))
|
||||||
|
|
||||||
# default logging level is 30, controller or client can change this subsequently
|
|
||||||
self.__setup_logging(logging_level=30)
|
|
||||||
|
|
||||||
# setup serial interface to device
|
# setup serial interface to device
|
||||||
self.__setup_serial(serial_port)
|
self.__setup_serial(serial_port)
|
||||||
|
|
||||||
|
@ -39,35 +40,6 @@ class ExampleARTIQDevice:
|
||||||
* close serial port
|
* close serial port
|
||||||
* close any open files
|
* close any open files
|
||||||
"""
|
"""
|
||||||
def __setup_logging(self, logging_level):
|
|
||||||
"""Do whatever is needed to configure ARTIQ logging.
|
|
||||||
|
|
||||||
For more on the python logging tool see https://docs.python.org/2/library/logging.html
|
|
||||||
|
|
||||||
:param int logging_level: to what degree are messages generated by this driver reported to the ARTIQ ecosystem?
|
|
||||||
50 is CRITICAL, 40 is ERROR, 30 is WARNING (default), 20 is INFO, 10 is DEBUG
|
|
||||||
:return None:
|
|
||||||
"""
|
|
||||||
logging_format_string = "%(asctime)-15s %(message)s"
|
|
||||||
logging.basicConfig(format=logging_format_string)
|
|
||||||
self.logger = logging.getLogger(self.__class__.__name__)
|
|
||||||
self.logger.setLevel(logging_level)
|
|
||||||
|
|
||||||
# TODO: Should there be a default class from which devices inherit? Such a class could include
|
|
||||||
# proper setup of things like the logger. This would also make it easier to add additional functionality
|
|
||||||
# across the driver ecosystem without modifying the code of each driver individually.
|
|
||||||
|
|
||||||
# TODO: how is the logger connected to the client and/or GUI? Either or both might need to be notified
|
|
||||||
# of errors. Nobody commented on this...
|
|
||||||
|
|
||||||
def set_logging_level(self, logging_level):
|
|
||||||
"""Change logging level.
|
|
||||||
|
|
||||||
:param int logging_level: to what degree are messages generated by this driver reported to the ARTIQ ecosystem?
|
|
||||||
50 is CRITICAL, 40 is ERROR, 30 is WARNING (default), 20 is INFO, 10 is DEBUG
|
|
||||||
:return None:
|
|
||||||
"""
|
|
||||||
self.logger.setLevel(logging_level)
|
|
||||||
|
|
||||||
def __setup_connection_to_parameter_database(self):
|
def __setup_connection_to_parameter_database(self):
|
||||||
"""Do whatever is needed to communicate with the parameter database.
|
"""Do whatever is needed to communicate with the parameter database.
|
||||||
|
@ -92,7 +64,7 @@ class ExampleARTIQDevice:
|
||||||
:return str: return s
|
:return str: return s
|
||||||
"""
|
"""
|
||||||
replys = "echo: {}".format(s)
|
replys = "echo: {}".format(s)
|
||||||
self.logger.info(replys)
|
logger.info(replys)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def sphynx_documentation_example(self, arg1, arg2, arg3=True):
|
def sphynx_documentation_example(self, arg1, arg2, arg3=True):
|
||||||
|
@ -120,10 +92,10 @@ class ExampleARTIQDevice:
|
||||||
|
|
||||||
:return None:
|
:return None:
|
||||||
"""
|
"""
|
||||||
self.logger.info("logs a message with level INFO")
|
logger.info("logs a message with level INFO")
|
||||||
self.logger.warning("logs a message with level WARNING")
|
logger.warning("logs a message with level WARNING")
|
||||||
self.logger.error("logs a message with level ERROR")
|
logger.error("logs a message with level ERROR")
|
||||||
self.logger.log("logs a message with level CRITICAL")
|
logger.log("logs a message with level CRITICAL")
|
||||||
|
|
||||||
def example_using_quantity_class(self, qvar1, qvar2):
|
def example_using_quantity_class(self, qvar1, qvar2):
|
||||||
"""Example of how to properly use write a function that takes arguments of the Quantity class.
|
"""Example of how to properly use write a function that takes arguments of the Quantity class.
|
||||||
|
@ -149,20 +121,20 @@ class ExampleARTIQDevice:
|
||||||
try:
|
try:
|
||||||
my_random_num = numpy.random.rand(1)[0]
|
my_random_num = numpy.random.rand(1)[0]
|
||||||
if myvar < 0:
|
if myvar < 0:
|
||||||
self.logger.error("argument must be greater than zero")
|
logger.error("argument must be greater than zero")
|
||||||
elif my_random_num > myvar:
|
elif my_random_num > myvar:
|
||||||
# alert GUI that this has happened
|
# alert GUI that this has happened
|
||||||
# raise an ARTIQ specific exception here
|
# raise an ARTIQ specific exception here
|
||||||
self.logger.error("you're unlucky")
|
logger.error("you're unlucky")
|
||||||
else:
|
else:
|
||||||
r = myvar/0
|
r = myvar/0
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
# caught a divide by zero error; if it can be handled locally do that
|
# caught a divide by zero error; if it can be handled locally do that
|
||||||
# if it can't be handeled locally throw it for another
|
# if it can't be handeled locally throw it for another
|
||||||
self.logger.error("divide by zero")
|
logger.error("divide by zero")
|
||||||
raise
|
raise
|
||||||
except:
|
except:
|
||||||
self.logger.error("unhandled exception")
|
logger.error("unhandled exception")
|
||||||
raise
|
raise
|
||||||
# TODO: Is this right? I don't know how artiq handles exceptions.
|
# TODO: Is this right? I don't know how artiq handles exceptions.
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,9 @@ import argparse
|
||||||
from artiq.protocols.pc_rpc import simple_server_loop
|
from artiq.protocols.pc_rpc import simple_server_loop
|
||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from artiq.tools import verbosity_args, init_logger
|
||||||
|
|
||||||
import artiq.devices.ExampleARTIQDevice
|
import artiq.devices.ExampleARTIQDevice
|
||||||
importlib.reload(artiq.devices.ExampleARTIQDevice)
|
importlib.reload(artiq.devices.ExampleARTIQDevice)
|
||||||
|
|
||||||
|
@ -27,14 +30,11 @@ def get_argparser():
|
||||||
default="/dev/ttyUSB0", type=str,
|
default="/dev/ttyUSB0", type=str,
|
||||||
help="serial port: on Windows an integer (e.g. 1),"
|
help="serial port: on Windows an integer (e.g. 1),"
|
||||||
"on Linux a device path (e.g. \"/dev/ttyUSB0\")")
|
"on Linux a device path (e.g. \"/dev/ttyUSB0\")")
|
||||||
parser.add_argument("--verbosity", type=int, default=1)
|
|
||||||
parser.add_argument("--log", type=int, default=30,
|
|
||||||
help="set log level by verbosity: 50 is CRITICAL, 40 is ERROR, 30 is WARNING, 20 is INFO, 10 is DEBUG")
|
|
||||||
|
|
||||||
# add additional commandline arguments here that might be needed to configure the device
|
# add additional commandline arguments here that might be needed to configure the device
|
||||||
parser.add_argument("--myvar", type=int, default=0,
|
parser.add_argument("--myvar", type=int, default=0,
|
||||||
help="example user-defined parameter")
|
help="example user-defined parameter")
|
||||||
|
verbosity_args(parser)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -46,12 +46,13 @@ def main():
|
||||||
|
|
||||||
# get command line arguments using the standard python argparser library
|
# get command line arguments using the standard python argparser library
|
||||||
args = get_argparser().parse_args()
|
args = get_argparser().parse_args()
|
||||||
|
log_fmt = "%(asctime)-15s [%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
|
||||||
|
init_logger(args, format=log_fmt)
|
||||||
|
|
||||||
# start event loop
|
# start event loop
|
||||||
simple_server_loop(
|
simple_server_loop(
|
||||||
{"example_artiq_device":
|
{"example_artiq_device":
|
||||||
artiq.devices.example_artiq_device.ExampleARTIQDevice(
|
artiq.devices.example_artiq_device.ExampleARTIQDevice(
|
||||||
logging_level=args.verbosity,
|
|
||||||
simulate_hw=args.simulate_hw,
|
simulate_hw=args.simulate_hw,
|
||||||
serial_port=args.port)},
|
serial_port=args.port)},
|
||||||
host=args.bind,
|
host=args.bind,
|
||||||
|
|
|
@ -36,5 +36,5 @@ def verbosity_args(parser):
|
||||||
group.add_argument("-q", "--quiet", default=0, action="count")
|
group.add_argument("-q", "--quiet", default=0, action="count")
|
||||||
|
|
||||||
|
|
||||||
def init_logger(args):
|
def init_logger(args, **kwargs):
|
||||||
logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10)
|
logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10, **kwargs)
|
||||||
|
|
Loading…
Reference in New Issue