一.对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 += ": " ;