Easylogging的封装使用二

本文详细介绍了Easylogging++的高级用法,包括配置文件的设置、日志级别管理、自定义日志格式和多线程日志同步。通过实例展示了如何在项目中有效利用Easylogging++进行日志记录,提升调试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.对Char类型的处理
在Easylogging的封装使用一:http://blog.youkuaiyun.com/woshichenweixian/article/details/77278488 中讲到,对char类型的输出需要进行一些特殊处理,最简单的处理是在最终输出char类型数据时进行一个强制转换,转成整形数据。
Easylogging中log数据的输出是在其类class Writer中实现的。在该类中对各种数据类型进行了<<符号重载中,这里截几个数据类型的输出如下:
 inline Writer& operator<<(const std::string& log_) {
        if (!proceed_) { return *this; }
        _ELPP_STREAM(logger_) << log_;
        return *this;
    }
    inline Writer& operator<<(char log_) {
        if (!proceed_) { return *this; }
        _ELPP_STREAM(logger_) << log_;
        return *this;
    }
    inline Writer& operator<<(bool log_) {
        if (!proceed_) { return *this; }
        _ELPP_STREAM(logger_) << log_;
        return *this;
    }
    inline Writer& operator<<(signed short log_) {
        if (!proceed_) { return *this; }
        _ELPP_STREAM(logger_) << log_;
        return *this;
    }

分别将std::string,char,bool,signed short数据类型输出到_ELPP_STREAM(logger_)中,展开该宏可以看到是返回logger_的stream成员:
#define _ELPP_STREAM(l) (*(l->stream()))
logger_的stream是std::stringstream指针类型:
std::stringstream* stream_;
所以,我们的log数据最终都被放进标准库的string流中,在这之前,可改变要输出的数据的类型。例如,将char转成整形:
inline Writer& operator<<(char log_) {
        if (!proceed_) { return *this; }
        _ELPP_STREAM(logger_) << static_cast<int>(log_);
        return *this;
    }
通过static_cast将char数据转成int类型。

二.对数组的处理
在《 Easylogging的封装使用一》中给出了输出数组的封装,其同样有char类型乱码的问题,同样可通过强转避免:
	template<class _TYPE>
	void logDump(char *info,_TYPE *buff ,int size,LEVEL logLevel)                                                                                       
	{                                                                                                                             
		std::string str(info) ;                                                                                                        
		str += ": " ;                                                                 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值