easylogging++ 按天数生成新日志文件

 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值