测试环境:
[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