zoukankan      html  css  js  c++  java
  • Boost Log

    boost log支持以下配置宏,只列出一些常用的,如下表所示:

    Macro nameEffect
    BOOST_LOG_DYN_LINK If defined in user code, the library will assume the binary is built as a dynamically loaded library (“dll” or “so”). Otherwise it is assumed that the library is built in static mode. This macro must be either defined or not defined for all translation units of user application that uses logging. This macro can help with auto-linking on platforms that support it.
    BOOST_ALL_DYN_LINK Same as BOOST_LOG_DYN_LINK but also affects other Boost libraries the same way.
    BOOST_USE_WINAPI_VERSION Affects compilation of both the library and user’s code. This macro is Windows-specific. Selects the target Windows version for various Boost libraries, including Boost.Log. Code compiled for a particular Windows version will likely fail to run on the older Windows versions, but may improve performance because of using newer OS features. The macro is expected to have an integer value equivalent to _WIN32_WINNT.
    BOOST_LOG_NO_THREADS If defined, disables multithreading support. Affects the compilation of both the library and users’ code. The macro is automatically defined if no threading support is detected.

    有一点要注意:如果你的程序工程中由多个模块构成(例如,由一个.exe和多个.dll构成),当你使用boost log
    库时必须built as a shared object。如果只是单个模块(例如:单个.exe或单个.dll)则可以build the library as a static library.

    boost log涉及的重点概念或术语定义解释

    日志记录:一个独立的消息包,这个消息包还不是实际写到日志里的消息,它只是一个候选的消息。
    属性 : 日志记录中的一个消息片。
    属性值:那就是上面所说的属性的值了,可以是各种数据类型。
    日志槽(LOG SINK):日志写向的目标,它要定义日志被写向什么地方,以及如何写。
    日志源:应用程序写日志时的入口,其实质是一个logger对象的实例。
    日志过滤器:决定日志记录是否要被记录的一组判断。
    日志格式化:决定日志记录输出的实际格式。
    日志核心:维护者日志源、日志槽、日志过滤器等之间的关系的一个全局中的实体。主要在初始化logging library时用到。

    最简单例子:

    #include <boost/log/trivial.hpp>
    
    int main(int, char*[])
    {
        BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
        BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
        BOOST_LOG_TRIVIAL(info) << "An informational severity message";
        BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
        BOOST_LOG_TRIVIAL(error) << "An error severity message";
        BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
    
        return 0;
    }

    添加过滤:

    #include <boost/log/core.hpp>
    #include <boost/log/trivial.hpp>
    #include <boost/log/expressions.hpp>
    
    namespace logging = boost::log;
    
    void init()
    {
        logging::core::get()->set_filter
        (
            logging::trivial::severity >= logging::trivial::info
        );
    }
    
    int main(int, char*[])
    {
        init();
    
        BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
        BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
        BOOST_LOG_TRIVIAL(info) << "An informational severity message";
        BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
        BOOST_LOG_TRIVIAL(error) << "An error severity message";
        BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
    
        return 0;
    }

     输出到文件:

    #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>
    
    namespace logging = boost::log;
    namespace src = boost::log::sources;
    namespace sinks = boost::log::sinks;
    namespace keywords = boost::log::keywords;
    
    #if 0
    
    //[ example_tutorial_file_simple
    void init()
    {
        logging::add_file_log("sample.log");
    
        logging::core::get()->set_filter
        (
            logging::trivial::severity >= logging::trivial::info
        );
    }
    //]
    
    // We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416
    //[ example_tutorial_file_advanced_no_callouts
    void init()
    {
        logging::add_file_log
        (
            keywords::file_name = "sample_%N.log",
            keywords::rotation_size = 10 * 1024 * 1024,
            keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
            keywords::format = "[%TimeStamp%]: %Message%"
        );
    
        logging::core::get()->set_filter
        (
            logging::trivial::severity >= logging::trivial::info
        );
    }
    //]
    
    #else
    
    //[ example_tutorial_file_advanced
    void init()
    {
        logging::add_file_log
        (
            keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
            keywords::rotation_size = 10 * 1024 * 1024,                                   /*< rotate files every 10 MiB... >*/
            keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
            keywords::format = "[%TimeStamp%]: %Message%"                                 /*< log record format >*/
        );
    
        logging::core::get()->set_filter
        (
            logging::trivial::severity >= logging::trivial::info
        );
    }
    //]
    
    #endif
    
    int main(int, char*[])
    {
        init();
        logging::add_common_attributes();
    
        using namespace logging::trivial;
        src::severity_logger< severity_level > lg;
    
        BOOST_LOG_SEV(lg, trace) << "A trace severity message";
        BOOST_LOG_SEV(lg, debug) << "A debug severity message";
        BOOST_LOG_SEV(lg, info) << "An informational severity message";
        BOOST_LOG_SEV(lg, warning) << "A warning severity message";
        BOOST_LOG_SEV(lg, error) << "An error severity message";
        BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
    
        return 0;
    }
  • 相关阅读:
    Spring之循环依赖与解决方案
    ipv4+ipv6网络中的DDNS
    NAT、PAT、DMZ、端口映射、端口转发、UPNP
    如何实现内外网或多网络环境下上网?路由route
    局域网学习MAC地址?ping+arp
    网络故障排查?ping和trace*
    有了MAC地址,为什么还要用IP地址?
    Nginx原理解析
    磁盘io
    last总结
  • 原文地址:https://www.cnblogs.com/kohlrabi/p/9131487.html
Copyright © 2011-2022 走看看