diff options
author | Vadim Yanitskiy <axilirator@gmail.com> | 2018-12-07 09:34:00 +0700 |
---|---|---|
committer | Vadim Yanitskiy <axilirator@gmail.com> | 2018-12-08 02:10:20 +0700 |
commit | cdf349bc645d36645dab13e3f975b4deafbee579 (patch) | |
tree | d95353262111509d5fbbf8caecb32da42662a8bb /src/target/trx_toolkit | |
parent | 5cb7e287ed120f228de90cc05dec1b9d8092174e (diff) |
trx_toolkit: introduce logging configuration arguments
Before this change, it was impossible to configure logging
parameters from command line, such as log level and format.
This change introduces the following optional arguments:
--log-level - logging level for stderr (by default, DEBUG);
--log-format - logging message format for stderr;
--log-file-name - enable logging to a given file;
--log-file-level - logging level for file (by default, DEBUG);
--log-file-format - logging message format for file;
which are defined in a new class called ApplicationBase, so
all existing applications should inherit them now.
Change-Id: Ic3b0440cd73946ad444bd7e48feb7a92d45f6488
Diffstat (limited to 'src/target/trx_toolkit')
-rw-r--r-- | src/target/trx_toolkit/app_common.py | 67 | ||||
-rwxr-xr-x | src/target/trx_toolkit/burst_gen.py | 9 | ||||
-rwxr-xr-x | src/target/trx_toolkit/burst_send.py | 9 | ||||
-rwxr-xr-x | src/target/trx_toolkit/ctrl_cmd.py | 9 | ||||
-rwxr-xr-x | src/target/trx_toolkit/fake_trx.py | 9 | ||||
-rwxr-xr-x | src/target/trx_toolkit/trx_sniff.py | 9 |
6 files changed, 97 insertions, 15 deletions
diff --git a/src/target/trx_toolkit/app_common.py b/src/target/trx_toolkit/app_common.py new file mode 100644 index 00000000..180e97ca --- /dev/null +++ b/src/target/trx_toolkit/app_common.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# TRX Toolkit +# Common helpers for applications +# +# (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com> +# +# All Rights Reserved +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +import logging as log + +class ApplicationBase: + # Osmocom-style logging message format + # Example: [DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd + LOG_FMT_DEFAULT = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s" + + def app_init_logging(self, argv): + # Default logging handler (stderr) + sh = log.StreamHandler() + sh.setLevel(log.getLevelName(argv.log_level)) + sh.setFormatter(log.Formatter(argv.log_fmt)) + log.root.addHandler(sh) + + # Optional file handler + if argv.log_file_name is not None: + fh = log.FileHandler(argv.log_file_name) + fh.setLevel(log.getLevelName(argv.log_file_level)) + fh.setFormatter(log.Formatter(argv.log_file_fmt)) + log.root.addHandler(fh) + + # Set DEBUG for the root logger + log.root.setLevel(log.DEBUG) + + def app_reg_logging_options(self, parser): + parser.add_argument("--log-level", metavar = "LVL", + dest = "log_level", type = str, default = "DEBUG", + choices = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], + help = "Set logging level (default %(default)s)") + parser.add_argument("--log-format", metavar = "FMT", + dest = "log_fmt", type = str, default = self.LOG_FMT_DEFAULT, + help = "Set logging message format") + + parser.add_argument("--log-file-name", metavar = "FILE", + dest = "log_file_name", type = str, + help = "Set logging file name") + parser.add_argument("--log-file-level", metavar = "LVL", + dest = "log_file_level", type = str, default = "DEBUG", + choices = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], + help = "Set logging level for file (default %(default)s)") + parser.add_argument("--log-file-format", metavar = "FMT", + dest = "log_file_fmt", type = str, default = self.LOG_FMT_DEFAULT, + help = "Set logging message format for file") diff --git a/src/target/trx_toolkit/burst_gen.py b/src/target/trx_toolkit/burst_gen.py index 7625a273..1c18b3cc 100755 --- a/src/target/trx_toolkit/burst_gen.py +++ b/src/target/trx_toolkit/burst_gen.py @@ -31,13 +31,14 @@ import signal import argparse import sys +from app_common import ApplicationBase from rand_burst_gen import RandBurstGen from data_dump import DATADumpFile from data_if import DATAInterface from gsm_shared import * from data_msg import * -class Application: +class Application(ApplicationBase): def __init__(self): print_copyright(CR_HOLDERS) self.argv = self.parse_argv() @@ -46,8 +47,7 @@ class Application: signal.signal(signal.SIGINT, self.sig_handler) # Configure logging - log.basicConfig(level = log.DEBUG, - format = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s") + self.app_init_logging(self.argv) # Open requested capture file if self.argv.output_file is not None: @@ -135,6 +135,9 @@ class Application: parser = argparse.ArgumentParser(prog = "burst_gen", description = "Auxiliary tool to generate and send random bursts") + # Register common logging options + self.app_reg_logging_options(parser) + trx_group = parser.add_argument_group("TRX interface") trx_group.add_argument("-r", "--remote-addr", dest = "remote_addr", type = str, default = "127.0.0.1", diff --git a/src/target/trx_toolkit/burst_send.py b/src/target/trx_toolkit/burst_send.py index 499e929d..16db222f 100755 --- a/src/target/trx_toolkit/burst_send.py +++ b/src/target/trx_toolkit/burst_send.py @@ -30,12 +30,13 @@ import signal import argparse import sys +from app_common import ApplicationBase from data_dump import DATADumpFile from data_if import DATAInterface from gsm_shared import * from data_msg import * -class Application: +class Application(ApplicationBase): def __init__(self): print_copyright(CR_HOLDERS) self.argv = self.parse_argv() @@ -44,8 +45,7 @@ class Application: signal.signal(signal.SIGINT, self.sig_handler) # Configure logging - log.basicConfig(level = log.DEBUG, - format = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s") + self.app_init_logging(self.argv) # Open requested capture file self.ddf = DATADumpFile(self.argv.capture_file) @@ -106,6 +106,9 @@ class Application: parser = argparse.ArgumentParser(prog = "burst_send", description = "Auxiliary tool to send (reply) captured bursts") + # Register common logging options + self.app_reg_logging_options(parser) + trx_group = parser.add_argument_group("TRX interface") trx_group.add_argument("-r", "--remote-addr", dest = "remote_addr", type = str, default = "127.0.0.1", diff --git a/src/target/trx_toolkit/ctrl_cmd.py b/src/target/trx_toolkit/ctrl_cmd.py index 43b24a31..ffc3e467 100755 --- a/src/target/trx_toolkit/ctrl_cmd.py +++ b/src/target/trx_toolkit/ctrl_cmd.py @@ -32,9 +32,10 @@ import argparse import select import sys +from app_common import ApplicationBase from udp_link import UDPLink -class Application: +class Application(ApplicationBase): def __init__(self): print_copyright(CR_HOLDERS) self.argv = self.parse_argv() @@ -43,8 +44,7 @@ class Application: signal.signal(signal.SIGINT, self.sig_handler) # Configure logging - log.basicConfig(level = log.DEBUG, - format = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s") + self.app_init_logging(self.argv) # Init UDP connection self.ctrl_link = UDPLink( @@ -59,6 +59,9 @@ class Application: parser = argparse.ArgumentParser(prog = "ctrl_cmd", description = "Auxiliary tool to send control commands") + # Register common logging options + self.app_reg_logging_options(parser) + trx_group = parser.add_argument_group("TRX interface") trx_group.add_argument("-r", "--remote-addr", dest = "remote_addr", type = str, default = "127.0.0.1", diff --git a/src/target/trx_toolkit/fake_trx.py b/src/target/trx_toolkit/fake_trx.py index 1c991cee..a45ce203 100755 --- a/src/target/trx_toolkit/fake_trx.py +++ b/src/target/trx_toolkit/fake_trx.py @@ -31,6 +31,7 @@ import argparse import select import sys +from app_common import ApplicationBase from ctrl_if_bts import CTRLInterfaceBTS from ctrl_if_bb import CTRLInterfaceBB from burst_fwd import BurstForwarder @@ -39,7 +40,7 @@ from fake_pm import FakePM from udp_link import UDPLink from clck_gen import CLCKGen -class Application: +class Application(ApplicationBase): def __init__(self): print_copyright(CR_HOLDERS) self.argv = self.parse_argv() @@ -48,8 +49,7 @@ class Application: signal.signal(signal.SIGINT, self.sig_handler) # Configure logging - log.basicConfig(level = log.DEBUG, - format = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s") + self.app_init_logging(self.argv) def run(self): # Init TRX CTRL interface for BTS @@ -131,6 +131,9 @@ class Application: parser = argparse.ArgumentParser(prog = "fake_trx", description = "Virtual Um-interface (fake transceiver)") + # Register common logging options + self.app_reg_logging_options(parser) + trx_group = parser.add_argument_group("TRX interface") trx_group.add_argument("-b", "--trx-bind-addr", dest = "trx_bind_addr", type = str, default = "0.0.0.0", diff --git a/src/target/trx_toolkit/trx_sniff.py b/src/target/trx_toolkit/trx_sniff.py index 9fb567ed..7a873510 100755 --- a/src/target/trx_toolkit/trx_sniff.py +++ b/src/target/trx_toolkit/trx_sniff.py @@ -32,10 +32,11 @@ import sys import scapy.all +from app_common import ApplicationBase from data_dump import DATADumpFile from data_msg import * -class Application: +class Application(ApplicationBase): # Counters cnt_burst_dropped_num = 0 cnt_burst_num = 0 @@ -51,8 +52,7 @@ class Application: self.argv = self.parse_argv() # Configure logging - log.basicConfig(level = log.DEBUG, - format = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s") + self.app_init_logging(self.argv) # Open requested capture file if self.argv.output_file is not None: @@ -195,6 +195,9 @@ class Application: dest = "verbose", action = "store_true", help = "Print burst bits to stdout") + # Register common logging options + self.app_reg_logging_options(parser) + trx_group = parser.add_argument_group("TRX interface") trx_group.add_argument("-i", "--sniff-interface", dest = "sniff_if", type = str, default = "lo", metavar = "IF", |