summaryrefslogtreecommitdiffstats
path: root/src/target/trx_toolkit/app_common.py
blob: 9bcd5930856367a8b74137be9ab70c1c9db5974d (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
#!/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_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 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")