boost log 打印源代码文件名和行号

2021-12-24 00:00:00 c++ boost boost-log boost-logging

我在我的 C++ 应用程序中使用 Boost(1.55.0) 登录.我已经能够生成这种格式的日志

I'm using Boost(1.55.0) Logging in my C++ application. I have been able to generate log of this format

[2014-Jul-15 10:47:26.137959]: <debug>  A regular message

我希望能够在其中添加源文件名和行号日志生成.

I want to be able to add source file name and line number where the log is generated.

[2014-Jul-15 10:47:26.137959]: <debug> [filename:line_no] A regular message

示例:

[2014-Jul-15 10:47:26.137959]: <debug> [helloworld.cpp : 12] A regular message

源代码:

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/attributes/attribute.hpp>
#include <boost/log/attributes/attribute_cast.hpp>
#include <boost/log/attributes/attribute_value.hpp>
#include <boost/make_shared.hpp>
#include <boost/property_tree/ptree.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

void init()
{
    logging::add_file_log
    (
        keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
        keywords::rotation_size = 10*1024*1204,                                 /*< rotate files every 10 MiB... >*/
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
        keywords::format =
        (
            boost::log::expressions::stream
                << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
                << ": <" << boost::log::trivial::severity << "> "
                << boost::log::expressions::smessage
        )
    );
}

int main(int, char*[])
{
    init();
    logging::add_common_attributes();

    using namespace logging::trivial;
    src::severity_logger< severity_level > lg;

    BOOST_LOG_SEV(lg, debug) << "A regular message";
    return 0;
}

推荐答案

我建议使用 boost::log::add_value() 函数.

定义:

#define LOG_LOCATION(LEVEL, MSG)      
  BOOST_LOG_SEV(logger::get(), LEVEL)     
    << boost::log::add_value("Line", __LINE__)          
    << boost::log::add_value("File", __FILE__)          
    << boost::log::add_value("Function", __FUNCTION__) << MSG

然后您可以将其格式化如下:

And then you can format it as follows:

boost::log::add_common_attributes();

boost::log::register_simple_filter_factory<boost::log::trivial::severity_level, char>("Severity");
boost::log::register_simple_formatter_factory<boost::log::trivial::severity_level, char>("Severity");

auto syslog_format(
    boost::log::expressions::stream <<
        "["   << boost::log::expressions::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S") <<
        "] [" << boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>("ThreadID") << 
        "] [" << std::left << std::setw(7) << std::setfill(' ') << boost::log::trivial::severity <<
        "] "  << boost::log::expressions::smessage <<
        " ("  << boost::log::expressions::attr<std::string>("Filename") <<
        ":"   << boost::log::expressions::attr<int>("Line") <<
        ":"   << boost::log::expressions::attr<std::string>("Function") <<
        ")"
);

boost::log::add_file_log(
    boost::log::keywords::file_name  = "sys_%d_%m_%Y.%N.log",
    boost::log::keywords::format = syslog_format
);

无需添加全局属性,您可以轻松地将其格式化,如上所示.我发现这是其他人的解决方案和原始 __FILE__ __LINE__ 方法之间的一个很好的折衷.

No need to add global attributes, and you can format it easily as seen above. I find this is a good compromise between other's solutions and the raw __FILE__ __LINE__ approach.

完整示例此处.

相关文章