📄 logmanager.cpp
字号:
// LogManager.cpp: implementation of the CLogManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PublicFunc.h"
#include "ItemBase.h"
#include "ErrorManager.h"
#include "LogManager.h"
#include "XmlManager.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CLogManager::CLogManager()
{
InitConfig();
}
CLogManager::~CLogManager()
{
}
CLogManager* CLogManager::Instance()
{
static CLogManager logMngr;
return &logMngr;
}
int CLogManager::LoggingToDebug(CString strLogging)
{
OutputDebugString(strLogging);
return ERROR_NONE;
}
int CLogManager::LoggingToFile(CString strLogging)
{
int nErr=ERROR_NONE;
m_csLog.Lock();
try
{
CString strLogFile;
m_itemLogs.GetAttr(XML_LOGMANAGER_LOGFILE, strLogFile);
if (strLogFile.IsEmpty()) strLogFile = _T("ZNetErrors.log");
CString strLogPath=CPublicFunc::GetFullPathOfFile(strLogFile);
CStdioFile fLog(strLogPath,
CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::typeText);
fLog.SeekToEnd();
fLog.WriteString(strLogging);
fLog.WriteString(_T("\r\n"));
fLog.Close();
}
catch(...)
{
nErr = ERROR_LOGMANAGER_LOGFILE;
}
m_csLog.Unlock();
return nErr;
}
int CLogManager::Logging(CString strLogging)
{
int nErr=ERROR_NONE;
strLogging = _T("XXX[ZNet]XXX: ") + strLogging + _T("\r\n");
int nLogTo=LOGTO_NONE;
m_itemLogs.GetAttr(XML_LOGMANAGER_LOGTO, nLogTo);
switch (nLogTo)
{
case LOGTO_DEBUG:
nErr = LoggingToDebug(strLogging);
break;
case LOGTO_FILE:
nErr = LoggingToFile(strLogging);
break;
}
return nErr;
}
int CLogManager::InitConfig()
{
CString strErrorXml=_T("ZNetConfig.xml");
CString strXmlPath=CPublicFunc::GetFullPathOfFile(strErrorXml);
CXmlManager XmlMngr;
int nErr=XmlMngr.Load(strXmlPath);
if (NOERROROCCUR(nErr))
nErr=XmlMngr.GetItem(_T("LogConfig"), m_itemLogs);
return nErr;
}
int CLogManager::Logging(CErrorInfo ErrInfo)
{
int nErr=ERROR_NONE;
//过滤警告级别
int nLogLevel=ERRORLEVEL_NONE;
m_itemLogs.GetAttr(XML_LOGMANAGER_LOGLEVEL, nLogLevel);
if (ErrInfo.m_nErrorLevel<nLogLevel)
nErr = ERROR_LOGMANAGER_LOGFILTER;
//过滤域列表
CString strFindDomain=_T(";")+ErrInfo.m_strDomain+_T(";");
CString strDomainList;
m_itemLogs.GetAttr(XML_LOGMANAGER_LOGDOMAINLIST, strDomainList);
if (!strDomainList.IsEmpty() && strDomainList.Find(strFindDomain)<0)
nErr = ERROR_LOGMANAGER_LOGFILTER;
//过滤类列表
CString strFindClass=_T(";")+ErrInfo.m_strClass+_T(";");
CString strClassList;
m_itemLogs.GetAttr(XML_LOGMANAGER_LOGCLASSLIST, strClassList);
if (!strClassList.IsEmpty() && strClassList.Find(strFindClass)<0)
nErr = ERROR_LOGMANAGER_LOGFILTER;
if (NOERROROCCUR(nErr))
{
CString strErr=ErrInfo.GetErrorText();
nErr = Logging(strErr);
}
return nErr;
}
int CLogManager::Logging(const char *format, ...)
{
char s[1024];
va_list argp;
va_start( argp, format );
vsprintf(s, format, argp);
va_end( argp );
CString strErr=s;
return Logging(strErr);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -