aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/utils
diff options
context:
space:
mode:
authorAndrej Rode <andrej.rode@ettus.com>2017-02-07 16:37:25 -0800
committerMartin Braun <martin.braun@ettus.com>2017-02-20 17:13:15 -0800
commit21aad77c9ca07f4271136b9241f5adb00a6bb908 (patch)
tree636ffe3ab2296e9afa661d3a12eb359224cd3254 /host/lib/utils
parent2b33f2bb4c01d4306fd46f78edf6e355a03e2ed7 (diff)
downloaduhd-21aad77c9ca07f4271136b9241f5adb00a6bb908.tar.gz
uhd-21aad77c9ca07f4271136b9241f5adb00a6bb908.tar.bz2
uhd-21aad77c9ca07f4271136b9241f5adb00a6bb908.zip
utils: introduce new logging API and remove msg API
Diffstat (limited to 'host/lib/utils')
-rw-r--r--host/lib/utils/CMakeLists.txt1
-rw-r--r--host/lib/utils/gain_group.cpp4
-rw-r--r--host/lib/utils/log.cpp218
-rw-r--r--host/lib/utils/msg.cpp134
-rw-r--r--host/lib/utils/tasks.cpp18
-rw-r--r--host/lib/utils/thread_priority.cpp4
-rw-r--r--host/lib/utils/thread_priority_c.cpp2
7 files changed, 161 insertions, 220 deletions
diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt
index 128d7c00a..f76c5763a 100644
--- a/host/lib/utils/CMakeLists.txt
+++ b/host/lib/utils/CMakeLists.txt
@@ -142,7 +142,6 @@ LIBUHD_APPEND_SOURCES(
${CMAKE_CURRENT_SOURCE_DIR}/ihex.cpp
${CMAKE_CURRENT_SOURCE_DIR}/load_modules.cpp
${CMAKE_CURRENT_SOURCE_DIR}/log.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/msg.cpp
${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
${CMAKE_CURRENT_SOURCE_DIR}/static.cpp
diff --git a/host/lib/utils/gain_group.cpp b/host/lib/utils/gain_group.cpp
index be5b55444..1a6c0407f 100644
--- a/host/lib/utils/gain_group.cpp
+++ b/host/lib/utils/gain_group.cpp
@@ -142,11 +142,11 @@ public:
gain_bucket.at(i) += additional_gain;
gain_left_to_distribute -= additional_gain;
}
- UHD_LOGV(often) << "gain_left_to_distribute " << gain_left_to_distribute << std::endl;
+ UHD_LOGGER_DEBUG("UHD") << "gain_left_to_distribute " << gain_left_to_distribute ;
//now write the bucket out to the individual gain values
for (size_t i = 0; i < gain_bucket.size(); i++){
- UHD_LOGV(often) << i << ": " << gain_bucket.at(i) << std::endl;
+ UHD_LOGGER_DEBUG("UHD") << i << ": " << gain_bucket.at(i) ;
all_fcns.at(i).set_value(gain_bucket.at(i));
}
}
diff --git a/host/lib/utils/log.cpp b/host/lib/utils/log.cpp
index 4e58ce894..7fc28f122 100644
--- a/host/lib/utils/log.cpp
+++ b/host/lib/utils/log.cpp
@@ -16,11 +16,9 @@
//
#include <uhd/utils/log.hpp>
-#include <uhd/utils/msg.hpp>
#include <uhd/utils/static.hpp>
#include <uhd/utils/paths.hpp>
#include <boost/filesystem.hpp>
-#include <boost/format.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/locks.hpp>
@@ -37,59 +35,99 @@ namespace ip = boost::interprocess;
**********************************************************************/
class log_resource_type{
public:
- uhd::_log::verbosity_t level;
+ uhd::log::severity_level level;
+ uhd::log::severity_level file_level;
+ uhd::log::severity_level console_level;
log_resource_type(void){
//file lock pointer must be null
_file_lock = NULL;
+ //default to no file logging
+ this->file_logging = false;
+
//set the default log level
- level = uhd::_log::never;
+ this->level = uhd::log::off;
+ this->file_level = uhd::log::off;
+ this->console_level = uhd::log::off;
//allow override from macro definition
- #ifdef UHD_LOG_LEVEL
- _set_log_level(BOOST_STRINGIZE(UHD_LOG_LEVEL));
- #endif
-
- //allow override from environment variable
+#ifdef UHD_LOG_MIN_LEVEL
+ this->level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_MIN_LEVEL));
+#endif
+#if defined(UHD_LOG_FILE_LEVEL) && defined(UHD_LOG_FILE_PATH)
+ this->file_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_FILE_LEVEL));
+#endif
+#ifdef UHD_LOG_CONSOLE_LEVEL
+ this->console_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_CONSOLE_LEVEL));
+#endif
+#ifdef UHD_LOG_FILE
+ this->log_file_target = BOOST_STRINGIZE(UHD_LOG_FILE);
+ this->file_logging = true;
+#endif
+
+ //allow override from environment variables
const char * log_level_env = std::getenv("UHD_LOG_LEVEL");
- if (log_level_env != NULL) _set_log_level(log_level_env);
+ if (log_level_env != NULL && log_level_env[0] != '\0') this->level = _get_log_level(log_level_env);
+
+ const char * log_file_level_env = std::getenv("UHD_LOG_FILE_LEVEL");
+ if (log_file_level_env != NULL && log_file_level_env[0] != '\0') this->file_level = _get_log_level(log_file_level_env);
+
+ const char * log_console_level_env = std::getenv("UHD_LOG_CONSOLE_LEVEL");
+ if (log_console_level_env != NULL && log_console_level_env[0] != '\0') this->console_level = _get_log_level(log_console_level_env);
+
+ const char* log_file_env = std::getenv("UHD_LOG_FILE");
+ if ((log_file_env != NULL) && (log_file_env[0] != '\0')) {
+ this->log_file_target = log_file_env;
+ this->file_logging = true;
+ }
}
~log_resource_type(void){
- boost::lock_guard<boost::mutex> lock(_mutex);
- _file_stream.close();
- if (_file_lock != NULL) delete _file_lock;
+ if (this->file_logging){
+ boost::lock_guard<boost::mutex> lock(_mutex);
+ _file_stream.close();
+ if (_file_lock != NULL) delete _file_lock;
+ }
}
void log_to_file(const std::string &log_msg){
- boost::lock_guard<boost::mutex> lock(_mutex);
- if (_file_lock == NULL){
- const std::string log_path = (fs::path(uhd::get_tmp_path()) / "uhd.log").string();
- _file_stream.open(log_path.c_str(), std::fstream::out | std::fstream::app);
- _file_lock = new ip::file_lock(log_path.c_str());
+ if ( this->file_logging ){
+ boost::lock_guard<boost::mutex> lock(_mutex);
+ if (_file_lock == NULL){
+ _file_stream.open(this->log_file_target.c_str(), std::fstream::out | std::fstream::app);
+ _file_lock = new ip::file_lock(this->log_file_target.c_str());
+ }
+ _file_lock->lock();
+ _file_stream << log_msg << std::flush;
+ _file_lock->unlock();
}
- _file_lock->lock();
- _file_stream << log_msg << std::flush;
- _file_lock->unlock();
}
private:
//! set the log level from a string that is either a digit or an enum name
- void _set_log_level(const std::string &log_level_str){
- const uhd::_log::verbosity_t log_level_num = uhd::_log::verbosity_t(log_level_str[0]-'0');
- if (std::isdigit(log_level_str[0]) and log_level_num >= uhd::_log::always and log_level_num <= uhd::_log::never){
- this->level = log_level_num;
- return;
+ bool file_logging;
+ std::string log_file_target;
+ uhd::log::severity_level _get_log_level(const std::string &log_level_str){
+ if (std::isdigit(log_level_str[0])) {
+ const uhd::log::severity_level log_level_num =
+ uhd::log::severity_level(std::stoi(log_level_str));
+ if (log_level_num >= uhd::log::trace and
+ log_level_num <= uhd::log::fatal) {
+ return log_level_num;
+ }
}
- #define if_lls_equal(name) else if(log_level_str == #name) this->level = uhd::_log::name
- if_lls_equal(always);
- if_lls_equal(often);
- if_lls_equal(regularly);
- if_lls_equal(rarely);
- if_lls_equal(very_rarely);
- if_lls_equal(never);
+
+#define if_loglevel_equal(name) \
+ else if (log_level_str == #name) return uhd::log::name
+ if_loglevel_equal(trace);
+ if_loglevel_equal(debug);
+ if_loglevel_equal(info);
+ if_loglevel_equal(warning);
+ if_loglevel_equal(error);
+ if_loglevel_equal(fatal);
+ return uhd::log::off;
}
//file stream and lock:
@@ -104,39 +142,55 @@ UHD_SINGLETON_FCN(log_resource_type, log_rs);
* The logger object implementation
**********************************************************************/
//! get the relative file path from the host directory
-static std::string get_rel_file_path(const fs::path &file){
- fs::path abs_path = file.parent_path();
- fs::path rel_path = file.leaf();
- while (not abs_path.empty() and abs_path.leaf() != "host"){
- rel_path = abs_path.leaf() / rel_path;
- abs_path = abs_path.parent_path();
- }
- return rel_path.string();
-}
+inline std::string path_to_filename(std::string path)
+{
+ return path.substr(path.find_last_of("/\\") + 1);
+}
uhd::_log::log::log(
- const verbosity_t verbosity,
+ const uhd::log::severity_level verbosity,
const std::string &file,
const unsigned int line,
- const std::string &function
+ const std::string &component,
+ const boost::thread::id id
)
{
_log_it = (verbosity >= log_rs().level);
+ _log_file =(verbosity >= log_rs().file_level);
+ _log_console = (verbosity >= log_rs().console_level);
if (_log_it)
{
+ if (_log_file){
const std::string time = pt::to_simple_string(pt::microsec_clock::local_time());
- const std::string header1 = str(boost::format("-- %s - level %d") % time % int(verbosity));
- const std::string header2 = str(boost::format("-- %s") % function).substr(0, 80);
- const std::string header3 = str(boost::format("-- %s:%u") % get_rel_file_path(file) % line);
- const std::string border = std::string(std::max(std::max(header1.size(), header2.size()), header3.size()), '-');
- _ss << std::endl
- << border << std::endl
- << header1 << std::endl
- << header2 << std::endl
- << header3 << std::endl
- << border << std::endl
+ _file
+ << time << ","
+ << "0x" << id << ","
+ << path_to_filename(file) << ":" << line << ","
+ << verbosity << ","
+ << component << ","
;
+ }
+#ifndef UHD_LOG_CONSOLE_DISABLE
+ if (_log_console){
+#ifdef UHD_LOG_CONSOLE_TIME
+ const std::string time = pt::to_simple_string(pt::microsec_clock::local_time());
+#endif
+ _console
+#ifdef UHD_LOG_CONSOLE_TIME
+ << "[" << time << "] "
+#endif
+#ifdef UHD_LOG_CONSOLE_THREAD
+ << "[0x" << id << "] "
+#endif
+#ifdef UHD_LOG_CONSOLE_SRC
+ << "[" << path_to_filename(file) << ":" << line << "] "
+#endif
+ << "[" << verbosity << "] "
+ << "[" << component << "] "
+ ;
+ }
+#endif
}
}
@@ -144,22 +198,44 @@ uhd::_log::log::~log(void)
{
if (not _log_it)
return;
-
- _ss << std::endl;
- try{
- log_rs().log_to_file(_ss.str());
- }
- catch(const std::exception &e){
- /*!
- * Critical behavior below.
- * The following steps must happen in order to avoid a lock-up condition.
- * This is because the message facility will call into the logging facility.
- * Therefore we must disable the logger (level = never) before messaging.
- */
- log_rs().level = never;
- UHD_MSG(error)
- << "Logging failed: " << e.what() << std::endl
- << "Logging has been disabled for this process" << std::endl
- ;
+#ifndef UHD_LOG_CONSOLE_DISABLE
+ if ( _log_console ){
+ std::clog << _console.str() << _ss.str() << std::endl;
+ }
+#endif
+ if ( _log_file){
+ _file << _ss.str() << std::endl;
+ try{
+ log_rs().log_to_file(_file.str());
+ }
+ catch(const std::exception &e){
+ /*!
+ * Critical behavior below.
+ * The following steps must happen in order to avoid a lock-up condition.
+ * This is because the message facility will call into the logging facility.
+ * Therefore we must disable the logger (level = never) before messaging.
+ */
+ log_rs().level = uhd::log::off;
+ std::cerr
+ << "Logging failed: " << e.what() << std::endl
+ << "Logging has been disabled for this process" << std::endl
+ ;
+ }
+
}
}
+
+void
+uhd::_log::log::set_log_level(uhd::log::severity_level level){
+ log_rs().level = level;
+}
+
+void
+uhd::_log::log::set_console_level(uhd::log::severity_level level){
+ log_rs().console_level = level;
+}
+
+void
+uhd::_log::log::set_file_level(uhd::log::severity_level level){
+ log_rs().file_level = level;
+}
diff --git a/host/lib/utils/msg.cpp b/host/lib/utils/msg.cpp
deleted file mode 100644
index 9a125515d..000000000
--- a/host/lib/utils/msg.cpp
+++ /dev/null
@@ -1,134 +0,0 @@
-//
-// Copyright 2011 Ettus Research LLC
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see <http://www.gnu.org/licenses/>.
-//
-
-#include <uhd/utils/msg.hpp>
-#include <uhd/utils/log.hpp>
-#include <uhd/utils/static.hpp>
-#include <boost/thread/mutex.hpp>
-#include <boost/tokenizer.hpp>
-#include <sstream>
-#include <iostream>
-
-/***********************************************************************
- * Helper functions
- **********************************************************************/
-#define tokenizer(inp, sep) \
- boost::tokenizer<boost::char_separator<char> > \
- (inp, boost::char_separator<char>(sep))
-
-static void msg_to_cout(const std::string &msg){
- std::stringstream ss;
-
- static bool just_had_a_newline = true;
- for(char ch: msg){
- if (just_had_a_newline){
- just_had_a_newline = false;
- ss << "-- ";
- }
- if (ch == '\n'){
- just_had_a_newline = true;
- }
- ss << ch;
- }
-
- std::cout << ss.str() << std::flush;
-}
-
-static void msg_to_cerr(const std::string &title, const std::string &msg){
- std::stringstream ss;
-
- ss << std::endl << title << ":" << std::endl;
- for(const std::string &line: tokenizer(msg, "\n")){
- ss << " " << line << std::endl;
- }
-
- std::cerr << ss.str() << std::flush;
-}
-
-/***********************************************************************
- * Global resources for the messenger
- **********************************************************************/
-struct msg_resource_type{
- boost::mutex mutex;
- uhd::msg::handler_t handler;
-};
-
-UHD_SINGLETON_FCN(msg_resource_type, msg_rs);
-
-/***********************************************************************
- * Setup the message handlers
- **********************************************************************/
-void uhd::msg::register_handler(const handler_t &handler){
- boost::mutex::scoped_lock lock(msg_rs().mutex);
- msg_rs().handler = handler;
-}
-
-const uhd::msg::handler_t& uhd::msg::get_handler(){
- boost::mutex::scoped_lock lock(msg_rs().mutex);
- return msg_rs().handler;
-}
-
-void uhd::msg::default_msg_handler(uhd::msg::type_t type, const std::string &msg){
- static boost::mutex msg_mutex;
- boost::mutex::scoped_lock lock(msg_mutex);
- switch(type){
- case uhd::msg::fastpath:
- std::cerr << msg << std::flush;
- break;
-
- case uhd::msg::status:
- msg_to_cout(msg);
- UHD_LOG << "Status message" << std::endl << msg;
- break;
-
- case uhd::msg::warning:
- msg_to_cerr("UHD Warning", msg);
- UHD_LOG << "Warning message" << std::endl << msg;
- break;
-
- case uhd::msg::error:
- msg_to_cerr("UHD Error", msg);
- UHD_LOG << "Error message" << std::endl << msg;
- break;
- }
-}
-
-UHD_STATIC_BLOCK(msg_register_default_handler){
- uhd::msg::register_handler(&uhd::msg::default_msg_handler);
-}
-
-/***********************************************************************
- * The message object implementation
- **********************************************************************/
-struct uhd::msg::_msg::impl{
- std::ostringstream ss;
- type_t type;
-};
-
-uhd::msg::_msg::_msg(const type_t type){
- _impl = UHD_PIMPL_MAKE(impl, ());
- _impl->type = type;
-}
-
-uhd::msg::_msg::~_msg(void){
- boost::mutex::scoped_lock lock(msg_rs().mutex);
- msg_rs().handler(_impl->type, _impl->ss.str());
-}
-
-std::ostream & uhd::msg::_msg::operator()(void){
- return _impl->ss;
-}
diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp
index 1e8f50736..4cc28e48b 100644
--- a/host/lib/utils/tasks.cpp
+++ b/host/lib/utils/tasks.cpp
@@ -17,7 +17,7 @@
#include <uhd/utils/tasks.hpp>
#include <uhd/utils/msg_task.hpp>
-#include <uhd/utils/msg.hpp>
+#include <uhd/utils/log.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <exception>
@@ -67,10 +67,10 @@ private:
}
void do_error_msg(const std::string &msg){
- UHD_MSG(error)
- << "An unexpected exception was caught in a task loop." << std::endl
- << "The task loop will now exit, things may not work." << std::endl
- << msg << std::endl
+ UHD_LOGGER_ERROR("UHD")
+ << "An unexpected exception was caught in a task loop."
+ << "The task loop will now exit, things may not work."
+ << msg
;
}
@@ -162,10 +162,10 @@ private:
}
void do_error_msg(const std::string &msg){
- UHD_MSG(error)
- << "An unexpected exception was caught in a task loop." << std::endl
- << "The task loop will now exit, things may not work." << std::endl
- << msg << std::endl
+ UHD_LOGGER_ERROR("UHD")
+ << "An unexpected exception was caught in a task loop."
+ << "The task loop will now exit, things may not work."
+ << msg
;
}
diff --git a/host/lib/utils/thread_priority.cpp b/host/lib/utils/thread_priority.cpp
index 98023c5aa..084d023bf 100644
--- a/host/lib/utils/thread_priority.cpp
+++ b/host/lib/utils/thread_priority.cpp
@@ -16,7 +16,7 @@
//
#include <uhd/utils/thread_priority.hpp>
-#include <uhd/utils/msg.hpp>
+#include <uhd/utils/log.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <iostream>
@@ -26,7 +26,7 @@ bool uhd::set_thread_priority_safe(float priority, bool realtime){
set_thread_priority(priority, realtime);
return true;
}catch(const std::exception &e){
- UHD_MSG(warning) << boost::format(
+ UHD_LOGGER_WARNING("UHD") << boost::format(
"Unable to set the thread priority. Performance may be negatively affected.\n"
"Please see the general application notes in the manual for instructions.\n"
"%s\n"
diff --git a/host/lib/utils/thread_priority_c.cpp b/host/lib/utils/thread_priority_c.cpp
index fe019e51d..b2de9970d 100644
--- a/host/lib/utils/thread_priority_c.cpp
+++ b/host/lib/utils/thread_priority_c.cpp
@@ -18,7 +18,7 @@
#include <uhd/error.h>
#include <uhd/utils/thread_priority.h>
#include <uhd/utils/thread_priority.hpp>
-#include <uhd/utils/msg.hpp>
+#include <uhd/utils/log.hpp>
#include <uhd/exception.hpp>
#include <boost/format.hpp>
#include <iostream>