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
This commit is contained in:
Vadim Yanitskiy 2018-12-07 09:34:00 +07:00
parent 5cb7e287ed
commit cdf349bc64
6 changed files with 97 additions and 15 deletions

View File

@ -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")

View File

@ -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",

View File

@ -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",

View File

@ -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",

View File

@ -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",

View File

@ -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",