博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
检查日志文件系统
阅读量:4046 次
发布时间:2019-05-24

本文共 6717 字,大约阅读时间需要 22 分钟。

2014年3月25日11:45:12

检查日志文件系统

1、使用VS2008创建一个带预编译头的控制台项目。

2、源文件内容:

#include "stdafx.h" #define LWW_CONSOLE #ifdef LWW_CONSOLE  #define CosPrintf printf  #else  #define CosPrintf //  #endif  #include 
#include
//#define LOG_FILE_MAX_SIZE 10*1024*1024 #define LOG_FILE_MAX_SIZE 20 #include
using namespace std;// 删除指定目录下所有文件及目录 bool DelDirContent(TCHAR * tcsPath) { WIN32_FIND_DATA wfd; HANDLE hFind; TCHAR tcsFullPath[MAX_PATH] = {0}; TCHAR tcsDirFilter[MAX_PATH] = {0}; _tcscpy(tcsDirFilter,tcsPath); _tcscat(tcsDirFilter,_T("\\*")); hFind = FindFirstFile(tcsDirFilter, &wfd); if (hFind == INVALID_HANDLE_VALUE) { printf ("FindFirstFile failed (%d)\n", GetLastError()); return false; } do { if (_tcscmp(wfd.cFileName, _T(".")) == 0 || _tcscmp(wfd.cFileName, _T("..")) == 0 ) { continue; } _tcscpy(tcsFullPath,tcsPath); _tcscat(tcsFullPath,_T("\\")); _tcscat(tcsFullPath,wfd.cFileName); //去掉只读属性 DWORD dwAttributes = GetFileAttributes(tcsFullPath); if (dwAttributes & FILE_ATTRIBUTE_READONLY) { dwAttributes &= ~FILE_ATTRIBUTE_READONLY; SetFileAttributes(tcsFullPath, dwAttributes); } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { DelDirContent(tcsFullPath); RemoveDirectory(tcsFullPath); } else { if(!DeleteFile(tcsFullPath)) { printf("删除文件失败\n"); } } }while (FindNextFile(hFind, &wfd)); FindClose(hFind); return true; } bool isValidYYYYMM(TCHAR * tcs,int nBeginYYYYMM,int nNowYYYYMM){ int nSize = _tcsclen(tcs); if(nSize != 6) return false; int nYYYYMM = _tstoi(tcs); if((nYYYYMM>=nBeginYYYYMM)&&(nYYYYMM<=nNowYYYYMM)) { return true; } else { return false; }}void getYYYYMM_months(tm * pNowTm,int nSaveMonth,int * pBeginYYYYMM,int *pNowYYYYMM){ int nSaveYear = nSaveMonth/12; int nBeginYear = (pNowTm->tm_year+1900) - nSaveYear; int nSaveMonth12 = nSaveMonth%12; int nBeginMonth = 0; int nNowMonth = pNowTm->tm_mon+1; if(nSaveMonth12 <= nNowMonth) { nBeginMonth = nNowMonth - nSaveMonth12 + 1; } else { nBeginYear--; nSaveMonth12 -= nNowMonth; nBeginMonth = 12 - nSaveMonth12 + 1; } *pBeginYYYYMM = nBeginYear * 100 + nBeginMonth; *pNowYYYYMM = (pNowTm->tm_year+1900) * 100 + nNowMonth; return;}bool isValidLogFileName(TCHAR * tcs,TCHAR * tcsYYYYMM,int nBeginDD,int nEndDD){ int nSize = _tcsclen(tcs); if((nSize != 19)&&(nSize != 12)) return false; TCHAR tcsTmp[40] = {0}; _tcsncpy(tcsTmp,tcs,6); if(_tcscmp(tcsTmp,tcsYYYYMM) != 0) return false; memset(tcsTmp,0,sizeof(tcsTmp)); _tcsncpy(tcsTmp,tcs+6,2); int nDD = _tstoi(tcsTmp); if((nDD
nEndDD)) return false; if(19 == nSize) { memset(tcsTmp,0,sizeof(tcsTmp)); _tcsncpy(tcsTmp,tcs+8,1); if(_tcscmp(tcsTmp,_T("_")) != 0) return false; memset(tcsTmp,0,sizeof(tcsTmp)); _tcsncpy(tcsTmp,tcs+9,6); int nHHMMSS = _tstoi(tcsTmp);//可细化 if((nHHMMSS<0)||(nHHMMSS>235959)) return false; memset(tcsTmp,0,sizeof(tcsTmp)); _tcsncpy(tcsTmp,tcs+15,4); if(_tcscmp(tcsTmp,_T(".txt")) != 0) return false; } else if(12 == nSize) { memset(tcsTmp,0,sizeof(tcsTmp)); _tcsncpy(tcsTmp,tcs+8,4); if(_tcscmp(tcsTmp,_T(".txt")) != 0) return false; } return true;}bool protectSecondLogDir(TCHAR * tcsPath,TCHAR * tcsYYYYMM,tm * pNowTm,int nSaveDay){ int nBeginDD = 1; int nEndDD = 31;//可细化 if(nSaveDay<=31)//一个月 { nEndDD = pNowTm->tm_mday; if(nEndDD > nSaveDay) { nBeginDD = nEndDD - nSaveDay +1; } } WIN32_FIND_DATA wfd; HANDLE hFind; TCHAR tcsFullPath[MAX_PATH] = {0}; TCHAR tcsDirFilter[MAX_PATH] = {0}; _tcscpy(tcsDirFilter,tcsPath); _tcscat(tcsDirFilter,_T("\\*")); hFind = FindFirstFile(tcsDirFilter, &wfd); if (hFind == INVALID_HANDLE_VALUE) { CosPrintf ("FindFirstFile failed (%d)\n", GetLastError()); return false; } do { if (_tcscmp(wfd.cFileName, _T(".")) == 0 || _tcscmp(wfd.cFileName, _T("..")) == 0 ) { continue; } _tcscpy(tcsFullPath,tcsPath); _tcscat(tcsFullPath,_T("\\")); _tcscat(tcsFullPath,wfd.cFileName); //去掉只读属性 DWORD dwAttributes = GetFileAttributes(tcsFullPath); if (dwAttributes & FILE_ATTRIBUTE_READONLY) { dwAttributes &= ~FILE_ATTRIBUTE_READONLY; SetFileAttributes(tcsFullPath, dwAttributes); } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { DelDirContent(tcsFullPath); RemoveDirectory(tcsFullPath); } else { if(!isValidLogFileName(wfd.cFileName,tcsYYYYMM,nBeginDD,nEndDD)) { if(!DeleteFile(tcsFullPath)) { CosPrintf("删除文件失败\n"); } } if(wfd.nFileSizeLow>2*LOG_FILE_MAX_SIZE) { if(!DeleteFile(tcsFullPath)) { CosPrintf("删除文件失败\n"); } } } }while (FindNextFile(hFind, &wfd)); FindClose(hFind); return true; }bool protectTopLogDir(TCHAR * tcsPath,tm * pNowTm,int nSaveDay){ if(nSaveDay < 1) { nSaveDay = 1; } else if(nSaveDay > 30*12*10) { nSaveDay = 30*12*10; } int nSaveMonth = nSaveDay/30; if(nSaveMonth<=0) { nSaveMonth = 1; } else { nSaveDay = 60;//由其他函数来精确 } int nBeginYYYYMM = 0; int nNowYYYYMM = 0; getYYYYMM_months(pNowTm,nSaveMonth,&nBeginYYYYMM,&nNowYYYYMM); WIN32_FIND_DATA wfd; HANDLE hFind; TCHAR tcsFullPath[MAX_PATH] = {0}; TCHAR tcsDirFilter[MAX_PATH] = {0}; _tcscpy(tcsDirFilter,tcsPath); _tcscat(tcsDirFilter,_T("\\*")); hFind = FindFirstFile(tcsDirFilter, &wfd); if (hFind == INVALID_HANDLE_VALUE) { CosPrintf ("FindFirstFile failed (%d)\n", GetLastError()); return false; } do { if (_tcscmp(wfd.cFileName, _T(".")) == 0 || _tcscmp(wfd.cFileName, _T("..")) == 0 ) { continue; } _tcscpy(tcsFullPath,tcsPath); _tcscat(tcsFullPath,_T("\\")); _tcscat(tcsFullPath,wfd.cFileName); //去掉只读属性 DWORD dwAttributes = GetFileAttributes(tcsFullPath); if (dwAttributes & FILE_ATTRIBUTE_READONLY) { dwAttributes &= ~FILE_ATTRIBUTE_READONLY; SetFileAttributes(tcsFullPath, dwAttributes); } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if(isValidYYYYMM(wfd.cFileName,nBeginYYYYMM,nNowYYYYMM)) { protectSecondLogDir(tcsFullPath,wfd.cFileName,pNowTm,nSaveDay); CosPrintf("isValidYYYYMM\n"); } else { DelDirContent(tcsFullPath); RemoveDirectory(tcsFullPath); } } else { if(!DeleteFile(tcsFullPath)) { CosPrintf("删除文件失败\n"); } } }while (FindNextFile(hFind, &wfd)); FindClose(hFind); return true; }bool getLocalTime(tm * pNowTm) { tm * pTm; time_t nowTime; nowTime = time(NULL); pTm = localtime(&nowTime); memcpy(pNowTm,pTm,sizeof(tm)); return true; } int main( void ) { char ch; int nSaveDay = 5; tm aNowTm; getLocalTime(&aNowTm); TCHAR tcsFileName[MAX_PATH] = {0}; _tcscpy(tcsFileName,TEXT("CenterTransLog")); protectTopLogDir(tcsFileName,&aNowTm,nSaveDay); printf("程序结束\n"); cin>>ch; return 0; }

转载地址:http://yjwci.baihongyu.com/

你可能感兴趣的文章
CAS实现SSO单点登录原理
查看>>
MongoDB中的_id和ObjectId
查看>>
美团酒店Node全栈开发实践
查看>>
MongoDB分片实战(一):集群搭建
查看>>
MongoDB分片实战(二):Sharding
查看>>
TLS/HTTPS 证书生成与验证
查看>>
用Node.js创建自签名的HTTPS服务器
查看>>
单点登录的三种实现方式
查看>>
Mongodb启动命令mongod参数说明
查看>>
理解Node.js中间件以及Connect 模块
查看>>
Nodejs基础中间件Connect
查看>>
Http头介绍:Expires,Cache-Control,Last-Modified,ETag
查看>>
Nginx+Tomcat实现负载均衡、Redis实现Tomcat session会话共享
查看>>
MySQL集群
查看>>
mongodb mongoexprt 导出数据 json csv格式
查看>>
MySQL MERGE存储引擎 简
查看>>
数据库分片(Sharding)与分区(Partition)的区别
查看>>
node.js递归打印文件目录、文件名
查看>>
本地与远程linux上传下载
查看>>
NodeJS的代码调试和性能调优
查看>>