Sfoglia il codice sorgente

删除日志文件后,自动建立文件

zzj 6 anni fa
parent
commit
cc0e1c5df4
2 ha cambiato i file con 11 aggiunte e 1 eliminazioni
  1. 10 1
      logger.cpp
  2. 1 0
      test.cpp

+ 10 - 1
logger.cpp

@@ -24,7 +24,7 @@ struct log_file
 	time_t	_last_time;//最后操作文件的时间
 	int 	_time_align;//日志文件名对齐的时间,支持 dhm -- 天、小时、分钟
 
-	zclock  _err_clock;
+	zclock  _err_clock,_check_dir_clock;
 
 	log_file(const boost::string_ref&fpath,uint32_t min_size=10,int time_align='h')
 		:_fname(fpath)
@@ -80,6 +80,7 @@ struct log_file
 		_fp=fopen(_fname.c_str(),"a+");
 		_cur_size=(uint32_t)ftell(_fp);
 
+		_check_dir_clock.reset();
 		return 0;
 	}
 
@@ -103,6 +104,14 @@ struct log_file
 			reopen();
 		}
 
+		if(_check_dir_clock.count_ms()>1000)
+		{
+			if(access(_fname.c_str(),0)==-1)
+				reopen();
+
+			_check_dir_clock.reset();
+		}
+
 		int rc=fwrite(p,1,len,_fp);
 		if(rc<len)
 		{

+ 1 - 0
test.cpp

@@ -12,6 +12,7 @@ int main()
 	for(int i=0;;i++)
 	{
 		log_info("%s",s.c_str());
+		usleep(10000);
 	}
 
 	std::this_thread::yield();