EasyLog相关的头文件和.cpp文件的下载地址:https://download.youkuaiyun.com/download/pcb931126/11157258;
1.配置好相关路径,在.h文件中添加头文件
//日志类头文件
#include "../../Shared/Log/EasyLog.h"
#include "../../Shared/Log/SyncFileLog.h"
#include "../../Shared/Common/Utils.h"
//并在相关的类中添加函数声明
int InitLogFileSetting();
并将相应的点.h文件和.cpp文件添加在工程项目中,如下图所示
2.在.cpp文件中添加下面函数
//.cpp文件的初始化函数中添加
char m_cLogFolderPath[512];
strcpy(m_cLogFolderPath, "Log\\");
InitLogFileSetting(); //初始化
//定义全局函数
void FileLog(char * _pcData, int _iLen);
void FileLog(char * _pcData, int _iLen)
{
g_log.Write(_pcData, _iLen);
}
int C3D_BCSSizeDetectDlg::InitLogFileSetting()
{
g_log.SetCurExeFilePath(Utils::CurFilePath());
int nRet = g_log.SetLogFolderPath(m_cLogFolderPath);
if (nRet < 0)
{
ShowMessage(_T("设置日志文件夹路径 (%s) 失败, return %d\n"), m_cLogFolderPath, nRet);
return -2;
}
g_log.SetMaxFileSize(m_iMaxLogFileSize);
g_log.Init(30);
CEasyLog::SetFileLogDelegate(FileLog);
return 0;
}
调用函数,写入日志文件
/*--------------------------------------------------------------
*功能:在ListControl控件中显示处理信息
*--------------------------------------------------------------
*输入:const Cstring类型的字符串
*输出:在控件上显示字符串函数,并将显示的数据写入日志
*/
void C3D_BCSSizeDetectDlg::ShowMessage(const TCHAR* str, ...)
{
TCHAR tcBuf[MAX_LOCAL_BUF_SIZE] = { 0 };
char cBuf[MAX_LOCAL_BUF_SIZE] = { 0 };
va_list args;
va_start(args, str);
_vsntprintf_s(tcBuf, MAX_LOCAL_BUF_SIZE - 1, _TRUNCATE, str, args);
va_end(args);
CString strInfo=_T("");
strInfo.Format(_T("%s"), tcBuf);
SYSTEMTIME st;
GetLocalTime(&st);
CString strMessage;
strMessage.Format(_T("%04d-%02d-%02d %02d:%02d:%02d %03d| "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
//将相关信息写入到MFC控件中
strMessage += strInfo;
if (m_list.GetSafeHwnd()!= NULL)
{
int iSelIndex = m_list.AddString(strMessage);
if (iSelIndex != LB_ERR && iSelIndex != LB_ERRSPACE)
{
m_list.SetCurSel(iSelIndex);
}
}
//将运行状态写入日志
strMessage = strMessage + _T("\n");
//得到字符串的长度
int iLength = WideCharToMultiByte(CP_ACP, 0, strMessage, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, strMessage, -1, cBuf, iLength, NULL, NULL);//TCHAR转char
CEasyLog::__Log(cBuf, iLength); //将相关信息写入日志文件
return;
}
这样就在相关的目下自动创建以日期命名的文件夹
包含日志文件