diff options
-rw-r--r-- | gnuradio-runtime/include/gnuradio/logger.h.in | 15 | ||||
-rw-r--r-- | gnuradio-runtime/lib/block.cc | 46 | ||||
-rw-r--r-- | gnuradio-runtime/lib/logger.cc | 56 |
3 files changed, 72 insertions, 45 deletions
diff --git a/gnuradio-runtime/include/gnuradio/logger.h.in b/gnuradio-runtime/include/gnuradio/logger.h.in index 8e8cd2fb1e..05367a1873 100644 --- a/gnuradio-runtime/include/gnuradio/logger.h.in +++ b/gnuradio-runtime/include/gnuradio/logger.h.in @@ -834,4 +834,19 @@ GR_RUNTIME_API std::vector<std::string> gr_logger_get_logger_names(void); */ GR_RUNTIME_API void gr_logger_reset_config(void); + +namespace gr { + /*! + * Function to use the GR prefs files to get and setup the two + * default loggers defined there. The loggers are unique to the + * class in which they are called, and we pass it the \p name to + * identify where the log message originates from. For a GNU Radio + * block, we use 'alias()' for this value, and this is set up for us + * automatically in gr::block. + */ + GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, + const std::string name); +} /* namespace gr */ + + #endif /* INCLUDED_GR_LOGGER_H */ diff --git a/gnuradio-runtime/lib/block.cc b/gnuradio-runtime/lib/block.cc index 8906d98b52..a15fb89c85 100644 --- a/gnuradio-runtime/lib/block.cc +++ b/gnuradio-runtime/lib/block.cc @@ -60,51 +60,7 @@ namespace gr { message_port_register_in(pmt::mp("system")); set_msg_handler(pmt::mp("system"), boost::bind(&block::system_handler, this, _1)); -#ifdef ENABLE_GR_LOG -#ifdef HAVE_LOG4CPP - 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", ""); - std::string debug_level = p->get_string("LOG", "debug_level", "off"); - std::string debug_file = p->get_string("LOG", "debug_file", ""); - - GR_CONFIG_LOGGER(config_file); - - GR_LOG_GETLOGGER(LOG, "gr_log." + alias()); - GR_LOG_SET_LEVEL(LOG, log_level); - if(log_file.size() > 0) { - if(log_file == "stdout") { - GR_LOG_SET_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n"); - } - else if(log_file == "stderr") { - GR_LOG_SET_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n"); - } - else { - GR_LOG_SET_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n"); - } - } - d_logger = LOG; - - GR_LOG_GETLOGGER(DLOG, "gr_log_debug." + alias()); - GR_LOG_SET_LEVEL(DLOG, debug_level); - if(debug_file.size() > 0) { - if(debug_file == "stdout") { - GR_LOG_SET_CONSOLE_APPENDER(DLOG, "cout","gr::debug :%p: %c{1} - %m%n"); - } - else if(debug_file == "stderr") { - GR_LOG_SET_CONSOLE_APPENDER(DLOG, "cerr", "gr::debug :%p: %c{1} - %m%n"); - } - else { - GR_LOG_SET_FILE_APPENDER(DLOG, debug_file, true, "%r :%p: %c{1} - %m%n"); - } - } - d_debug_logger = DLOG; -#endif /* HAVE_LOG4CPP */ -#else /* ENABLE_GR_LOG */ - d_logger = NULL; - d_debug_logger = NULL; -#endif /* ENABLE_GR_LOG */ + configure_default_loggers(d_logger, d_debug_logger, alias()); } block::~block() diff --git a/gnuradio-runtime/lib/logger.cc b/gnuradio-runtime/lib/logger.cc index c11f384cc6..961ba55724 100644 --- a/gnuradio-runtime/lib/logger.cc +++ b/gnuradio-runtime/lib/logger.cc @@ -31,6 +31,7 @@ #endif #include <gnuradio/logger.h> +#include <gnuradio/prefs.h> #include <stdexcept> #include <algorithm> @@ -42,6 +43,61 @@ namespace gr { bool logger_config::logger_configured(false); + + bool + configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, + const std::string name) + { +#ifdef ENABLE_GR_LOG +#ifdef HAVE_LOG4CPP + 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", ""); + std::string debug_level = p->get_string("LOG", "debug_level", "off"); + std::string debug_file = p->get_string("LOG", "debug_file", ""); + + GR_CONFIG_LOGGER(config_file); + + GR_LOG_GETLOGGER(LOG, "gr_log." + name); + GR_LOG_SET_LEVEL(LOG, log_level); + if(log_file.size() > 0) { + if(log_file == "stdout") { + GR_LOG_SET_CONSOLE_APPENDER(LOG, "cout","gr::log :%p: %c{1} - %m%n"); + } + else if(log_file == "stderr") { + GR_LOG_SET_CONSOLE_APPENDER(LOG, "cerr","gr::log :%p: %c{1} - %m%n"); + } + else { + GR_LOG_SET_FILE_APPENDER(LOG, log_file , true,"%r :%p: %c{1} - %m%n"); + } + } + l = LOG; + + GR_LOG_GETLOGGER(DLOG, "gr_log_debug." + name); + GR_LOG_SET_LEVEL(DLOG, debug_level); + if(debug_file.size() > 0) { + if(debug_file == "stdout") { + GR_LOG_SET_CONSOLE_APPENDER(DLOG, "cout","gr::debug :%p: %c{1} - %m%n"); + } + else if(debug_file == "stderr") { + GR_LOG_SET_CONSOLE_APPENDER(DLOG, "cerr", "gr::debug :%p: %c{1} - %m%n"); + } + else { + GR_LOG_SET_FILE_APPENDER(DLOG, debug_file, true, "%r :%p: %c{1} - %m%n"); + } + } + d = DLOG; + return true; +#endif /* HAVE_LOG4CPP */ + +#else /* ENABLE_GR_LOG */ + l = NULL; + d = NULL; + return false; +#endif /* ENABLE_GR_LOG */ + } + /************************ BEGIN LOG4CPP HELPERS ***********************/ /* Logger config class. This is a singleton that controls how * log4cpp is configured If watch_period>0 a thread is started to |