MFC写日志功能,支持中文
这是一个Unicode环境下支持中文的 txt 文件
每一个小时生成一个新的文件,以 “年月日时” 命名
每一条数据占一行,每一条数据前跟 “年月日时分秒” 标签
速度可达到每秒300条左右
//得到exe的根路径 所有的路径函数返回值最后都包括"\\" CString GetExeRootPath() { CString strModule; HMODULE hmod = GetModuleHandle(NULL);//获取一个应用程序或动态链接库的模块句柄。只有在当前进程的场景中,这个句柄才会有效。 GetModuleFileName(hmod, strModule.GetBuffer(MAX_PATH), MAX_PATH);//MAX_PATH是C语言运行时库中通过#define指令定义的一个宏常量,它定义了编译器所支持的最长全路径名的长度。 strModule.ReleaseBuffer();//在使用GetBuffer后使用 CString strModulePath; int nPos = strModule.ReverseFind(_T('\\')); strModulePath = strModule.Left(nPos + 1); return strModulePath; }
void CMFCApplication4Dlg::WriteDataToLogFile(CString strData) { CTime mTime; mTime = CTime::GetCurrentTime(); CString strFileName; strFileName.Format(_T("%04d-%02d-%02d-%02d.log"), mTime.GetYear(), mTime.GetMonth(), mTime.GetDay(), mTime.GetHour()); CString strPath; strPath = GetExeRootPath(); strPath = strPath + _T("Data\\log\\") + strFileName; setlocale(LC_CTYPE, ("chs")); CStdioFile file; CFileException e; CString strErr; if (file.Open(strPath, CFile::modeCreate | CFile::modeNoTruncate | CFile::shareDenyNone | CFile::modeWrite | CFile::typeText, &e) == 0) { AfxMessageBox(_T("打开") + strPath + _T("文件失败")); strErr.Format(_T("错误代码:%d"), e.m_cause); AfxMessageBox(strErr); return; } file.SeekToEnd(); CString strTime; strTime.Format(_T("[%04d-%02d-%02d %02d:%02d:%02d]"), mTime.GetYear(), mTime.GetMonth(), mTime.GetDay(), mTime.GetHour(), mTime.GetMinute(), mTime.GetSecond()); strData = strTime + strData + _T("\n"); file.WriteString(strData); file.Close(); }
关键点在于 CFile::shareDenyNone,否则打开失败可能性大增,错误代码:11 CFileException::sharingViolation 未加载SHARE.EXE或共享区被锁
上面的这个写日志功能不太好用,再来个经过验证的log日志类,线程中写也没有问题,支持format功能
#pragma once class CLog { public: CLog(); virtual ~CLog(); CString GetAppPath(); void WriteLog(const char *format, ...); };
#include "pch.h" #include "CLog.h" CLog::CLog() { } CLog::~CLog() { } CString CLog::GetAppPath() { CString strFilePath; GetModuleFileName(NULL, strFilePath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH); strFilePath.ReleaseBuffer(); int m_iPosIndex; CFileFind m_FileFind; m_iPosIndex = strFilePath.ReverseFind(_T('\\')); strFilePath = strFilePath.Left(m_iPosIndex); return strFilePath; } void CLog::WriteLog(const char *format, ...) { va_list arg; int done; va_start(arg, format); CString strPath = GetAppPath() + "\\MESLOG"; CFileFind m_FileFind; if (!m_FileFind.FindFile(strPath)) { CreateDirectory(strPath, NULL); } SYSTEMTIME sys; ::GetLocalTime(&sys); char szFileName[256] = { 0 }; sprintf_s(szFileName, sizeof(szFileName), "%s\\%d-%d-%d.log", strPath.GetBuffer(), sys.wYear, sys.wMonth, sys.wDay); try { FILE *pFile; fopen_s(&pFile, szFileName, "a"); if (pFile == NULL) { return; } fprintf(pFile, "%d-%02d-%02d %02d:%02d:%02d:%03d[%04d] ", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, ::GetCurrentThreadId()); done = vfprintf(pFile, format, arg); va_end(arg); fprintf(pFile, "\r\n"); fflush(pFile); fclose(pFile); } catch (...) { AfxMessageBox("写log文件失败"); } }
CLog mLog; mLog.WriteLog("检测到断开 原因--->%s", sError); mLog.WriteLog("error");