标签:程序 名称 plog open list hang *** 命名 ==
g_pLog = new CLog("log"); g_pLog->Enable(); g_pScrLog = new CLog("data"); g_pScrLog->Enable(); CString srelog= _T("log..."); g_pLog->WriteLogFile(srelog);
#include <sys/stat.h> #include <fcntl.h> #include <io.h> #include <afxmt.h> #define MAX_ROW 5000 /********************************************************************************** 程序在当前目录下生成日志文件夹(如果该文件夹存在,则不生成), 每天的日志放在一个以当天日期命名的文件夹中。 MAX_ROW 定义一个日志文件的最大行数,一个日志文件达到这个行数后即关闭,并创建一个新文件。 **********************************************************************************/ class CLog { public: CLog(char* directory); ~CLog(); public: void Disable(); // 关闭日志功能 void Enable(); // 打开日志功能 void WriteLogFile(const char* fmt, ... );// 写日志函数 void DeleteLogFile(); protected: int NewLogFile(); // 创建新日志文件 BOOL IsChangeDay(); // 判断是否跨天(凌晨0点) CString GetCurntTime(); // 获得当前时间字符串,格式为 200709061150 BOOL DelTree(LPCTSTR lpszPath); private: int m_filehandle; int m_row; int m_dirnum; int m_filenum; BOOL m_needlog; char m_sContent[1024*1024]; CString m_strContent; CString m_sCurtTime; CString m_sYestady; CString m_sToday; CString m_sCurtDir; CString m_sLogDir; CString m_sLogDayDir; CCriticalSection m_cs; }; extern CLog *g_pLog; extern CLog *g_pScrLog;
#include "stdafx.h" #include "Log.h" #include <direct.h> CLog *g_pLog; // 日志类指针 CLog *g_pScrLog; // 设备原始数据记录指针 /* ============== 创建日志文件 参数: directory 目录名称 返回值: 无 *///============= CLog::CLog(char* directory) { m_row = 0; m_filehandle = 0; m_dirnum = 0; m_filenum = 0; m_sCurtTime = _T(""); m_sToday = _T(""); m_sYestady = _T(""); m_sCurtDir = _T(""); m_sLogDir = _T(""); m_needlog = FALSE; GetCurrentDirectory(_MAX_PATH,m_sCurtDir.GetBuffer(_MAX_PATH)); m_sCurtDir.ReleaseBuffer(); CString sDirName; CFileFind f; BOOL bHasLogDir = FALSE; BOOL bFind = f.FindFile(m_sCurtDir + "\\*.*"); // 文件是否存在 while (bFind) { bFind = f.FindNextFile(); if (f.IsDots()) continue; if (f.IsDirectory()) { sDirName = f.GetFileName(); sDirName.MakeLower(); if (sDirName == (CString)directory) { bHasLogDir = TRUE; break; } } } m_sLogDir = m_sCurtDir + (CString)"\\" + (CString)directory; if (!bHasLogDir) { _mkdir(m_sLogDir); } CString strTime = GetCurntTime (); m_sToday = strTime.Mid(0,8); } CLog::~CLog() { if(m_filehandle != 0) _close(m_filehandle); } /* ============== 创建日志文件 参数: 无 返回值: 0 成功.-1 失败 *///============= int CLog::NewLogFile() { CString strLogfileName; if (m_filenum > 0) { _close(m_filehandle); } strLogfileName = m_sLogDayDir + (CString)"\\" + m_sCurtTime + (CString)".Log"; if ((m_filehandle=_open(strLogfileName, _O_WRONLY)) == -1) { if ((m_filehandle=_open(strLogfileName, _O_WRONLY|_O_CREAT|O_TRUNC, _S_IREAD|_S_IWRITE)) == -1) { printf("cannot create log file:%s\n",strLogfileName); return -1; } } m_filenum++; return 0; } /* ============== 判断是否跨天(凌晨0点) 参数: 无 返回值: false 当天 true 跨天 *///============= BOOL CLog::IsChangeDay() { m_sCurtTime = GetCurntTime(); m_sToday = m_sCurtTime.Mid(0,8); if (m_sToday != m_sYestady) { m_sYestady = m_sToday; m_sLogDayDir = m_sLogDir + (CString)"\\" + m_sToday; _mkdir(m_sLogDayDir); return TRUE; } return FALSE; } /* ============== 写日志函数 参数: 要记录的内容 返回值: 无 *///============= void CLog::WriteLogFile(const char* fmt, ... ) { if (!m_needlog) return; m_cs.Lock(); if (m_row >= MAX_ROW || IsChangeDay()) { if (NewLogFile() == -1) { m_cs.Unlock(); return; } m_row = 0; } va_list v_args; va_start(v_args,fmt); vsprintf( m_sContent, fmt, v_args ); va_end( v_args ); CString strTime; CTime cTime = CTime::GetCurrentTime(); strTime = _T("20") + cTime.Format("%y-%m-%d %H:%M:%S"); m_strContent = (CString)"[" + strTime + (CString)"]" + (CString)m_sContent + "\n"; _write(m_filehandle, m_strContent, m_strContent.GetLength()); m_row++; m_cs.Unlock(); } /* ============== 获得当前时间字符串,格式为 20070906 参数: 无 返回值: 当前时间字符串 *///============= CString CLog::GetCurntTime() { CString strTime; CTime cTime = CTime::GetCurrentTime(); strTime = _T("20") + cTime.Format("%y%m%d%H%M%S"); return strTime; } /* ============== 打开日志功能 参数: 无 返回值: 无 *///============= void CLog::Enable() { m_needlog = TRUE; } /* ============== 关闭日志功能 参数: 无 返回值: 无 *///============= void CLog::Disable() { m_needlog = FALSE; } /* ============== 删除7天前的日志 参数: 无 返回值: 无 *///============= void CLog::DeleteLogFile() { CString iFlieName,iFlieName1,iToday,TStr; int k,k1,k2; char s[60] = {‘\0‘}; TStr = GetCurntTime(); iToday = TStr.Mid(0,8); CFileFind ff; BOOL bFound = ff.FindFile(m_sLogDir+"\\*", 0); while(bFound) { bFound = ff.FindNextFile(); if (ff.IsDots()) continue; if (ff.IsDirectory()) { // 这里是目录 iFlieName = ff.GetFilePath(); iFlieName1 = iFlieName.Right(8); k = atoi(iToday)/100 - atoi(iFlieName1)/100; k1 = atoi(iToday) - atoi(iFlieName1); k2 = 31 - (atoi(iFlieName1)%100) + (atoi(iToday)%100); _tcscpy( s , iFlieName.GetBuffer(iFlieName.GetLength()) ) ; if ((k>1)&&(k<12)) { DelTree((LPCTSTR)s); } else if (k == 0) { if (k1 > 7) { DelTree((LPCTSTR)s); } } else if (k == 1) { if (k2 > 7) { DelTree((LPCTSTR)s); } } else { if ((atoi(iToday)/1000-atoi(iFlieName1)/1000)==1) { if ((atoi(iFlieName1)%10000)/100 == 12) { if (k2 > 7) { DelTree((LPCTSTR)s); } } else { DelTree((LPCTSTR)s); } } else { DelTree((LPCTSTR)s); } } } } ff.Close(); } /* ============== 删除文件夹 参数: 文件夹的路径 返回值: 删除是否成功 *///============= BOOL CLog::DelTree(LPCTSTR lpszPath) { SHFILEOPSTRUCT FileOp; FileOp.fFlags = FOF_NOCONFIRMATION; FileOp.hNameMappings = NULL; FileOp.hwnd = NULL; FileOp.lpszProgressTitle = NULL; FileOp.pFrom = lpszPath;//路径名从何处来 FileOp.pTo = NULL;// FileOp.wFunc = FO_DELETE; return SHFileOperation(&FileOp) == 0; }
标签:程序 名称 plog open list hang *** 命名 ==
原文地址:https://www.cnblogs.com/osbreak/p/9534179.html