【问题标题】:Boost.Log: Support file name and line numberBoost.Log:支持文件名和行号
【发布时间】:2015-09-18 04:53:16
【问题描述】:

我正试图让我的团队离开 log4cxx 并尝试改用 Boost.Log v2。我们现在的log4cxx pattern 比较简单:

log4cxx::helpers::Properties prop;
prop.setProperty("log4j.rootLogger","DEBUG, A1");
prop.setProperty("log4j.appender.A1","org.apache.log4j.ConsoleAppender");
prop.setProperty("log4j.appender.A1.layout","org.apache.log4j.PatternLayout");
prop.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{ABSOLUTE} %-5p [%c] %m%n");
log4cxx::PropertyConfigurator::configure(prop);

但是我找不到支持文件名和行号打印的解决方案。到目前为止,我找到了一个old post,但没有明确的解决方案(没有接受的解决方案)。我已经研究过使用那些BOOST_LOG_NAMED_SCOPE,但它们感觉非常难看,因为当在同一个函数中使用多个这些时,无法打印正确的行号。

我还找到了一个更简单的直接解决方案,here。但这也让人觉得难看,因为它会打印完整路径,而不仅仅是基本名称(它是不可配置的)。所以全路径和行号总是打印在一个固定的位置(expr::smessage之前)。

我还发现了这个post,它看起来像是一个旧的解决方案。

最后,我发现最有希望的解决方案是here。但是它甚至不为我编译。

所以我的问题很简单:如何使用 Boost.Log v2 打印文件名(不是完整路径)和行号最少 formatter flexibility(没有解决方案 MACRO__FILE__ / __LINE__ 接受,请)。我很欣赏不涉及BOOST_LOG_NAMED_SCOPE 的解决方案,如here 所述:

如果您想查看特定日志记录的行号,那么 最好的方法是定义一个自定义宏,您将使用它来编写 日志。在该宏中,您可以将文件名和行号添加为 记录的属性(您可以为此使用操纵器)。笔记 在这种情况下,您将无法在 过滤器,但您可能不需要它。

【问题讨论】:

    标签: c++ boost log4cxx boost-log


    【解决方案1】:

    我终于找到了一个基于add_value的简单解决方案。这是完整的源代码:

    #include <ostream>
    #include <fstream>
    #include <boost/log/core.hpp>
    #include <boost/log/trivial.hpp>
    #include <boost/log/sinks/sync_frontend.hpp>
    #include <boost/log/sinks/text_ostream_backend.hpp>
    #include <boost/log/utility/setup/common_attributes.hpp>
    #include <boost/log/utility/manipulators/add_value.hpp>
    #include <boost/filesystem.hpp>
    
    namespace logging = boost::log;
    namespace src = boost::log::sources;
    namespace expr = boost::log::expressions;
    namespace sinks = boost::log::sinks;
    
    void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
    {
        // Get the LineID attribute value and put it into the stream
        strm << logging::extract< unsigned int >("LineID", rec) << ": ";
        strm << logging::extract< int >("Line", rec) << ": ";
        logging::value_ref< std::string > fullpath = logging::extract< std::string >("File", rec);
        strm << boost::filesystem::path(fullpath.get()).filename().string() << ": ";
    
        // The same for the severity level.
        // The simplified syntax is possible if attribute keywords are used.
        strm << "<" << rec[logging::trivial::severity] << "> ";
    
        // Finally, put the record message to the stream
        strm << rec[expr::smessage];
    }
    
    void init()
    {
        typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
        boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
    
        sink->locked_backend()->add_stream(
            boost::make_shared< std::ofstream >("sample.log"));
    
        sink->set_formatter(&my_formatter);
    
        logging::core::get()->add_sink(sink);
    }
    
    #define MY_GLOBAL_LOGGER(log_,sv) BOOST_LOG_SEV( log_, sv) \
      << boost::log::add_value("Line", __LINE__)      \
      << boost::log::add_value("File", __FILE__)       \
      << boost::log::add_value("Function", BOOST_CURRENT_FUNCTION)
    
    int main(int, char*[])
    {
        init();
        logging::add_common_attributes();
    
        using namespace logging::trivial;
        src::severity_logger< severity_level > lg;
    
        MY_GLOBAL_LOGGER(lg,debug) << "Keep";
        MY_GLOBAL_LOGGER(lg,info) << "It";
        MY_GLOBAL_LOGGER(lg,warning) << "Simple";
        MY_GLOBAL_LOGGER(lg,error) << "Stupid";
    
        return 0;
    }
    

    在我的 Linux 机器上,我使用以下代码编译它:

    $ c++ -otutorial_fmt_custom -DBOOST_LOG_DYN_LINK tutorial_fmt_custom.cpp -lboost_log -lboost_log_setup -lboost_thread -lpthread -lboost_filesystem
    

    如果你运行并检查这里生成的日志文件就是你得到的:

    $ ./tutorial_fmt_custom && cat sample.log 
    1: 61: tutorial_fmt_custom.cpp: <debug> Keep
    2: 62: tutorial_fmt_custom.cpp: <info> It
    3: 63: tutorial_fmt_custom.cpp: <warning> Simple
    4: 64: tutorial_fmt_custom.cpp: <error> Stupid
    

    我的解决方案基于两个输入(谢谢!):

    【讨论】:

    • 请记住,只要您使用文件名编译项目,您的解决方案就会输出文件名。尝试使用../test/tutorial_fmt_custom.cpp 进行编译,那么您将在日志文件中得到相同的结果,而不仅仅是文件名。
    • 我正在尝试您建议的使用 add_value 的方法,这看起来比我见过的其他方法要好得多。但我无法让它工作。我认为您遗漏了属性和关键字的声明+定义。你能在你的答案中提供它们吗?此外,第二个链接的页面不是英文的。你能指点我的英文翻译吗? (我试过谷歌翻译,但没有用。)谢谢!
    • 我需要的编译 -lboost_system
    【解决方案2】:

    为了输出文件名和行,您需要注册Scopes

    logging::core::get()->add_global_attribute("Scopes", attributes::named_scope());
    

    并添加自定义格式化程序:

    void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
    {
        const auto cont = logging::extract< attributes::named_scope::value_type >("Scopes", rec);
        if(cont.empty())
            return;
    
        auto it = cont->begin();
        boost::filesystem::path path(it->file_name.c_str());
        strm << path.filename().string() << ":" << it->line << "\t" << rec[expr::smessage];
    }
    

    注意:为了使其正常工作,需要在每次记录之前清除范围容器: if(!attributes::named_scope::get_scopes().empty()) attributes::named_scope::pop_scope();

    【讨论】:

    • 您能否添加一个关于如何使用它的示例,以便它打印正确的行号(BOOST_LOG_SET 所在的位置,而不是BOOST_LOG_NAMED_SCOPE 所在的行)。谢谢。
    • @malat 注意cont-&gt;begin() 那里。您需要在每次记录之前清除容器,并且您可能总是首先访问相同的条目。查看更新的答案。
    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 2010-10-24
    • 2019-01-29
    相关资源
    最近更新 更多