move logging setup from example_driver into cli frontend

This commit is contained in:
Joe Britton 2015-02-02 19:40:00 -07:00
parent c699f5e704
commit 786fb5f449
3 changed files with 21 additions and 48 deletions

View File

@ -4,6 +4,10 @@ import struct
import numpy.random
from artiq.language.units import dB, check_unit, Quantity
logger = logging.getLogger(__name__)
class ExampleARTIQDevice:
"""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))
# default logging level is 30, controller or client can change this subsequently
self.__setup_logging(logging_level=30)
# setup serial interface to device
self.__setup_serial(serial_port)
@ -39,35 +40,6 @@ class ExampleARTIQDevice:
* close serial port
* 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):
"""Do whatever is needed to communicate with the parameter database.
@ -92,7 +64,7 @@ class ExampleARTIQDevice:
:return str: return s
"""
replys = "echo: {}".format(s)
self.logger.info(replys)
logger.info(replys)
return s
def sphynx_documentation_example(self, arg1, arg2, arg3=True):
@ -120,10 +92,10 @@ class ExampleARTIQDevice:
:return None:
"""
self.logger.info("logs a message with level INFO")
self.logger.warning("logs a message with level WARNING")
self.logger.error("logs a message with level ERROR")
self.logger.log("logs a message with level CRITICAL")
logger.info("logs a message with level INFO")
logger.warning("logs a message with level WARNING")
logger.error("logs a message with level ERROR")
logger.log("logs a message with level CRITICAL")
def example_using_quantity_class(self, qvar1, qvar2):
"""Example of how to properly use write a function that takes arguments of the Quantity class.
@ -149,20 +121,20 @@ class ExampleARTIQDevice:
try:
my_random_num = numpy.random.rand(1)[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:
# alert GUI that this has happened
# raise an ARTIQ specific exception here
self.logger.error("you're unlucky")
logger.error("you're unlucky")
else:
r = myvar/0
except ZeroDivisionError:
# 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
self.logger.error("divide by zero")
logger.error("divide by zero")
raise
except:
self.logger.error("unhandled exception")
logger.error("unhandled exception")
raise
# TODO: Is this right? I don't know how artiq handles exceptions.

View File

@ -5,6 +5,9 @@ import argparse
from artiq.protocols.pc_rpc import simple_server_loop
import importlib
import logging
from artiq.tools import verbosity_args, init_logger
import artiq.devices.ExampleARTIQDevice
importlib.reload(artiq.devices.ExampleARTIQDevice)
@ -27,14 +30,11 @@ def get_argparser():
default="/dev/ttyUSB0", type=str,
help="serial port: on Windows an integer (e.g. 1),"
"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
parser.add_argument("--myvar", type=int, default=0,
help="example user-defined parameter")
verbosity_args(parser)
return parser
def main():
@ -46,12 +46,13 @@ def main():
# get command line arguments using the standard python argparser library
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
simple_server_loop(
{"example_artiq_device":
artiq.devices.example_artiq_device.ExampleARTIQDevice(
logging_level=args.verbosity,
simulate_hw=args.simulate_hw,
serial_port=args.port)},
host=args.bind,

View File

@ -36,5 +36,5 @@ def verbosity_args(parser):
group.add_argument("-q", "--quiet", default=0, action="count")
def init_logger(args):
logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10)
def init_logger(args, **kwargs):
logging.basicConfig(level=logging.WARNING + args.quiet*10 - args.verbose*10, **kwargs)