boost::log库,示例代码

本文提供了boost::log库的简单示例和多线程应用,并分享了一个基于boost::log的封装,测试环境为boost 1.57、1.64及VS2013、VS2017。内容包括最简单的日志输出、实用的日志记录以及多线程日志处理。所有代码在VS2017 Update5和boost 1.65下通过测试,源代码采用utf-8编码。
摘要由CSDN通过智能技术生成

测试环境:

[1]boost 1.57,  1.64

[2]VS2013Update3,  VS2017Update5

 

介绍

       前面给出几个简单的示例, 最后给出Boost::Log的封装。

主要来源
[1]boost官网,做了些微小的改动。

 

最简单的示例

 

// TestBoostLog.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

#include <boost/log/trivial.hpp>

//
// Title: Test Boost Log, Example 1
// Environment: VS2013Upate4, boost 1.57
// description: output trace to the console!
//
// The example dependent 6 boost library
// boost_chrono
// boost_date_time
// boost_filesystem
// boost_log
// boost_system
// boost_thread
//

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;


int _tmain(int argc, _TCHAR* argv[])
{
	BOOST_LOG_TRIVIAL(trace) << "测试中文能否正确输出(GBK编码)";

	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";


	std::cin.get();

	return 0;
}

 

 

 

 

 

最实用的示例

 

#include "KagulaLog.h"


#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;


//[ example_tutorial_file_advanced
void init()
{
	auto file_sink = 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 >*/
		);

	file_sink->locked_backend()->set_file_collector(sinks::file::make_collector(
		keywords::targe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值