boost.log从编译到使用

本文介绍了如何从boost.org下载并编译Boost.Log为动态库,重点关注编译选项和运行时依赖。在多模块项目中,推荐将log库编译为动态库,并在每个依赖模块中设置预编译宏BOOST_ALL_DYN_LINK。文中还提到,通过logging::add_console_log和logging::add_file_log定制日志输出,并使用keyword::format进行格式化。对于大型项目,可以创建一个专门的log管理DLL工程,以保持程序目录整洁并确保全局唯一输出模块。

最近新开项目,要用到log,虽然现在的log库不少。但是要为C++项目增加库还是首选boost。

首先从boost.org上下载最新的boost,编译:

bjam --with-log link=shared runtime-link=shared

编译完成,发现生成了stage\lib目录下不仅有log库,还包含了其他一些库,这些都是log在运行中需要依赖的。将这些lib和dll全部copy到需要用到的可执行文件目录下。

需要注意的几点:

现在一般的应用都是由多模块组成,而且每一个模块都需要输出log,所以将log库编译为动态库是最好的选择

依赖log库的模块(lib/dll/exe), 需要加入预编译宏BOOST_ALL_DYN_LINK,否则在链接时会找不到对应的boost lib文件

 

log库的使用:

这部分都是参考别人的博客来写的, 具体请参看:https://www.cnblogs.com/liaocheng/p/4222885.html

#pragma once
#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/utility/setup/console.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/attributes/timer.hpp>
#include <boost/log/support/date_time.hpp>

void InitLog(std::string dir);
#include "stdafx.h"
#include "log.h"

namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;
BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime);

void InitLog(std::string dir)
{
	boost::log::add_console_log(std::clog, keywords::format = expr::stream << expr::format_date_time(_timestamp, "[%Y-%m-%d,%H:%M:%S.%f] ")
		<< "[" << expr::attr< boost::log::trivial::severity_level >("Severity") << "] " 
		<< expr::message);
	boost::log::add_file_log(keywords::file_name = dir + "/yuer_%Y-%m-%d_%H-%M-%S.%N.log", keywords::auto_flush = true
		, keywords::rotation_size = 10 * 1024 * 1024
		, keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0)
		, keywords::format = expr::stream << expr::format_date_time(_timestamp, "[%Y-%m-%d,%H:%M:%S.%f] ") << "[" << expr::attr< boost::log::trivial::severity_level >("Severity") <<"] "<< expr::message
		, keywords::min_free_space = 3 * 1024 * 1024
	);
	boost::log::add_common_attributes();
}

logging::add_console_log是为控制台输出做定制化

logging::add_file_log是为文件输出做定制化

keyword::format是格式化输出, rotation_size是日志大小,超过该大小重新生成日志文件

InitLog只能在应用初始化时调用一次,之后其他地方可以调用BOOST_LOG_TRIVIAL进行输出:

BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << ".";

可以将log模块编译为动态依赖C++库的lib版本:

b2 toolset=msvc-15.0 address-model=32 link=static runtime-link=shared variant=debug --with-log,这样的好处是不需要引入log库依赖的其他库dll,目录很整洁方便,不过需要额外创建一个log管理的DLL工程用于提供给其他模块使用,保证整个程序的输出模块只有一个。boost.log库的介绍中也提到了:

The library has a separately compiled part which should be built as described in the Getting Started guide. One thing should be noted, though. If your application consists of more than one module (e.g. an exe and one or several dll's) that use Boost.Log, the library must be built as a shared object. If you have a single executable or a single module that works with Boost.Log, you may build the library as a static library.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值