summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVadim Yanitskiy <axilirator@gmail.com>2020-04-09 04:08:04 +0700
committerVadim Yanitskiy <axilirator@gmail.com>2020-04-09 04:44:49 +0700
commit75ae23674c3c74bc83361559db0e218432562098 (patch)
treeaddae7b358463a62f2902f37442c2b245f85a643
parentec71203e79bf1b585ad56b80743759562898698f (diff)
trx_toolkit/app_common: add options to enable time printing
-rw-r--r--src/target/trx_toolkit/app_common.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/src/target/trx_toolkit/app_common.py b/src/target/trx_toolkit/app_common.py
index 8ee5054c..64baa6d0 100644
--- a/src/target/trx_toolkit/app_common.py
+++ b/src/target/trx_toolkit/app_common.py
@@ -4,7 +4,7 @@
# TRX Toolkit
# Common helpers for applications
#
-# (C) 2018 by Vadim Yanitskiy <axilirator@gmail.com>
+# (C) 2018-2020 by Vadim Yanitskiy <axilirator@gmail.com>
#
# All Rights Reserved
#
@@ -29,6 +29,9 @@ class ApplicationBase:
# Example: [DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
LOG_FMT_DEFAULT = "[%(levelname)s] %(filename)s:%(lineno)d %(message)s"
+ # Default time / date format (e.g. 2003-01-23 00:29:50)
+ LOG_TIME_FMT_DEFAULT = "%Y-%m-%d %H:%M:%S"
+
def app_print_copyright(self, holders = []):
# Print copyright holders if any
for date, author in holders:
@@ -40,8 +43,9 @@ class ApplicationBase:
"This is free software: you are free to change and redistribute it.\n" \
"There is NO WARRANTY, to the extent permitted by law.\n")
- def add_log_handler(self, lh, log_level, log_fmt):
- lf = log.Formatter(log_fmt)
+ def add_log_handler(self, lh, log_level, log_fmt, time_fmt, log_time = False):
+ log_fmt = "%(asctime)s " + log_fmt if log_time else log_fmt
+ lf = log.Formatter(log_fmt, time_fmt)
ll = log.getLevelName(log_level)
log.root.addHandler(lh)
@@ -50,13 +54,14 @@ class ApplicationBase:
def app_init_logging(self, argv):
# Default logging handler (stderr)
- lo = (argv.log_level, argv.log_fmt)
+ lo = (argv.log_level, argv.log_fmt, argv.log_time_fmt, argv.log_time)
lh = log.StreamHandler()
self.add_log_handler(lh, *lo)
# Optional file handler
if argv.log_file_name is not None:
- lo = (argv.log_file_level, argv.log_file_fmt)
+ lo = (argv.log_file_level, argv.log_file_fmt,
+ argv.log_file_time_fmt, argv.log_file_time)
lh = log.FileHandler(argv.log_file_name)
self.add_log_handler(lh, *lo)
@@ -68,6 +73,13 @@ class ApplicationBase:
dest = "log_level", type = str, default = "DEBUG",
choices = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help = "Set logging level (default %(default)s)")
+ parser.add_argument("--log-time",
+ dest = "log_time", action = "store_true",
+ help = "Prefix each log message with the current time")
+ parser.add_argument("--log-time-format", metavar = "FMT",
+ dest = "log_time_fmt", type = str,
+ default = self.LOG_TIME_FMT_DEFAULT,
+ help = "Set time format (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")
@@ -79,6 +91,13 @@ class ApplicationBase:
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-time",
+ dest = "log_file_time", action = "store_true",
+ help = "Prefix each log message with the current time")
+ parser.add_argument("--log-file-time-format", metavar = "FMT",
+ dest = "log_file_time_fmt", type = str,
+ default = self.LOG_TIME_FMT_DEFAULT,
+ help = "Set time format 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")