diff options
-rw-r--r-- | docs/doxygen/other/main_page.dox | 57 | ||||
-rw-r--r-- | gnuradio-core/src/lib/filter/gr_fir_sysconfig_x86.cc | 14 | ||||
-rw-r--r-- | gnuradio-core/src/lib/runtime/gr_log.cc | 35 | ||||
-rw-r--r-- | gnuradio-core/src/lib/runtime/gr_log.h | 65 | ||||
-rw-r--r-- | gnuradio-core/src/lib/runtime/gr_log.i | 5 |
5 files changed, 160 insertions, 16 deletions
diff --git a/docs/doxygen/other/main_page.dox b/docs/doxygen/other/main_page.dox index ec8642de66..cd948c3058 100644 --- a/docs/doxygen/other/main_page.dox +++ b/docs/doxygen/other/main_page.dox @@ -81,13 +81,35 @@ on how configuration works and looks. Mostly, a default configuration script provided with GNU Radio can be used. After installation, the default configuration script is located at: - $prefix/share/gnuradio/gr_log_default.xml + $prefix/etc/gnuradio/gr_log_default.xml -In a class, we must first configure the logger and then get access to it: +However, we use the global GNU Radio configuration file to tell the +system where this file is located. In the [LOG] section of the +configuration file, the location of the logger's XML file can be +specified. The default configuration file is found in: + + $prefix/etc/gnuradio/conf.d/gnuradio-core.conf + +A local "~/.gnuradio/config.conf" file can be used to override any +parameter in the global file. + +For the following examples, we will assume that our local +"~/.gnuradio/config.conf" looks like this: + +\code +[LOG] +log_file = /opt/gr/etc/gnuadio/gr_log_default.xml +log_level = All +\endcode + +The startup and configuration process proceeds as follows. \code - GR_CONFIG_LOGGER("prefix/etc/gnuradio/conf.d/gr_log_default.xml"); - GR_LOG_GETLOGGER(LOG, name); + std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); + GR_CONFIG_LOGGER(log_file); + GR_LOG_GETLOGGER(LOG, <name>); + GR_LOG_SET_LEVEL(LOG, log_level); \endcode The default config file has two names that can be used (as std::string @@ -95,6 +117,9 @@ types): "gr_log" and "gr_log_debug". The first one will print all levels of logging informaiton while the second one will only print Debug and above. +Names of loggers are global, so "gr_log" as a name will be the same +logger no matter where it is picked from. + The "LOG" name for the logger is now a LoggerPtr object and can be named anything. @@ -107,17 +132,19 @@ something like "gr_log.costas_loop". This will inherit all properties of the parent logger, "gr_log". After calling "GR_LOG_GETLOGGER", the LoggerPtr that was specified, -"LOG" in the example above, is used to set the properties. For -instance, the level for any logger can be easily modified using the -setLogger(level) method, such as: - -\code - LOG->setLevel(log4cxx::Level::getAll()); -\endcode - -In this case, the "getAll()" method sets the logger's level to log -everything. Other methods are "getDebug()", "getInfo()", and so on for -the different levels of logging. +"LOG" in the example above, is used to set the properties. The main +setting we are generally interested in is the level of the +logger. This will determine if this logger gets outputted or not. In +this case, we are actually getting the gr_log level from the GNU Radio +configuration file, stored in log_level. The macro GR_LOG_SET_LEVEL +can take either a string or a log4cxx::LevelPtr object. The log_level +variable in this case holds a string read in from the config file, and +it's case insensitive. I can be "off", "all", "trace", "debug", +"info", "warn", "error", or "fatal" to match with the levels above. + +In our example here, all logging information is displayed because the +config file specifies "All". We can then easily turn off all reporting +by setting this value to "Off". The various logging macros are defined in gr_log.h. Here are some simple exmaples of using them: diff --git a/gnuradio-core/src/lib/filter/gr_fir_sysconfig_x86.cc b/gnuradio-core/src/lib/filter/gr_fir_sysconfig_x86.cc index 4e3598c5d4..a218015154 100644 --- a/gnuradio-core/src/lib/filter/gr_fir_sysconfig_x86.cc +++ b/gnuradio-core/src/lib/filter/gr_fir_sysconfig_x86.cc @@ -178,8 +178,10 @@ gr_fir_sysconfig_x86::create_gr_fir_ccf (const std::vector<float> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_3dnow ()){ if (first){ @@ -210,8 +212,10 @@ gr_fir_sysconfig_x86::create_gr_fir_fcc (const std::vector<gr_complex> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_3dnow ()){ if (first){ @@ -242,8 +246,10 @@ gr_fir_sysconfig_x86::create_gr_fir_ccc (const std::vector<gr_complex> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_3dnowext ()){ if (first) { @@ -282,8 +288,10 @@ gr_fir_sysconfig_x86::create_gr_fir_fff (const std::vector<float> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_3dnow ()){ if (first) { @@ -314,8 +322,10 @@ gr_fir_sysconfig_x86::create_gr_fir_fsf (const std::vector<float> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_3dnow ()){ if (first) { @@ -349,8 +359,10 @@ gr_fir_sysconfig_x86::create_gr_fir_sss (const std::vector<short> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_sse2 ()){ if(first) { @@ -379,8 +391,10 @@ gr_fir_sysconfig_x86::create_gr_fir_scc (const std::vector<gr_complex> &taps) static bool first = true; std::string log_file = gr_prefs::singleton()->get_string("LOG", "log_config", ""); + std::string log_level = gr_prefs::singleton()->get_string("LOG", "log_level", "off"); GR_CONFIG_LOGGER(log_file); GR_LOG_GETLOGGER(LOG, "gr_log.gr_fir_sysconfig_x86"); + GR_LOG_SET_LEVEL(LOG, log_level); if (gr_cpu::has_3dnowext ()){ if (first){ diff --git a/gnuradio-core/src/lib/runtime/gr_log.cc b/gnuradio-core/src/lib/runtime/gr_log.cc index 7af55b155c..9be3ba04fd 100644 --- a/gnuradio-core/src/lib/runtime/gr_log.cc +++ b/gnuradio-core/src/lib/runtime/gr_log.cc @@ -38,6 +38,9 @@ #endif #include <gr_log.h> +#include <stdexcept> +#include <algorithm> + void logger_load_config(const std::string &config_filename) @@ -52,3 +55,35 @@ logger_load_config(const std::string &config_filename) PropertyConfigurator::configure(config_filename); } } + +void +logger_set_level(LoggerPtr logger, const std::string &level) +{ + std::string nocase = level; + std::transform(level.begin(), level.end(), nocase.begin(), ::tolower); + + if(nocase == "off") + logger_set_level(logger, log4cxx::Level::getOff()); + else if(nocase == "all") + logger_set_level(logger, log4cxx::Level::getAll()); + else if(nocase == "trace") + logger_set_level(logger, log4cxx::Level::getTrace()); + else if(nocase == "debug") + logger_set_level(logger, log4cxx::Level::getDebug()); + else if(nocase == "info") + logger_set_level(logger, log4cxx::Level::getInfo()); + else if(nocase == "warn") + logger_set_level(logger, log4cxx::Level::getWarn()); + else if(nocase == "error") + logger_set_level(logger, log4cxx::Level::getError()); + else if(nocase == "fatal") + logger_set_level(logger, log4cxx::Level::getFatal()); + else + throw std::runtime_error("logger_set_level: Bad level type.\n"); +} + +void +logger_set_level(LoggerPtr logger, log4cxx::LevelPtr level) +{ + logger->setLevel(level); +} diff --git a/gnuradio-core/src/lib/runtime/gr_log.h b/gnuradio-core/src/lib/runtime/gr_log.h index 9050b70eb3..e932257a32 100644 --- a/gnuradio-core/src/lib/runtime/gr_log.h +++ b/gnuradio-core/src/lib/runtime/gr_log.h @@ -74,6 +74,13 @@ using namespace log4cxx::helpers; #define GR_LOG_GETLOGGER(logger, name) \ LoggerPtr logger = gr_log::getLogger(name); +#define GR_LOG_SET_LEVEL(logger, level) \ + logger_set_level(logger, level); + +#define GR_SET_LEVEL(name, level) \ + LoggerPtr logger = Logger::getLogger(name); \ + logger_set_level(logger, level); + /* Logger name referenced macros */ #define GR_TRACE(name, msg) { \ LoggerPtr logger = Logger::getLogger(name); \ @@ -135,10 +142,58 @@ using namespace log4cxx::helpers; assert(cond);} -// Load configuration file +/*! + * \brief Load logger's configuration file. + * + * Initialize the GNU Radio logger by loading the configuration file + * \p config_filename. + * + * \param config_filename The configuration file. Set to "" for the + * basic logger that outputs to the console. + */ void logger_load_config(const std::string &config_filename=""); /*! + * \brief Set the logger's output level. + * + * Sets the level of the logger. This takes a string that is + * translated to the standard levels and can be (case insensitive): + * + * \li off + * \li all + * \li trace + * \li debug + * \li info + * \li warn + * \li error + * \li fatal + * + * \param logger the logger to set the level of. + * \param level string to set the level to. + */ +void logger_set_level(LoggerPtr logger, const std::string &level); + +/*! + * \brief Set the logger's output level. + * + * Sets the level of the logger. This takes the actual Log4cxx::Level + * data type, which can be: + * + * \li Log4cxx::Level::getOff() + * \li Log4cxx::Level::getAll() + * \li Log4cxx::Level::getTrace() + * \li Log4cxx::Level::getDebug() + * \li Log4cxx::Level::getInfo() + * \li Log4cxx::Level::getWarn() + * \li Log4cxx::Level::getError() + * \li Log4cxx::Level::getFatal() + * + * \param logger the logger to set the level of. + * \param level new logger level of type Log4cxx::Level + */ +void logger_set_level(LoggerPtr logger, log4cxx::LevelPtr level); + +/*! * \brief instantiate (configure) logger. * \ingroup logging * @@ -175,6 +230,9 @@ class gr_log return logger; }; + /*! \brief inline function, wrapper to set the logger level */ + void set_level(std::string name,std::string level){GR_SET_LEVEL(name,level);} + // Wrappers for logging macros /*! \brief inline function, wrapper for LOG4CXX_TRACE for TRACE message */ void trace(std::string name,std::string msg){GR_TRACE(name,msg);}; @@ -200,6 +258,10 @@ class gr_log /*! \brief inline function, wrapper for LOG4CXX_ASSERT for conditional ERROR message */ void gr_assert(std::string name,bool cond,std::string msg){GR_ASSERT(name,cond,msg);}; + + /*! \brief inline function, wrapper to set the logger level */ + void set_log_level(LoggerPtr logger,std::string level){GR_LOG_SET_LEVEL(logger,level);} + /*! \brief inline function, wrapper for LOG4CXX_TRACE for TRACE message */ void log_trace(LoggerPtr logger,std::string msg){GR_LOG_TRACE(logger,msg);}; @@ -231,6 +293,7 @@ class gr_log #define GR_CONFIG_LOGGER(config) #define GR_LOG_GETLOGGER(logger, name) +#define GR_LOG_SET_LEVEL(logger, level) #define GR_TRACE(name, msg) #define GR_DEBUG(name, msg) #define GR_INFO(name, msg) diff --git a/gnuradio-core/src/lib/runtime/gr_log.i b/gnuradio-core/src/lib/runtime/gr_log.i index e539c5528b..4c9cc4f255 100644 --- a/gnuradio-core/src/lib/runtime/gr_log.i +++ b/gnuradio-core/src/lib/runtime/gr_log.i @@ -54,6 +54,9 @@ public: ~LoggerPtr(); }; +void logger_load_config(const std::string &config_filename); +void logger_set_level(LoggerPtr logger, const std::string &level); + class gr_log { private: @@ -61,6 +64,7 @@ private: public: // gr_log(std::string config_filename); gr_log(std::string config_filename,int watchPeriodSec); + void set_level(std::string name,std::string level); void trace(std::string name,std::string msg); void debug(std::string name,std::string msg); void info(std::string name,std::string msg); @@ -72,6 +76,7 @@ public: static LoggerPtr getLogger(std::string name); + void set_log_level(LoggerPtr logger,std::string level); void log_trace(LoggerPtr logger,std::string msg); void log_debug(LoggerPtr logger,std::string msg); void log_info(LoggerPtr logger,std::string msg); |