資源簡介
由于項目經常調試,會經常累積大量日志,因此需要寫一個定時刪除的功能,代碼中精確到秒,路徑可以自己輸入,也可以默認為當前路徑(Glog文件,需自己創建一個空文件),代碼中包含寫日志,因此可以自己測試,由于寫得急促,多處需要優化。由于代碼使用線程寫的,因此可以很方便地移植到其他項目中。
代碼片段和文件信息
#include?“Glog.h“
#include?“delglog.h“
using?namespace?std;
unsigned?int?__stdcall?DelglogRun(void?*arg)
{
string?s=Delglog::GetInstance()->GetPath();
//string?s=“C:\\ScraperThreeD\\Glog“;
char*?c;?
????const?int?len?=?s.length();?
????c?=?new?char[len+1];?
????strcpy(cs.c_str());
Delglog::GetInstance()->DelglogRealize(c);
return?0;
}
Delglog?*Delglog::m_pDelglog?=?new?Delglog();
void?Delglog::DelglogStart()
{
HANDLE?hThread;
hThread?=?(HANDLE)_beginthreadex(NULL?0?DelglogRun?NULL?0?NULL);
}
string?Delglog::GetPath()
{
char?szExePath[MAX_PATH]?=?{0};??
GetModuleFileNameA(NULLszExePathMAX_PATH);?
string?strExsPath?=?szExePath;
int?pos1?=?strExsPath.find_last_of(‘\\‘);
string?strAppName(strExsPath.substr(pos1?+?1)?);
string?a?=?strExsPath.substr(0pos1);
int?pos2?=?a.find_last_of(‘\\‘);
string?b?=?a.substr(0pos2);
int?pos3?=?strAppName.find_last_of(‘.‘);
strAppName?=?strAppName.substr(0pos3);
string?strLogPath?=?b?+“\\Glog“;
return?strLogPath;
}
//獲取文件創建時間,若從其它路徑移過來的文件以這個文件移過來的時間為準
string?Delglog::FileTime(PFILETIME?pfile)
{
????FILETIME?fileTime;
????SYSTEMTIME?st;
????FileTimeToLocalFileTime(pfile&fileTime);
????FileTimeToSystemTime(&fileTime?&st);
string?time;
string?sysmsdshsmisse;//年月日,時分秒
stringstream?ssyssmssdsshssmissse;
ssy< ssy>>sy;
ssm< ssm>>sm;
if(atoi(sm.c_str())<10)
sm=“0“+sm;
ssd< ssd>>sd;
if(atoi(sd.c_str())<10)
sd=“0“+sd;
ssh< ssh>>sh;
if(atoi(sh.c_str())<10)
sh=“0“+sh;
ssmi< ssmi>>smi;
if(atoi(smi.c_str())<10)
smi=“0“+smi;
ssse< ssse>>sse;
if(atoi(sse.c_str())<10)
sse=“0“+sse;
time=sy+sm+sd+sh+smi+sse;
LOG(INFO)<<“ftime:????“< return?time;
????//cout<}
//函數功能:?掃描查找文件
//參數說明:
//szPath:需要掃描的目錄
void?Delglog::DelglogRealize(char?*szPath)
{
????WIN32_FIND_DATAA?FindFileData;
????char?szFileToFind[MAX_PATH]?=?{0};
????lstrcpyA(szFileToFind?szPath);
????lstrcatA(szFileToFind?“\\*.*“);
????//cout< ????//printf(“\n“);
????//查找目錄下所有文件
????HANDLE?hFile?=?FindFirstFileA(szFileToFind&FindFileData);
????if?(hFile?==?INVALID_HANDLE_VALUE)return;
????do
????{
????????char?szNewPath[MAX_PATH]?=?{0};
????????lstrcpyA(szNewPath?szPath);
????????if(szPath==“*.“)
????????{
????????????LOG(INFO)< ????????????//printf(“\n“);
????????}
????????//判斷是否是目錄
????????if?(FindFileData.dwFileAttributes?==?FILE_ATTRIBUTE_DIRECTORY)
????????{
????????????//判斷是否是.或..(沒有下級文件?)
????????????if?(!lstrcmpA(FindFileData.cFileName?“.“)?||?!lstrcmpA(FindFileData.cFileName?“..“))
????????????{
????????????}
????????????else
????????????{
????????????????//遞歸查找下級目錄
????????????????lstrcatA(szNewPath?“\\“);
????????????????lstrcatA(szNewPath?FindFileData.cFileName);
????????????????Delglog::DelglogRealize(szNewPath);
????????????????
評論
共有 條評論