easylogging修改按天保存
初始化时保存日志文件名格式按年月日结尾
el::Configurations conf;
//el::Configurations conf("E:\\github\\c-\\faceDetect\\Release\\log.conf");
//el::Configurations conf("log.conf");
//el::Loggers::reconfigureAllLoggers(conf);
// 重新设置GLOBAL级别的配置项FORMAT的值
conf.setGlobally(el::ConfigurationType::Format, "[%datetime{%H:%m:%s} | %level] %msg");
conf.setGlobally(el::ConfigurationType::Enabled, "true");
conf.setGlobally(el::ConfigurationType::ToFile, "true");
conf.setGlobally(el::ConfigurationType::Filename, "log\\log_%datetime{%Y%M%d}.log");
conf.setGlobally(el::ConfigurationType::MillisecondsWidth, "3");
conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "104857600"); //单位:B / 1048576 1MB / 104857600 100MB / 1073741824 1GB
// 选择划分级别的日志
el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging);
// 设置级别门阀值,修改参数可以控制日志输出
el::Loggers::setLoggingLevel(el::Level::Error);
// 重新设置配置
el::Loggers::reconfigureAllLoggers(conf);
自用,简单粗暴,不多说,上代码
easylogging++.cpp中unsafeValidateFileRolling修改
static int m_nToday = 0;
bool TypedConfigurations::unsafeValidateFileRolling(Level level, const PreRollOutCallback& preRollOutCallback) {
base::type::fstream_t* fs = unsafeGetConfigByRef(level, &m_fileStreamMap, "fileStream").get();
if (fs == nullptr) {
return true;
}
std::size_t maxLogFileSize = unsafeGetConfigByVal(level, &m_maxLogFileSizeMap, "maxLogFileSize");
std::size_t currFileSize = base::utils::File::getSizeOfFile(fs);
if (maxLogFileSize != 0 && currFileSize >= maxLogFileSize) {
std::string fname = unsafeGetConfigByRef(level, &m_filenameMap, "filename");
ELPP_INTERNAL_INFO(1, "Truncating log file [" << fname << "] as a result of configurations for level ["
<< LevelHelper::convertToString(level) << "]");
fs->close();
preRollOutCallback(fname.c_str(), currFileSize);
fs->open(fname, std::fstream::out | std::fstream::trunc);
return true;
}
time_t raw_time;
struct tm* tm_info;
time(&raw_time);
tm_info = localtime(&raw_time);
if (m_nToday == 0)
{
m_nToday = tm_info->tm_mday;
return false;
}
if (m_nToday != tm_info->tm_mday)
{
m_nToday = tm_info->tm_mday;
std::string fname = unsafeGetConfigByRef(level, &m_filenameMap, "filename");
ELPP_INTERNAL_INFO(1, "Truncating log file [" << fname << "] as a result of configurations for level ["
<< LevelHelper::convertToString(level) << "]");
fs->close();
preRollOutCallback(fname.c_str(), currFileSize);
int nPos = fname.find_last_of('.');
char szDay[16] = { 0 };
sprintf_s(szDay,"%04d%02d%02d", tm_info->tm_year + 1900, tm_info->tm_mon + 1, m_nToday);
fname = fname.substr(0, nPos - 8) + std::string(szDay) + fname.substr(nPos);
fs->open(fname, std::fstream::out | std::fstream::trunc);
setValue(level,fname,&m_filenameMap);
/*
fname = unsafeGetConfigByRef(level, &m_filenameMap, "filename");
MessageBox(NULL, fname.c_str(), NULL, NULL);
*/
return true;
}
return false;
}