From 6bab6acee634fcd05a175428e1077ab00188829a Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Fri, 7 Dec 2018 05:00:26 +0700 Subject: trx_toolkit: use generic logging module instead of print() There are multiple advantages of using Python's logging module: - advanced message formatting (file name, line number, etc.), - multiple logging targets (e.g. stderr, file, socket), - logging levels (e.g. DEBUG, INFO, ERROR), - the pythonic way ;) so, let's replace multiple print() calls by logging calls, add use the following logging message format by default: [%(levelname)s] %(filename)s:%(lineno)d %(message)s Examples: [INFO] ctrl_if_bts.py:57 Starting transceiver... [DEBUG] clck_gen.py:87 IND CLOCK 26826 [DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd [INFO] ctrl_if_bts.py:73 Stopping transceiver... [INFO] fake_trx.py:127 Shutting down... Please note that there is no way to filter messages by logging level yet. This is to be introduced soon, together with argparse. Change-Id: I7fcafabafe8323b58990997a47afdd48b6d1f357 --- src/target/trx_toolkit/clck_gen.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/target/trx_toolkit/clck_gen.py') diff --git a/src/target/trx_toolkit/clck_gen.py b/src/target/trx_toolkit/clck_gen.py index b488770e..56207f46 100755 --- a/src/target/trx_toolkit/clck_gen.py +++ b/src/target/trx_toolkit/clck_gen.py @@ -25,6 +25,7 @@ from copyright import print_copyright CR_HOLDERS = [("2017-2018", "Vadim Yanitskiy ")] +import logging as log import signal import time import sys @@ -83,7 +84,7 @@ class CLCKGen: link.send(payload) # Debug print - print("[T] %s" % payload) + log.debug(payload) # Increase frame count self.clck_src += self.ind_period @@ -101,13 +102,17 @@ class Application: # Set up signal handlers signal.signal(signal.SIGINT, self.sig_handler) + # Configure logging + log.basicConfig(level = log.DEBUG, + format = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s") + def run(self): self.link = UDPLink("127.0.0.1", 5800, "0.0.0.0", 5700) self.clck = CLCKGen([self.link], ind_period = 51) self.clck.start() def sig_handler(self, signum, frame): - print("Signal %d received" % signum) + log.info("Signal %d received" % signum) if signum is signal.SIGINT: self.clck.stop() -- cgit v1.2.3