diff options
author | David Winter <david.winter@analog.com> | 2021-06-28 09:46:48 +0200 |
---|---|---|
committer | mormj <34754695+mormj@users.noreply.github.com> | 2021-07-02 14:05:04 -0400 |
commit | 4071620361e32264c2581a77d104c12bfef62133 (patch) | |
tree | e12472cc8582dde463d12c0cf2db9df44d2eb839 | |
parent | 078d40ddeab957fcc91060131a4b3a1dfe9df676 (diff) |
gr: logger: Add logger_get_configured_logger()
A helper method is added to gr/lib/logger.cc to facilitate the
allocation of loggers which are automatically configured according to the
local GNURadio configuration.
Signed-off-by: David Winter <david.winter@analog.com>
-rw-r--r-- | gnuradio-runtime/include/gnuradio/logger.h | 19 | ||||
-rw-r--r-- | gnuradio-runtime/lib/logger.cc | 25 | ||||
-rw-r--r-- | gnuradio-runtime/python/gnuradio/gr/bindings/logger_python.cc | 7 |
3 files changed, 49 insertions, 2 deletions
diff --git a/gnuradio-runtime/include/gnuradio/logger.h b/gnuradio-runtime/include/gnuradio/logger.h index 1ce26a16d6..6891706fb0 100644 --- a/gnuradio-runtime/include/gnuradio/logger.h +++ b/gnuradio-runtime/include/gnuradio/logger.h @@ -61,6 +61,9 @@ typedef log4cpp::Category* logger_ptr; #define GR_LOG_ASSIGN_LOGPTR(logger, name) logger = gr::logger_get_logger(name) +#define GR_LOG_ASSIGN_CONFIGURED_LOGPTR(logger, name) \ + logger = gr::logger_get_configured_logger(name) + #define GR_CONFIG_LOGGER(config) gr::logger_config::load_config(config) #define GR_CONFIG_AND_WATCH_LOGGER(config, period) \ @@ -68,6 +71,9 @@ typedef log4cpp::Category* logger_ptr; #define GR_LOG_GETLOGGER(logger, name) gr::logger_ptr logger = gr::logger_get_logger(name) +#define GR_LOG_GET_CONFIGURED_LOGGER(logger, name) \ + gr::logger_ptr logger = gr::logger_get_configured_logger(name) + #define GR_SET_LEVEL(name, level) \ { \ gr::logger_ptr logger = gr::logger_get_logger(name); \ @@ -379,6 +385,19 @@ public: GR_RUNTIME_API logger_ptr logger_get_logger(std::string name); /*! + * \brief Retrieve a pointer to a fully configured logger by name + * + * Retrieves a logger pointer. + * This method differs from logger_get_logger in that it configures the logger to + * reflect the current gnuradio configuration, including log level and log output file. + * + * \p name. + * + * \param name Name of the logger for which a pointer is requested + */ +GR_RUNTIME_API logger_ptr logger_get_configured_logger(const std::string& name); + +/*! * \brief Load logger's configuration file. * * Initialize the GNU Radio logger by loading the configuration file diff --git a/gnuradio-runtime/lib/logger.cc b/gnuradio-runtime/lib/logger.cc index ea3febfc34..eebc4a68e2 100644 --- a/gnuradio-runtime/lib/logger.cc +++ b/gnuradio-runtime/lib/logger.cc @@ -153,6 +153,31 @@ logger_ptr logger_get_logger(std::string name) } } +logger_ptr logger_get_configured_logger(const std::string& name) +{ + if (log4cpp::Category::exists(name)) + return &log4cpp::Category::getInstance(name); + + prefs* p = prefs::singleton(); + std::string config_file = p->get_string("LOG", "log_config", ""); + std::string log_level = p->get_string("LOG", "log_level", "off"); + std::string log_file = p->get_string("LOG", "log_file", ""); + + GR_LOG_GETLOGGER(LOG, "gr_log." + name); + GR_LOG_SET_LEVEL(LOG, log_level); + + if (!log_file.empty()) { + if (log_file == "stdout") { + GR_LOG_SET_CONSOLE_APPENDER(LOG, "stdout", "gr::log :%p: %c{1} - %m%n"); + } else if (log_file == "stderr") { + GR_LOG_SET_CONSOLE_APPENDER(LOG, "stderr", "gr::log :%p: %c{1} - %m%n"); + } else { + GR_LOG_SET_FILE_APPENDER(LOG, log_file, true, "%r :%p: %c{1} - %m%n"); + } + } + return LOG; +} + bool logger_load_config(const std::string& config_filename) { if (!config_filename.empty()) { diff --git a/gnuradio-runtime/python/gnuradio/gr/bindings/logger_python.cc b/gnuradio-runtime/python/gnuradio/gr/bindings/logger_python.cc index 1447b76dae..8ec43201fb 100644 --- a/gnuradio-runtime/python/gnuradio/gr/bindings/logger_python.cc +++ b/gnuradio-runtime/python/gnuradio/gr/bindings/logger_python.cc @@ -13,8 +13,8 @@ /* If manual edits are made, the following tags should be modified accordingly. */ /* BINDTOOL_GEN_AUTOMATIC(0) */ /* BINDTOOL_USE_PYGCCXML(0) */ -/* BINDTOOL_HEADER_FILE(logger.h) */ -/* BINDTOOL_HEADER_FILE_HASH(b071f4deb18875805b73952a7aa43389) */ +/* BINDTOOL_HEADER_FILE(logger.h) */ +/* BINDTOOL_HEADER_FILE_HASH(eaf28bcaeb7c34dc0fca3fdd4a9860c0) */ /***********************************************************************************/ #include <pybind11/complex.h> @@ -100,6 +100,9 @@ void bind_logger(py::module& m) py::arg("name"), py::arg("alias")); m.def("logger_get_logger", &gr::logger_get_logger, py::arg("name")); + m.def("logger_get_configured_logger", + &gr::logger_get_configured_logger, + py::arg("name")); m.def("logger_load_config", &gr::logger_load_config, py::arg("config_filename") = ""); m.def("gr_logger_reset_config", &gr_logger_reset_config); m.def("logger_set_level", |