How to change the default formatting with boost::log::BOOST_TRIVIAL_LOG?

痴心易碎 提交于 2020-01-14 07:27:07

问题


boost::log looks really powerful. It offers a BOOST_LOG_TRIVIAL macro for trivial logging. But how can I change the default formatting? It prints the timestamp by default, by I don't want it. Do you have any idea? It seems the only way is to define a new sink ex-novo and add it to the core, then you can call set_format() on the backend in case. But this is no "trivial" anymore.


回答1:


Boost.Log has a default sink, which is used as long as you do not provide your own sink. The following code snippet changes the format of the console-log by adding a new sink.

#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/console.hpp>

int main()
{
    boost::log::add_console_log(std::cout, boost::log::keywords::format = ">> %Message%");
    BOOST_LOG_TRIVIAL(info) << "Hello world!";
}

Note that you have to add the log_setup library to your build i.e. do a

-lboost_log_setup -lboost_log

where the order of the libs is important.



来源:https://stackoverflow.com/questions/10972818/how-to-change-the-default-formatting-with-boostlogboost-trivial-log

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!