summaryrefslogtreecommitdiffstats
path: root/src/target/trx_toolkit/app_common.py
blob: 64baa6d0f75dba6ae1f942ba6f608caba700ed78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# TRX Toolkit
# Common helpers for applications
#
# (C) 2018-2020 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"

	# 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:
			print("Copyright (C) %s by %s" % (date, author))

		# Print the license header itself
		print("License GPLv2+: GNU GPL version 2 or later " \
			"<http://gnu.org/licenses/gpl.html>\n" \
			"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, 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)
		lh.setFormatter(lf)
		lh.setLevel(ll)

	def app_init_logging(self, argv):
		# Default logging handler (stderr)
		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,
			      argv.log_file_time_fmt, argv.log_file_time)
			lh = log.FileHandler(argv.log_file_name)
			self.add_log_handler(lh, *lo)

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

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