13 friend void SetTraceFilePrefix(LPCTSTR strFilePrefix);
14 friend std::string GetTraceFilePrefix();
15 friend void SetTraceLevel(
const int nLevel);
16 friend int GetTraceLevel();
17 friend void Std_Log(
const int nLevel,
bool bPrintHeader, LPCTSTR strFormat, ...);
18 friend void Std_ResetLog();
21 std::string m_strFilename;
25 long long m_nThreadId;
30 std::string m_strTraceFilePrefix;
31 SYSTEMTIME m_timeStart;
36 if(m_hFile) ::CloseHandle(m_hFile);
40 HANDLE OpenTraceFile()
43 TCHAR strFilePath[1001];
44 std::string strPrefix;
46 ::GetLocalTime(&sysTime);
48 if(m_strTraceFilePrefix.length())
49 strPrefix = m_strTraceFilePrefix;
56 _T(
"%s_%04d%02d%02d_%02d%02d%02d_%X.txt"),
64 ::GetCurrentProcessId()
67 m_strFilename = strFilePath;
77 FILE_ATTRIBUTE_NORMAL,
81 if(m_hFile) m_timeStart = sysTime;
95 nThreadId = ::GetCurrentThreadId();
96 while(m_nThreadId!=nThreadId)
99 #if _MSC_VER > 1300 // VC 7
100 ::InterlockedCompareExchangePointer((
void**)&m_nThreadId, (
void*)nThreadId, 0);
102 ::InterlockedCompareExchange((
void**)&m_nThreadId, (
void*)nThreadId, 0);
105 if(m_nThreadId==nThreadId)
break;
114 #if _MSC_VER > 1300 // VC 7
115 ::InterlockedCompareExchangePointer((
void**)&m_nThreadId, 0, (
void*)::GetCurrentThreadId());
117 ::InterlockedCompareExchange((
void**)&m_nThreadId, 0, (
void*)::GetCurrentThreadId());
121 void SetTraceLevel(
const int nLevel) { m_nLevel = nLevel>0?nLevel:0; }
122 int GetTraceLevel() {
return m_nLevel; }
125 void SetTraceFilePrefix(LPCTSTR strFilePrefix)
129 m_strTraceFilePrefix = strFilePrefix;
136 m_nLevel = TraceDetail;
146 XYTraceHelper theHelper;
148 void SetTraceFilePrefix(LPCTSTR strFilePrefix)
153 theHelper.SetTraceFilePrefix(strFilePrefix);
158 std::string GetTraceFilePrefix()
160 return theHelper.m_strTraceFilePrefix;
163 void SetTraceLevel(
const int nLevel)
168 theHelper.SetTraceLevel(nLevel);
174 {
return theHelper.GetTraceLevel();}
176 std::string GetLevel(
const int nLevel)
201 void STD_UTILS_PORT Std_ResetLog()
205 if(theHelper.m_hFile)
207 theHelper.CloseTraceFile();
208 if(theHelper.m_strFilename.length())
209 DeleteFile((LPCSTR) theHelper.m_strFilename.c_str());
227 void STD_UTILS_PORT Std_Log(
const int nLevel,
bool bPrintHeader, LPCTSTR strFormat, ...)
231 if(theHelper.m_nLevel==0||nLevel>theHelper.m_nLevel)
return;
238 ::GetLocalTime(&sysTime);
240 HANDLE hFile = theHelper.m_hFile;
242 if(hFile==NULL) hFile = theHelper.OpenTraceFile();
247 sysTime.wYear!=theHelper.m_timeStart.wYear||
248 sysTime.wMonth!=theHelper.m_timeStart.wMonth||
249 sysTime.wDay!=theHelper.m_timeStart.wDay)
251 theHelper.CloseTraceFile();
252 theHelper.OpenTraceFile();
255 std::string strLevel = GetLevel(nLevel);
261 const int nMaxSize = 32*1024;
262 TCHAR pBuffer[nMaxSize+51];
271 _T(
"[%s %02d/%02d/%04d %02d:%02d:%02d_%03d_%X] "),
279 sysTime.wMilliseconds,
280 theHelper.m_nThreadId
286 va_start(args, strFormat);
287 nPos += _vsntprintf(pBuffer+nPos,nMaxSize,strFormat,args);
290 _stprintf(pBuffer+nPos,_T(
"\r\n"));
293 ::WriteFile(hFile,pBuffer,_tcslen(pBuffer),&dwBytes,NULL);
Namespace for the standard utility objects.