aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/python/usrp_mpm
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2018-01-15 15:05:20 -0800
committerMartin Braun <martin.braun@ettus.com>2018-02-08 22:18:14 +0100
commite6dc09470d506cfe4e74fc52f8a8ef21c16b9f2f (patch)
tree297a48418f48518bc8eed36bcb7bdf859e54941c /mpm/python/usrp_mpm
parentebacd27d4948193f591ff35266ad453d10cb9b28 (diff)
downloaduhd-e6dc09470d506cfe4e74fc52f8a8ef21c16b9f2f.tar.gz
uhd-e6dc09470d506cfe4e74fc52f8a8ef21c16b9f2f.tar.bz2
uhd-e6dc09470d506cfe4e74fc52f8a8ef21c16b9f2f.zip
mpm: Add prefs modules
- prefs is a module for centralized preferences management - Uses Python's ConfigParser module
Diffstat (limited to 'mpm/python/usrp_mpm')
-rw-r--r--mpm/python/usrp_mpm/CMakeLists.txt1
-rw-r--r--mpm/python/usrp_mpm/mpmlog.py6
-rw-r--r--mpm/python/usrp_mpm/prefs.py92
-rw-r--r--mpm/python/usrp_mpm/sys_utils/CMakeLists.txt2
4 files changed, 99 insertions, 2 deletions
diff --git a/mpm/python/usrp_mpm/CMakeLists.txt b/mpm/python/usrp_mpm/CMakeLists.txt
index 6b2f1a5a0..a3585cc6b 100644
--- a/mpm/python/usrp_mpm/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/CMakeLists.txt
@@ -20,6 +20,7 @@ SET(USRP_MPM_TOP_FILES
${CMAKE_CURRENT_SOURCE_DIR}/mpmlog.py
${CMAKE_CURRENT_SOURCE_DIR}/mpmtypes.py
${CMAKE_CURRENT_SOURCE_DIR}/mpmutils.py
+ ${CMAKE_CURRENT_SOURCE_DIR}/prefs.py
${CMAKE_CURRENT_SOURCE_DIR}/rpc_server.py
)
LIST(APPEND USRP_MPM_FILES ${USRP_MPM_TOP_FILES})
diff --git a/mpm/python/usrp_mpm/mpmlog.py b/mpm/python/usrp_mpm/mpmlog.py
index 0c37e92bf..113d4bd06 100644
--- a/mpm/python/usrp_mpm/mpmlog.py
+++ b/mpm/python/usrp_mpm/mpmlog.py
@@ -148,7 +148,7 @@ def get_main_logger(
DEFAULT_LOG_LEVEL - log_default_delta * 10,
CRITICAL
))
- default_log_level = max(1, default_log_level - (default_log_level % 10))
+ default_log_level = max(TRACE, default_log_level - (default_log_level % 10))
LOGGER.setLevel(default_log_level)
# Connect to C++ logging:
if LOGGER.cpp_log_buf is not None:
@@ -160,6 +160,10 @@ def get_main_logger(
lib_logger.log(log_level, "[%s] %s",
component, message.strip())
LOGGER.cpp_log_buf.set_notify_callback(log_from_cpp)
+ # Flush errors stuck in the prefs module:
+ log = LOGGER.getChild('prefs')
+ for err_key, err_msg in mpm_prefs['__ERRORS__'].items():
+ log.error('%s: %s', err_key, err_msg)
return LOGGER
def get_logger(child_name):
diff --git a/mpm/python/usrp_mpm/prefs.py b/mpm/python/usrp_mpm/prefs.py
new file mode 100644
index 000000000..d244a0b42
--- /dev/null
+++ b/mpm/python/usrp_mpm/prefs.py
@@ -0,0 +1,92 @@
+#
+# Copyright 2018 Ettus Research, a National Instruments Company
+#
+# SPDX-License-Identifier: GPL-3.0
+#
+"""
+MPM preferences management
+"""
+
+import configparser
+from builtins import object
+from usrp_mpm.mpmlog import TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL
+
+# Store the global preferences object
+_PREFS = None
+
+### MPM defaults ##############################################################
+MPM_DEFAULT_CONFFILE_PATH = '/etc/uhd/mpm.conf'
+MPM_DEFAULT_LOG_LEVEL = 'info'
+MPM_DEFAULT_LOG_BUF_SIZE = 100 # Number of log records to buf
+
+class _MPMPrefs(configparser.ConfigParser):
+ """
+ Container for MPM preferences.
+
+ On initialization, it will read preferences from the system-wide config
+ file, if available, and from environment variables.
+ Settings can be updated at any time (e.g., because want to factor in
+ command line arguments).
+
+ This class also encodes default prefs in the self.default_prefs dictionary.
+ Said dictionary needs to be compatible with the ConfigParser.read_dict()
+ API call (refer to the Python documentation).
+ """
+ default_prefs = {
+ 'mpm': {
+ 'log_level': MPM_DEFAULT_LOG_LEVEL,
+ 'log_buf_size': MPM_DEFAULT_LOG_BUF_SIZE,
+ },
+ }
+
+ def __init__(self):
+ configparser.ConfigParser.__init__(self)
+ self.read_dict(self.default_prefs)
+ self._errors = []
+ try:
+ self.read(MPM_DEFAULT_CONFFILE_PATH)
+ except configparser.Error as ex:
+ self._errors.append('Config file parsing error: {}'.format(str(ex)))
+
+ def get_log_level(self):
+ """
+ Return the selected default log level as an integer, usable by the
+ logging API. This is a safe call, it will always return a useful value.
+ Because this gets called as part of the logging API, it can't log any
+ error messages. It will store them in the object itself under the
+ __ERRORS__ section, key 'log_level'.
+ """
+ log_level = self.get('mpm', 'log_level').lower()
+ log_level_map = {
+ 'trace': TRACE,
+ 'debug': DEBUG,
+ 'info': INFO,
+ 'warning': WARNING,
+ 'error': ERROR,
+ 'critical': CRITICAL,
+ }
+ if log_level not in log_level_map:
+ self._errors.append('Invalid log level: {}'.format(log_level))
+ # Note: After this function returns, we can use the logger so we
+ # don't need to use this awkward side channel anymore!
+ return log_level_map.get(
+ log_level,
+ log_level_map[MPM_DEFAULT_LOG_LEVEL]
+ )
+
+ def get_log_errors(self):
+ """
+ Returns errors that were generated during init but couldn't be logged
+ because the logger isn't ready yet.
+ """
+ return self._errors
+
+def get_prefs():
+ """
+ Return singleton preferences object. It's an object of type ConfigParser.
+ """
+ global _PREFS
+ if _PREFS is None:
+ _PREFS = _MPMPrefs()
+ return _PREFS
+
diff --git a/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt b/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt
index 5a9d79034..ee7c249df 100644
--- a/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt
+++ b/mpm/python/usrp_mpm/sys_utils/CMakeLists.txt
@@ -1,5 +1,5 @@
#
-# Copyright 2017 Ettus Research, National Instruments Company
+# Copyright 2017 Ettus Research, a National Instruments Company
#
# SPDX-License-Identifier: GPL-3.0
#