📄 aftrace.h
字号:
// AFTrace.h: interface for the AFTrace class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_AFTRACE_H__AA3A4C41_6914_46CC_960F_ACCEA8DFAD5A__INCLUDED_)
#define AFX_AFTRACE_H__AA3A4C41_6914_46CC_960F_ACCEA8DFAD5A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma warning (disable:4786)
#include "string"
#include "list"
using namespace std;
class AFTrace;
inline AFTrace & endl(AFTrace & t);
class AFTrace
{
typedef AFTrace & (*PFNAFTRACEONEPARAM) (AFTrace & );
typedef void (*PFNWRITEMSGCALLBACK) (char * szText, int nLen );
public:
AFTrace(const char * sAppName ,unsigned int nWriteMode = AFTrace::console);
AFTrace();
void Create(const char * sAppName = "AFTrace",unsigned int nWriteMode = AFTrace::console);
virtual ~AFTrace();
inline AFTrace & operator << (const char * val);
inline AFTrace & operator << (const int val);
inline AFTrace & operator << (const unsigned int val);
inline AFTrace & operator << (const long val);
inline AFTrace & operator << (const unsigned long val);
inline AFTrace & operator << (const __int64 val);
inline AFTrace & operator << (PFNAFTRACEONEPARAM val);
inline AFTrace & endl();
void SetWriteMsgCallBack(PFNWRITEMSGCALLBACK pfn = NULL) {
m_pfnCallBack = pfn;
};
inline void SetAppName(const char * szApp);
inline CString GetAppName();
int out(const char * sz,...);
enum WriteMode {
console = 0x00000001,
file = 0x00000002,
linenum = 0x00000004,
nohead = 0x00000008,
hitime = 0x00000010,
ownfile = 0x00000020
};
virtual void WriteMessage(string * s,int nLen);
protected:
void SetMode(unsigned int dwWriteMode = console);
void RenameFile();
inline void flush();
inline void Write(const char * sz);
inline void MakeHead(string * & s);
void WriteToList(string * s);
static list<string * > m_listText;
static list<AFTrace *> m_listTrace;
static HANDLE m_hEventWait;
static CRITICAL_SECTION m_csWriteEvent;
static HANDLE m_hWriteThread;
static HANDLE m_hOnlyFile;
static HANDLE m_hConsole;
static string m_sFilePath;
static string m_sFileName;
static DWORD m_dwMaxFileSize;
int m_nMaxLines;
char m_sAppName[20];
static DWORD WINAPI WriteThreadProc(void * p);
static int m_nRefCount;
static __int64 m_i64TickCountFrequence;
bool m_bNewLine;
unsigned int m_dwWriteMode;
unsigned long m_nLineNumber;
string * m_sText;
HANDLE m_hFile;
static PFNWRITEMSGCALLBACK m_pfnCallBack;
};
AFTrace & AFTrace::operator << (PFNAFTRACEONEPARAM val)
{
return val(*this);
}
AFTrace & endl(AFTrace & t)
{
return t.endl();
}
AFTrace & AFTrace::endl()
{
flush();
return *this;
}
void AFTrace::flush()
{
m_bNewLine = true;
if (!m_sText || m_sText->empty())
return ;
WriteToList(m_sText);
m_sText = NULL;
}
AFTrace & AFTrace::operator << (const char * val)
{
Write(val);
return *this;
}
AFTrace & AFTrace::operator << (const int val)
{
char buf[32];
itoa(val,buf,10);
Write(buf);
return *this;
}
AFTrace & AFTrace::operator << (const unsigned int val)
{
char buf[32];
itoa(val,buf,10);
Write(buf);
return *this;
}
AFTrace & AFTrace::operator << (const long val)
{
char buf[32];
ltoa(val,buf,10);
Write(buf);
return *this;
}
AFTrace & AFTrace::operator << (const unsigned long val)
{
char buf[32];
ultoa(val,buf,10);
Write(buf);
return *this;
}
AFTrace & AFTrace::operator << (const __int64 val)
{
char buf[64];
_i64toa(val,buf,10);
Write(buf);
return *this;
}
void AFTrace::Write(const char * sz)
{
if (m_bNewLine)
{
m_bNewLine = false;
MakeHead(m_sText);
}
*m_sText += sz;
}
CString AFTrace::GetAppName()
{
return CString(m_sAppName);
}
void AFTrace::SetAppName(const char * szApp)
{
memset(m_sAppName,0,20);
if (szApp)
{
memcpy(m_sAppName,szApp,min(20,strlen(szApp)));
}
else
memcpy(m_sAppName,"AFTrace",7);
}
void AFTrace::MakeHead(string * & ps)
{
ps = new string ;
string & s = *ps;
if (m_dwWriteMode & nohead)
return ;
s.resize(128);
SYSTEMTIME tm;
GetLocalTime(&tm);
__int64 i64TickCount = tm.wMilliseconds;
if (m_i64TickCountFrequence!=1000)
QueryPerformanceCounter((LARGE_INTEGER *)&i64TickCount);
i64TickCount = i64TickCount % m_i64TickCountFrequence;
int nChars;
if (m_dwWriteMode & hitime)
{
if (m_dwWriteMode & linenum)
nChars = sprintf(s.begin(),"%8u %04d/%02d/%02d %02d:%02d:%02d:%08I64d %s ",m_nLineNumber,tm.wYear,tm.wMonth,
tm.wDay,tm.wHour,tm.wMinute,tm.wSecond,i64TickCount,m_sAppName);
else
nChars = sprintf(s.begin(),"%04d/%02d/%02d %02d:%02d:%02d:%08I64d %s ",tm.wYear,tm.wMonth,
tm.wDay,tm.wHour,tm.wMinute,tm.wSecond,i64TickCount,m_sAppName);
}
else
{
if (m_dwWriteMode & linenum)
nChars = sprintf(s.begin(),"%8u %04d/%02d/%02d %02d:%02d:%02d:%03d %s ",m_nLineNumber,tm.wYear,tm.wMonth,
tm.wDay,tm.wHour,tm.wMinute,tm.wSecond,tm.wMilliseconds,m_sAppName);
else
nChars = sprintf(s.begin(),"%04d-%02d-%02d %02d:%02d:%02d:%03d %s ",tm.wYear,tm.wMonth,
tm.wDay,tm.wHour,tm.wMinute,tm.wSecond,tm.wMilliseconds,m_sAppName);
}
s.resize(nChars);
}
#endif // !defined(AFX_AFTRACE_H__AA3A4C41_6914_46CC_960F_ACCEA8DFAD5A__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -