2011-05-04 02:28:13 +00:00
|
|
|
//
|
2016-03-11 15:14:12 +00:00
|
|
|
// Copyright 2012,2014,2016 Ettus Research LLC
|
2011-05-04 02:28:13 +00:00
|
|
|
//
|
|
|
|
|
// 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/>.
|
|
|
|
|
//
|
|
|
|
|
|
2011-05-04 19:18:11 +00:00
|
|
|
#include <uhd/utils/log.hpp>
|
2011-05-04 02:28:13 +00:00
|
|
|
#include <uhd/utils/static.hpp>
|
2012-05-15 18:11:35 +00:00
|
|
|
#include <uhd/utils/paths.hpp>
|
2011-05-04 02:28:13 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2014-09-30 16:35:57 +00:00
|
|
|
#include <boost/thread/locks.hpp>
|
2011-05-05 00:01:41 +00:00
|
|
|
#include <boost/interprocess/sync/file_lock.hpp>
|
2011-05-04 02:28:13 +00:00
|
|
|
#include <fstream>
|
2011-05-04 18:49:02 +00:00
|
|
|
#include <cctype>
|
2011-05-04 02:28:13 +00:00
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
namespace pt = boost::posix_time;
|
2011-05-05 00:01:41 +00:00
|
|
|
namespace ip = boost::interprocess;
|
2011-05-04 02:28:13 +00:00
|
|
|
|
|
|
|
|
/***********************************************************************
|
2011-05-13 19:41:35 +00:00
|
|
|
* Global resources for the logger
|
2011-05-04 02:28:13 +00:00
|
|
|
**********************************************************************/
|
2011-05-13 19:41:35 +00:00
|
|
|
class log_resource_type{
|
2011-05-04 02:28:13 +00:00
|
|
|
public:
|
2017-02-08 00:37:25 +00:00
|
|
|
uhd::log::severity_level level;
|
|
|
|
|
uhd::log::severity_level file_level;
|
|
|
|
|
uhd::log::severity_level console_level;
|
2011-05-04 18:49:02 +00:00
|
|
|
|
2017-02-22 23:02:25 +00:00
|
|
|
log_resource_type(void): level(uhd::log::info), file_level(uhd::log::info), console_level(uhd::log::info){
|
2011-05-13 19:48:36 +00:00
|
|
|
|
|
|
|
|
//file lock pointer must be null
|
|
|
|
|
_file_lock = NULL;
|
|
|
|
|
|
2017-02-08 00:37:25 +00:00
|
|
|
//default to no file logging
|
|
|
|
|
this->file_logging = false;
|
|
|
|
|
|
2011-05-04 18:49:02 +00:00
|
|
|
//set the default log level
|
2017-02-08 00:37:25 +00:00
|
|
|
this->level = uhd::log::off;
|
|
|
|
|
this->file_level = uhd::log::off;
|
|
|
|
|
this->console_level = uhd::log::off;
|
2011-05-04 18:49:02 +00:00
|
|
|
|
|
|
|
|
//allow override from macro definition
|
2017-02-08 00:37:25 +00:00
|
|
|
#ifdef UHD_LOG_MIN_LEVEL
|
2017-02-22 23:02:25 +00:00
|
|
|
this->level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_MIN_LEVEL), this->level);
|
2017-02-08 00:37:25 +00:00
|
|
|
#endif
|
|
|
|
|
#if defined(UHD_LOG_FILE_LEVEL) && defined(UHD_LOG_FILE_PATH)
|
2017-02-22 23:02:25 +00:00
|
|
|
this->file_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_FILE_LEVEL), this->file_level);
|
2017-02-08 00:37:25 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef UHD_LOG_CONSOLE_LEVEL
|
2017-02-22 23:02:25 +00:00
|
|
|
this->console_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_CONSOLE_LEVEL), this->console_level);
|
2017-02-08 00:37:25 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef UHD_LOG_FILE
|
|
|
|
|
this->log_file_target = BOOST_STRINGIZE(UHD_LOG_FILE);
|
|
|
|
|
this->file_logging = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//allow override from environment variables
|
2011-05-04 18:49:02 +00:00
|
|
|
const char * log_level_env = std::getenv("UHD_LOG_LEVEL");
|
2017-02-22 23:02:25 +00:00
|
|
|
if (log_level_env != NULL && log_level_env[0] != '\0') this->level = _get_log_level(log_level_env, this->level);
|
2017-02-08 00:37:25 +00:00
|
|
|
|
|
|
|
|
const char * log_file_level_env = std::getenv("UHD_LOG_FILE_LEVEL");
|
2017-02-22 23:02:25 +00:00
|
|
|
if (log_file_level_env != NULL && log_file_level_env[0] != '\0') this->file_level = _get_log_level(log_file_level_env, this->file_level);
|
2017-02-08 00:37:25 +00:00
|
|
|
|
|
|
|
|
const char * log_console_level_env = std::getenv("UHD_LOG_CONSOLE_LEVEL");
|
2017-02-22 23:02:25 +00:00
|
|
|
if (log_console_level_env != NULL && log_console_level_env[0] != '\0') this->console_level = _get_log_level(log_console_level_env, this->console_level);
|
2017-02-08 00:37:25 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2011-05-04 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-13 19:41:35 +00:00
|
|
|
~log_resource_type(void){
|
2017-02-08 00:37:25 +00:00
|
|
|
if (this->file_logging){
|
|
|
|
|
boost::lock_guard<boost::mutex> lock(_mutex);
|
|
|
|
|
_file_stream.close();
|
|
|
|
|
if (_file_lock != NULL) delete _file_lock;
|
|
|
|
|
}
|
2011-05-04 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-01 20:57:25 +00:00
|
|
|
void log_to_file(const std::string &log_msg){
|
2017-02-08 00:37:25 +00:00
|
|
|
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();
|
2011-05-05 00:01:41 +00:00
|
|
|
}
|
2011-05-04 18:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-04 02:28:13 +00:00
|
|
|
private:
|
2011-05-04 18:49:02 +00:00
|
|
|
//! set the log level from a string that is either a digit or an enum name
|
2017-02-08 00:37:25 +00:00
|
|
|
bool file_logging;
|
|
|
|
|
std::string log_file_target;
|
2017-02-22 23:02:25 +00:00
|
|
|
uhd::log::severity_level _get_log_level(const std::string &log_level_str,
|
|
|
|
|
const uhd::log::severity_level &previous_level){
|
2017-02-08 00:37:25 +00:00
|
|
|
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;
|
2017-02-22 23:02:25 +00:00
|
|
|
}else{
|
|
|
|
|
return previous_level;
|
2017-02-08 00:37:25 +00:00
|
|
|
}
|
2011-05-04 18:49:02 +00:00
|
|
|
}
|
2017-02-08 00:37:25 +00:00
|
|
|
|
|
|
|
|
#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);
|
2017-02-22 23:02:25 +00:00
|
|
|
if_loglevel_equal(off);
|
|
|
|
|
return previous_level;
|
2011-05-04 18:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-13 19:41:35 +00:00
|
|
|
//file stream and lock:
|
2011-05-04 18:49:02 +00:00
|
|
|
std::ofstream _file_stream;
|
2011-05-13 19:41:35 +00:00
|
|
|
ip::file_lock *_file_lock;
|
2011-06-01 20:57:25 +00:00
|
|
|
boost::mutex _mutex;
|
2011-05-04 02:28:13 +00:00
|
|
|
};
|
|
|
|
|
|
2011-05-13 19:41:35 +00:00
|
|
|
UHD_SINGLETON_FCN(log_resource_type, log_rs);
|
2011-05-04 02:28:13 +00:00
|
|
|
|
|
|
|
|
/***********************************************************************
|
2011-05-05 01:36:10 +00:00
|
|
|
* The logger object implementation
|
2011-05-04 02:28:13 +00:00
|
|
|
**********************************************************************/
|
2011-05-04 21:33:35 +00:00
|
|
|
//! get the relative file path from the host directory
|
|
|
|
|
|
2017-02-08 00:37:25 +00:00
|
|
|
inline std::string path_to_filename(std::string path)
|
|
|
|
|
{
|
|
|
|
|
return path.substr(path.find_last_of("/\\") + 1);
|
|
|
|
|
}
|
2011-06-01 20:57:25 +00:00
|
|
|
|
2011-05-04 02:28:13 +00:00
|
|
|
uhd::_log::log::log(
|
2017-02-08 00:37:25 +00:00
|
|
|
const uhd::log::severity_level verbosity,
|
2011-05-04 02:28:13 +00:00
|
|
|
const std::string &file,
|
|
|
|
|
const unsigned int line,
|
2017-02-08 00:37:25 +00:00
|
|
|
const std::string &component,
|
|
|
|
|
const boost::thread::id id
|
2014-09-20 01:22:13 +00:00
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
_log_it = (verbosity >= log_rs().level);
|
2017-02-08 00:37:25 +00:00
|
|
|
_log_file =(verbosity >= log_rs().file_level);
|
|
|
|
|
_log_console = (verbosity >= log_rs().console_level);
|
2014-09-20 01:22:13 +00:00
|
|
|
if (_log_it)
|
|
|
|
|
{
|
2017-02-08 00:37:25 +00:00
|
|
|
if (_log_file){
|
2014-09-20 01:22:13 +00:00
|
|
|
const std::string time = pt::to_simple_string(pt::microsec_clock::local_time());
|
2017-02-08 00:37:25 +00:00
|
|
|
_file
|
|
|
|
|
<< time << ","
|
|
|
|
|
<< "0x" << id << ","
|
|
|
|
|
<< path_to_filename(file) << ":" << line << ","
|
|
|
|
|
<< verbosity << ","
|
|
|
|
|
<< component << ","
|
2014-09-20 01:22:13 +00:00
|
|
|
;
|
2017-02-08 00:37:25 +00:00
|
|
|
}
|
|
|
|
|
#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
|
2014-09-20 01:22:13 +00:00
|
|
|
}
|
2011-05-04 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
2014-09-20 01:22:13 +00:00
|
|
|
uhd::_log::log::~log(void)
|
|
|
|
|
{
|
|
|
|
|
if (not _log_it)
|
|
|
|
|
return;
|
2017-02-08 00:37:25 +00:00
|
|
|
#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
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-13 19:41:35 +00:00
|
|
|
}
|
2011-05-04 02:28:13 +00:00
|
|
|
}
|
2017-02-08 00:37:25 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|