Vadim Yanitskiy has uploaded this change for review. ( 
https://gerrit.osmocom.org/12192


Change subject: trx_toolkit: introduce logging configuration arguments
......................................................................

trx_toolkit: introduce logging configuration arguments

Change-Id: Ic3b0440cd73946ad444bd7e48feb7a92d45f6488
---
A src/target/trx_toolkit/app_common.py
M src/target/trx_toolkit/burst_gen.py
M src/target/trx_toolkit/burst_send.py
M src/target/trx_toolkit/ctrl_cmd.py
M src/target/trx_toolkit/fake_trx.py
M src/target/trx_toolkit/trx_sniff.py
6 files changed, 97 insertions(+), 15 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/92/12192/1

diff --git a/src/target/trx_toolkit/app_common.py 
b/src/target/trx_toolkit/app_common.py
new file mode 100644
index 0000000..ed6878f
--- /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 <[email protected]>
+#
+# 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-lvl", 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-fmt", 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-lvl", 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-fmt", 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 7625a27..1c18b3c 100755
--- a/src/target/trx_toolkit/burst_gen.py
+++ b/src/target/trx_toolkit/burst_gen.py
@@ -31,13 +31,14 @@
 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 @@
                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 @@
                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 499e929..16db222 100755
--- a/src/target/trx_toolkit/burst_send.py
+++ b/src/target/trx_toolkit/burst_send.py
@@ -30,12 +30,13 @@
 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 @@
                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 @@
                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 43b24a3..ffc3e46 100755
--- a/src/target/trx_toolkit/ctrl_cmd.py
+++ b/src/target/trx_toolkit/ctrl_cmd.py
@@ -32,9 +32,10 @@
 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 @@
                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 @@
                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 1c991ce..a45ce20 100755
--- a/src/target/trx_toolkit/fake_trx.py
+++ b/src/target/trx_toolkit/fake_trx.py
@@ -31,6 +31,7 @@
 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 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 @@
                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 @@
                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 9fb567e..7a87351 100755
--- a/src/target/trx_toolkit/trx_sniff.py
+++ b/src/target/trx_toolkit/trx_sniff.py
@@ -32,10 +32,11 @@

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

--
To view, visit https://gerrit.osmocom.org/12192
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic3b0440cd73946ad444bd7e48feb7a92d45f6488
Gerrit-Change-Number: 12192
Gerrit-PatchSet: 1
Gerrit-Owner: Vadim Yanitskiy <[email protected]>

Reply via email to