1 # -*- coding: utf-8 -*-
4 # Common helpers for applications
6 # (C) 2018-2020 by Vadim Yanitskiy <axilirator@gmail.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
22 class ApplicationBase
:
23 # Osmocom-style logging message format
24 # Example: [DEBUG] ctrl_if_bts.py:71 Recv POWEROFF cmd
25 LOG_FMT_DEFAULT
= "[%(levelname)s] %(filename)s:%(lineno)d %(message)s"
27 # Default time / date format (e.g. 2003-01-23 00:29:50)
28 LOG_TIME_FMT_DEFAULT
= "%Y-%m-%d %H:%M:%S"
30 def app_print_copyright(self
, holders
= []):
31 # Print copyright holders if any
32 for date
, author
in holders
:
33 print("Copyright (C) %s by %s" % (date
, author
))
35 # Print the license header itself
36 print("License GPLv2+: GNU GPL version 2 or later " \
37 "<http://gnu.org/licenses/gpl.html>\n" \
38 "This is free software: you are free to change and redistribute it.\n" \
39 "There is NO WARRANTY, to the extent permitted by law.\n")
41 def add_log_handler(self
, lh
, log_level
, log_fmt
, time_fmt
, log_time
= False):
42 log_fmt
= "%(asctime)s " + log_fmt
if log_time
else log_fmt
43 lf
= log
.Formatter(log_fmt
, time_fmt
)
44 ll
= log
.getLevelName(log_level
)
46 log
.root
.addHandler(lh
)
50 def app_init_logging(self
, argv
):
51 # Default logging handler (stderr)
52 lo
= (argv
.log_level
, argv
.log_fmt
, argv
.log_time_fmt
, argv
.log_time
)
53 lh
= log
.StreamHandler()
54 self
.add_log_handler(lh
, *lo
)
56 # Optional file handler
57 if argv
.log_file_name
is not None:
58 lo
= (argv
.log_file_level
, argv
.log_file_fmt
,
59 argv
.log_file_time_fmt
, argv
.log_file_time
)
60 lh
= log
.FileHandler(argv
.log_file_name
)
61 self
.add_log_handler(lh
, *lo
)
63 # Set DEBUG for the root logger
64 log
.root
.setLevel(log
.DEBUG
)
66 def app_reg_logging_options(self
, parser
):
67 parser
.add_argument("--log-level", metavar
= "LVL",
68 dest
= "log_level", type = str, default
= "DEBUG",
69 choices
= ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
70 help = "Set logging level (default %(default)s)")
71 parser
.add_argument("--log-time",
72 dest
= "log_time", action
= "store_true",
73 help = "Prefix each log message with the current time")
74 parser
.add_argument("--log-time-format", metavar
= "FMT",
75 dest
= "log_time_fmt", type = str,
76 default
= self
.LOG_TIME_FMT_DEFAULT
,
77 help = "Set time format (default %(default)s)")
78 parser
.add_argument("--log-format", metavar
= "FMT",
79 dest
= "log_fmt", type = str, default
= self
.LOG_FMT_DEFAULT
,
80 help = "Set logging message format")
82 parser
.add_argument("--log-file-name", metavar
= "FILE",
83 dest
= "log_file_name", type = str,
84 help = "Set logging file name")
85 parser
.add_argument("--log-file-level", metavar
= "LVL",
86 dest
= "log_file_level", type = str, default
= "DEBUG",
87 choices
= ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
88 help = "Set logging level for file (default %(default)s)")
89 parser
.add_argument("--log-file-time",
90 dest
= "log_file_time", action
= "store_true",
91 help = "Prefix each log message with the current time")
92 parser
.add_argument("--log-file-time-format", metavar
= "FMT",
93 dest
= "log_file_time_fmt", type = str,
94 default
= self
.LOG_TIME_FMT_DEFAULT
,
95 help = "Set time format for file (default %(default)s)")
96 parser
.add_argument("--log-file-format", metavar
= "FMT",
97 dest
= "log_file_fmt", type = str, default
= self
.LOG_FMT_DEFAULT
,
98 help = "Set logging message format for file")