Boost Log

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

Macro name Effect
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;
}

原文地址:https://www.cnblogs.com/kohlrabi/p/9131487.html

时间: 2024-08-29 07:01:10

Boost Log的相关文章

boost.log要点笔记

常用简写: 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; 要点: 结构图要牢记在

boost.log在项目中应用

#pragma once #include <string> #include <boost/log/trivial.hpp> using std::string; #define LOG_DEBUG\ BOOST_LOG_SEV((MyLog::s_slg),(boost::log::trivial::debug)) #define LOG_INFO\ BOOST_LOG_SEV((MyLog::s_slg),(boost::log::trivial::info)) #defin

Boost Log 基本用法

Boost Log 基本用法 flyfish 2014-11-5 根据boost提供的代码示例,学习Boost Log 的基本用法 前提 boost版本boost_1_56_0 示例代码文件夹 boost_1_56_0\libs\log\example\basic_usage 使用的单词很形象,整个过程就像流水一样 假设要输出的日志比作水 水                     (Hello, World!) 水槽                 (sink) 流向哪里        (co

boost.log 的使用

在编译boost.log 的示例程序时,遇到了下面的错误: zhifan$ make boost_log LDFLAGS='-lboost_log-mt' Undefined symbols for architecture x86_64: "boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)", re

Boost log 简单使用

Boost log 简单使用 flyfish 2014-11-8 该示例是在VC2010 MFC Unicode环境下使用 内容包括 1 启动关闭日志 2 设置日志存储路径 3 设置输出日志等级 4 日志是否立即写入文件 5 设置单个文件的大小 6 设置磁盘最小可利用空间 Logger.h #pragma once #include <cassert> #include <iostream> #include <fstream> #include <boost/l

boost.log(九) 配置文件

前面几节中描述了Boost.Log 的基础知识,对Boost.Log 库的操作我们都是在C++代码中进行中,这样就会有一些不便的地方.比如说我们想要更改一下输出格式或者过滤条件,都必须对C++代码进行更改,并且还得编译一次(感觉编译时间还有点长).其实Boost.Log 里面已经为这个问题提供了一种解决方案,就是通过配置文件来初始化Boost.Log 库,C++这边的代码也比较简单,就是这样的: std::ifstream settings("settings.txt"); if (!

boost.log(八)宽字符记录

宽字符记录 Boost.Log支持包含本地字符集字符串的日志记录.基本上有两种方式做这件事.在 UNIX 系统上通常使用一些多字节字符编码 (例如 UTF-8) 用来表示本地字符.在这种情况下,Boost.Log库可以直接以纯 ASCII 的方式记录而不需要其它额外的设置. 在Windows 上常见的做法是使用宽字符串来表示本地字符串.此外大多数系统 API 也是使用的宽字符,这需要特定于 Windows 的接收器也支持宽字符.另一方面,通用的接收器,例如 TextFile,是面向字节的,你写入

boost.log(七)再谈过滤

再谈过滤 我们已经在前面的章节接触过滤,但只是浅尝辄止.我们现在能够添加日志记录并设置接收器的属性,我们需要建立复杂的过滤功能.让我们看下这个例子: #include <string> #include <fstream> #include <iomanip> #include <boost/log/core.hpp> #include <boost/smart_ptr.hpp> #include <boost/log/sinks.hpp&

boost.log(四)记录器

记录器对象 在上个章节的接收器部分我们已经知道了boost.log库是如何存储日志的,现在是时候去尝试记录日志了.首先我们要创建一个记录器,这非常简单: boost::log::sources::logger lg; [注意]boost.log库在后台给记录器(logger)提供了写的功能,就如同BOOST_LOG_TRIVIAL 宏一样. 与接收器不同,记录器无须在任何地方注册,因为它们是直接与日志记录核心进行交互的.此外请注意boost.log库提供了两个版本的记录器: 线程安全的(logg

boost.log(一)打印日志

打印输出 对于那些不想阅读手册,只需要一个简单的工具用于日志记录的人.你可以直接在控制台中输出日志信息,首先你需要包含头文件boost/log/trivial.hpp,然后编写下面的代码: #include <iostream> #include <boost/log/trivial.hpp> int main(int, char*[]) { BOOST_LOG_TRIVIAL(trace) << "A trace severity message"