nik*_*sfi 4 c++ logging boost boost-log
我有一个广泛使用boost log 2.0的应用程序.现在,我想设置一些默认的标志为应用程序一样std::setprecision(std::numeric_limits<double>::digits10 + 1),std::scientific和std::left.但是我该怎么做?一种方法是在我的main函数的最开始创建一个记录器并创建一个虚拟日志消息.这将永久设置所需的标志.但有没有更好的方法来做到这一点?
编辑回复:"OP应显示实际代码."
我有一个全局的Logging单例,叫做L:
class L{
public:
enum severity_level
{
dddebug,
ddebug,
debug,
control,
iiinfo,
iinfo,
info,
result,
warning,
error,
critical
};
typedef boost::log::sources::severity_channel_logger<
severity_level, // the type of the severity level
std::string // the type of the channel name
> logger_t;
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink_;
static L& get();
static boost::shared_ptr<text_sink> sink();
static double t0();
static double tElapsed();
private:
L();
double t0_p;
static std::string tElapsedFormat();
L(const L&) = delete;
void operator=(const L&) = delete;
};
Run Code Online (Sandbox Code Playgroud)
它提供了日志记录接收器,严重性级别,并利用MPI方法在MPI节点之间进行同步计时.类成员的实现如下:
#include "log.h"
#include <iomanip>
#include <limits>
#include <fstream>
#include <boost/log/attributes/function.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
#include "mpiwrap.h"
#include <mpi.h>
BOOST_LOG_ATTRIBUTE_KEYWORD(t, "Time", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(rank, "Rank", int)
BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", L::severity_level)
L::L():
sink_(boost::make_shared< text_sink >()),
t0_p(MPI_Wtime())
{
sink_->locked_backend()->add_stream(
boost::make_shared< std::ofstream >("log." + std::to_string(MpiWrap::getRank())));
sink_->set_formatter
(
expr::stream
<< "< "
<< t << " "
<< "[p:" << rank << "] "
<< "[c:" << channel << "] "
<< "[s:" << severity << "] "
<< expr::smessage
);
logging::core::get()->add_sink(sink_);
logging::core::get()->set_filter(
(channel == "ChannelName1" && severity >= dddebug)
|| (channel == "ChannelName2" && severity >= info)
|| (channel == "ChannelName3" && severity >= result)
);
// Add attributes
logging::core::get()->add_global_attribute("Time", attrs::make_function(&tElapsedFormat));
logging::core::get()->add_global_attribute("Rank", attrs::constant<int>(MpiWrap::getRank()));
}
L& L::get(){
static L instance;
return instance;
}
boost::shared_ptr<L::text_sink> L::sink(){
return get().sink_;
}
double L::t0(){
return get().t0_p;
}
double L::tElapsed(){
return MPI_Wtime() - t0();
}
std::string L::tElapsedFormat(){
std::stringstream ss;
const int prec = std::numeric_limits<double>::digits10;
ss << std::setw(prec + 2 + 6) << std::left << std::setprecision(prec) << tElapsed();
return ss.str();
}
std::ostream& operator<< (std::ostream& strm, L::severity_level level)
{
static const char* strings[] =
{
"DBG3",
"DBG2",
"DBG1",
"CTRL",
"INF3",
"INF2",
"INF1",
"RSLT",
"WARN",
"ERRR",
"CRIT"
};
if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
strm << strings[level];
else
strm << static_cast< int >(level);
return strm;
}
Run Code Online (Sandbox Code Playgroud)
现在用于:我的类通常有一个静态logger_t(typedef for boost::log::sources::severity_channel_logger<severity_level, std::string>)成员
class A {
public:
logger_t logger;
//other stuff here
void function_which_does_logging();
};
L::logger_t A::logger(boost::log::keywords::channel = "ClassA");
void A::function_which_does_logging(){
//do non logging related stuff
BOOST_LOG_SEV(logger, L::result) << "the error is: " << 0.1234567890;
//do non logging related stuff
}
Run Code Online (Sandbox Code Playgroud)
我目前解决问题的方法是在我的程序开头添加一个日志语句
int main(){
L::logger_t logger(boost::log::keywords::channel = "init");
BOOST_LOG_SEV(logger, L::critical) << "setting up logger" << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
@rhashimoto对当前解决方案如何通过多线程/并发日志记录操作进行分解提出了一个很好的观点.我觉得最好的解决方案是定义自己的日志宏来替换BOOST_LOG_SEV包含流修饰符的日志宏,如下所示:
#define LOG_SCIENTIFIC(logger, sev) (BOOST_LOG_SEV(logger, sev) << std::scientific)
Run Code Online (Sandbox Code Playgroud)
这可以仅用作替代品,BOOST_LOG_SEV其格式数字为科学.但是,通过代码并使用新的自定义宏替换每个日志记录操作可能会很痛苦.您也可以重新定义BOOST_LOG_SEV行为,而不是定义自己的宏.boost/log/sources/severity_feature.hpp定义BOOST_LOG_SEV如下:
//! An equivalent to BOOST_LOG_STREAM_SEV(logger, lvl)
#define BOOST_LOG_SEV(logger, lvl) BOOST_LOG_STREAM_SEV(logger, lvl)
Run Code Online (Sandbox Code Playgroud)
因为BOOST_LOG_STREAM_SEV仍然是公共提升API的一部分,您应该能够安全地重新定义,BOOST_LOG_SEV如下所示:
#define BOOST_LOG_SEV(logger, lvl) (BOOST_LOG_STREAM_SEV(logger, lvl) << std::scientific)
Run Code Online (Sandbox Code Playgroud)
只要在包含boost日志标题后定义了它,它就应该按照您的意愿执行.但是,我建议使用带有自定义名称的宏,以便其他人清楚您的代码正在做什么.