From 5ba7218cc1d33e11a8a134efd74d921974692838 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Wed, 28 Jan 2015 14:44:15 +0100 Subject: [PATCH] Factorize argparser for debug level --- artiq/frontend/artiq_master.py | 3 +++ artiq/frontend/lda_controller.py | 3 +++ artiq/frontend/pdq2_client.py | 3 +++ artiq/frontend/pdq2_controller.py | 8 +++----- artiq/tools.py | 11 +++++++++++ 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/artiq/frontend/artiq_master.py b/artiq/frontend/artiq_master.py index f6cf333e1..638f034bb 100755 --- a/artiq/frontend/artiq_master.py +++ b/artiq/frontend/artiq_master.py @@ -10,6 +10,7 @@ from artiq.protocols.file_db import FlatFileDB, SimpleHistory from artiq.master.scheduler import Scheduler from artiq.master.rt_results import RTResults from artiq.master.repository import Repository +from artiq.tools import verbosity_args, init_logger def get_argparser(): @@ -23,12 +24,14 @@ def get_argparser(): parser.add_argument( "--port-control", default=3251, type=int, help="TCP port to listen to for control") + verbosity_args(parser) return parser def main(): args = get_argparser().parse_args() + init_logger(args) ddb = FlatFileDB("ddb.pyon") pdb = FlatFileDB("pdb.pyon") simplephist = SimpleHistory(30) diff --git a/artiq/frontend/lda_controller.py b/artiq/frontend/lda_controller.py index a19c8c308..65ebfaf1a 100755 --- a/artiq/frontend/lda_controller.py +++ b/artiq/frontend/lda_controller.py @@ -4,6 +4,7 @@ import argparse from artiq.devices.lda.driver import Lda, Ldasim from artiq.protocols.pc_rpc import simple_server_loop +from artiq.tools import verbosity_args, init_logger def get_argparser(): @@ -16,11 +17,13 @@ def get_argparser(): help="TCP port to listen to") parser.add_argument('-s', '--serial', default=None, help="USB serial number of the device") + verbosity_args(parser) return parser def main(): args = get_argparser().parse_args() + init_logger(args) if args.device == "sim": lda = Ldasim() else: diff --git a/artiq/frontend/pdq2_client.py b/artiq/frontend/pdq2_client.py index d5e587a4f..598f7f461 100755 --- a/artiq/frontend/pdq2_client.py +++ b/artiq/frontend/pdq2_client.py @@ -9,6 +9,7 @@ from scipy import interpolate import numpy as np from artiq.protocols.pc_rpc import Client +from artiq.tools import verbosity_args, init_logger def get_argparser(): @@ -49,11 +50,13 @@ def get_argparser(): action="store_true", help="do reset before") parser.add_argument("-b", "--bit", default=False, action="store_true", help="do bit test") + verbosity_args(parser) return parser def main(): args = get_argparser().parse_args() + init_logger(args) dev = Client(args.server, args.port, "pdq2") dev.init() diff --git a/artiq/frontend/pdq2_controller.py b/artiq/frontend/pdq2_controller.py index ad28ac504..dd86de0ce 100755 --- a/artiq/frontend/pdq2_controller.py +++ b/artiq/frontend/pdq2_controller.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 import argparse -import logging from artiq.devices.pdq2.driver import Pdq2 from artiq.protocols.pc_rpc import simple_server_loop +from artiq.tools import verbosity_args, init_logger def get_argparser(): @@ -19,16 +19,14 @@ def get_argparser(): parser.add_argument( "-d", "--debug", default=False, action="store_true", help="debug communications") + verbosity_args(parser) return parser def main(): args = get_argparser().parse_args() - if args.debug: - logging.basicConfig(level=logging.DEBUG) - else: - logging.basicConfig(level=logging.WARNING) + init_logger(args) dev = Pdq2(serial=args.serial) try: diff --git a/artiq/tools.py b/artiq/tools.py index ea8276ee1..5505f20b6 100644 --- a/artiq/tools.py +++ b/artiq/tools.py @@ -1,6 +1,7 @@ from operator import itemgetter import importlib.machinery import linecache +import logging def format_run_arguments(arguments): @@ -27,3 +28,13 @@ def file_import(filename): loader = importlib.machinery.SourceFileLoader(modname, filename) return loader.load_module() + + +def verbosity_args(parser): + group = parser.add_argument_group("verbosity") + group.add_argument("-v", "--verbose", default=0, action="count") + group.add_argument("-q", "--quiet", default=0, action="count") + + +def init_logger(args): + logging.basicConfig(logging.INFO + args.quiet*10 - args.verbose*10)