📄 sh_log.cpp
字号:
// SH_Log.cpp: implementation of the SH_Log class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SH_Log.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include "SH_File.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SH_Log::SH_Log()
{
m_iKind = LOG_TO_ALL;
m_pBuffer = new char[1024];
ASSERT( m_pBuffer != NULL);
}
SH_Log::SH_Log(int iKind,BOOL bShowTime/* = FALSE*/)
{
m_iKind = iKind;
m_pBuffer = new char[1024];
ASSERT( m_pBuffer != NULL);
}
SH_Log::~SH_Log()
{
SAFE_DELETE_ARRAY(m_pBuffer);
}
void SH_Log::Write(LPCTSTR fmt,...)
{
va_list va;
ASSERT(m_pBuffer != NULL);
if(m_bEnabled)
{
m_pBuffer[0] = 0;
va_start(va,fmt);
vsprintf(m_pBuffer,fmt,va);
va_end(va);
GetCurrentTime();
if(m_iKind & LOG_TO_OUTPUT) WriteToOutput(m_pBuffer);
if(m_iKind & LOG_TO_FILE) WriteToFile(m_pBuffer);
if(m_iKind & LOG_TO_NET) WriteToNet(m_pBuffer);
}
}
void SH_Log::SetLogFile(LPCTSTR cszFile)
{
m_strLogFile = cszFile;
}
void SH_Log::SetLogKind(int iKind)
{
m_iKind = iKind;
}
LPCTSTR SH_Log::GetLogFile()
{
return (LPCTSTR)m_strLogFile;
}
int SH_Log::GetKind()
{
return m_iKind;
}
void SH_Log::Enable(BOOL bEnabled)
{
m_bEnabled = bEnabled;
}
void SH_Log::ShowTime(BOOL bShow)
{
m_bShowTime = bShow;
}
LPCTSTR SH_Log::GetCurrentTime()
{
time_t now;
now = time(NULL);
m_strCurrent = ctime(&now);
m_strCurrent.TrimRight("\n");
m_strCurrent += ":";
return (LPCTSTR)m_strCurrent;
}
void SH_Log::WriteToOutput(LPCTSTR cszInfo)
{
if(m_bShowTime)
OutputDebugString(m_strCurrent);
OutputDebugString(cszInfo);
}
void SH_Log::WriteToFile(LPCTSTR cszInfo)
{
SH_File fp;
ASSERT(m_strLogFile.GetLength() > 0 );
fp.Open(GetLogFile(),"a+t");
if(fp.Open(GetLogFile(),"a+t"))
{
if(m_bShowTime)
fp.WriteString((LPCTSTR)m_strCurrent);
fp.WriteString(cszInfo);
fp.Flush();
fp.Close();
}
else
{
TRACE("Fail to write log information to file %s\n",GetLogFile());
ASSERT(FALSE);
}
}
void SH_Log::WriteToNet(LPCTSTR cszInfo)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -