*
* File: Log.h
*
* @des: The file declare CLog class.
*
* @author yiyy 2009-3-17.
*
*/
#pragma once
// Log Error while the module is running.
class CLog
{
public:
static void Initialize(LPCSTR lpDir);
static void UnInitialize();
// Write Error.
// The error format is: [time]: caller--API name--Error Code.
static void Write(LPCSTR lpCaller, LPCTSTR lpCallee, const DWORD dwErrorCode);
static void Write(LPCSTR lpLog);
// Attribute.
private:
static LPCSTR LOG_FILE_NAME;
static HANDLE s_hLogFile;
static CRITICAL_SECTION s_CriticalSection;
};
// Callee 发生错误的函数名
// ErrorCode 错误码
#ifdef _GMT_DEBUG
#define GMT_LOG_ERR(Callee, ErrorCode)/
printf("%s--%s--%d/r/n", __FUNCTION__, #Callee, ErrorCode);
#else
#define GMT_LOG_ERR(Callee, ErrorCode)/
CLog::Write(__FUNCTION__, #Callee, ErrorCode);
#endif
// Callee 发生错误的函数名
// GetErrorFun 得到错误码的函数
#ifdef _GMT_DEBUG
#define GMT_LOG_FUN(Callee, GetErrorFun)/
printf("%s--%s--%d/r/n", __FUNCTION__, #Callee, ::GetErrorFun());
#else
#define GMT_LOG_FUN(Callee, GetErrorFun)/
CLog::Write(__FUNCTION__, #Callee, ::GetErrorFun());
#endif
// str 错误内容。
#ifdef _GMT_DEBUG
#define GMT_LOG_STR(str)/
printf("%s--%s/r/n", __FUNCTION__, str);
#else
#define GMT_LOG_STR(str)/
{/
char szBuff[BUFFER_SIZE_1024] = {0};/
sprintf_s(szBuff, sizeof(szBuff), "%s: %s", __FUNCTION__, str);/
CLog::Write(str);/
}
#endif
/*
* File: Log.cpp
*
* @des: The file define CLog class.
*
* @author yiyy 2009-3-17.
*
*/
#include "StdAfx.h"
#include <time.h>
#include "Log.h"
LPCSTR CLog::LOG_FILE_NAME = "Comm_module.log";
HANDLE CLog::s_hLogFile = NULL;
CRITICAL_SECTION CLog::s_CriticalSection;
void CLog::Initialize(LPCSTR lpDir)
{
GMT_ASSERT(NULL != lpDir);
InitializeCriticalSection(&s_CriticalSection);
// Try to open existing log file, if the file dose not exist, creat new one.
char szFileFullPath[MAX_PATH] = {0};
sprintf_s(szFileFullPath, sizeof(szFileFullPath), "%s%s", lpDir, LOG_FILE_NAME);
s_hLogFile = ::CreateFile(szFileFullPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (NULL == s_hLogFile)
return;
// moves the file pointer to end.
::SetFilePointer(s_hLogFile, 0, NULL,FILE_END);
}
void CLog::UnInitialize()
{
if (NULL != s_hLogFile)
::CloseHandle(s_hLogFile);
DeleteCriticalSection(&s_CriticalSection);
}
void CLog::Write(LPCSTR lpCaller, LPCSTR lpCallee, const DWORD dwErrorCode)
{
if (NULL == s_hLogFile)
return;
EnterCriticalSection(&s_CriticalSection);
GMT_ASSERT(NULL != lpCaller);
GMT_ASSERT(NULL != lpCallee);
DWORD dwWriteBytes = 0;
char szErrorLog[BUFFER_SIZE_1024] = {0};
time_t ltime;
time( <ime );
struct tm* ptm = _localtime64(<ime);
sprintf_s(szErrorLog, sizeof(szErrorLog), "[%02d:%02d:%02d]: %s--%s--%d/r/n",
ptm->tm_hour, ptm->tm_min, ptm->tm_sec, lpCaller, lpCallee, dwErrorCode);
::WriteFile(s_hLogFile, szErrorLog, strlen(szErrorLog), &dwWriteBytes, NULL);
LeaveCriticalSection(&s_CriticalSection);
}
void CLog::Write(LPCSTR lpLog)
{
if (NULL == s_hLogFile)
return;
EnterCriticalSection(&s_CriticalSection);
GMT_ASSERT(NULL != lpLog);
DWORD dwWriteBytes = 0;
char szErrorLog[BUFFER_SIZE_1024] = {0};
time_t ltime;
time( <ime );
struct tm* ptm = _localtime64(<ime);
sprintf_s(szErrorLog, sizeof(szErrorLog), "[%02d:%02d:%02d]: %s/r/n",
ptm->tm_hour, ptm->tm_min, ptm->tm_sec, lpLog);
::WriteFile(s_hLogFile, szErrorLog, strlen(szErrorLog), &dwWriteBytes, NULL);
LeaveCriticalSection(&s_CriticalSection);
}