Commit 7ef14a28 authored by captainwong's avatar captainwong

add macro observer

parent d9aa7362
...@@ -28,13 +28,27 @@ private: ...@@ -28,13 +28,27 @@ private:
class CLock class CLock
{ {
public: public:
CLock() /*CLock(PCRITICAL_SECTION cs) : m_cs(cs), bNullInit(FALSE)
{ {
if (m_cs == NULL) {
bNullInit = TRUE;
m_cs = new CRITICAL_SECTION();
}
InitializeCriticalSection(m_cs);
}*/
CLock()/* : m_cs(NULL), bNullInit(FALSE)*/
{
/*if (m_cs == NULL) {
m_cs = new CRITICAL_SECTION();
}*/
InitializeCriticalSection(&m_cs); InitializeCriticalSection(&m_cs);
} }
~CLock() ~CLock()
{ {
DeleteCriticalSection(&m_cs); DeleteCriticalSection(&m_cs);
/*if (bNullInit) {
delete m_cs;
}*/
} }
void Lock() void Lock()
{ {
...@@ -44,8 +58,11 @@ public: ...@@ -44,8 +58,11 @@ public:
{ {
LeaveCriticalSection(&m_cs); LeaveCriticalSection(&m_cs);
} }
const LPCRITICAL_SECTION GetObject() { return &m_cs; }
private: private:
CRITICAL_SECTION m_cs; CRITICAL_SECTION m_cs;
//BOOL bNullInit;
}; };
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#pragma once #pragma once
#endif // _MSC_VER > 1000 #endif // _MSC_VER > 1000
#include <assert.h>
//#include "dbghlpapi.h" //#include "dbghlpapi.h"
#ifdef TRACE #ifdef TRACE
...@@ -33,7 +35,8 @@ ...@@ -33,7 +35,8 @@
CRITICAL_SECTION CLog::m_cs;\ CRITICAL_SECTION CLog::m_cs;\
CLock CLog::m_lockForSingleton;\ CLock CLog::m_lockForSingleton;\
char CLog::m_ansiBuf[MAX_OUTPUT_LEN];\ char CLog::m_ansiBuf[MAX_OUTPUT_LEN];\
wchar_t CLog::m_utf16Buf[MAX_OUTPUT_LEN]; wchar_t CLog::m_utf16Buf[MAX_OUTPUT_LEN];\
CLog* CLog::m_pInstance = NULL;
/* /*
#define TRACEFUNCNAME \ #define TRACEFUNCNAME \
...@@ -66,12 +69,13 @@ private: ...@@ -66,12 +69,13 @@ private:
static CLock m_lockForSingleton; static CLock m_lockForSingleton;
static char m_ansiBuf[MAX_OUTPUT_LEN]; static char m_ansiBuf[MAX_OUTPUT_LEN];
static wchar_t m_utf16Buf[MAX_OUTPUT_LEN]; static wchar_t m_utf16Buf[MAX_OUTPUT_LEN];
static CLog* m_pInstance;
public: public:
static CString GetPrivateLogPath(){ static const wchar_t* GetPrivateLogPath(){
static CString strPrivateMapFolder = _T(""); static wchar_t strPrivateMapFolder[1024] = { 0 };
static BOOL b = TRUE; static BOOL b = TRUE;
if(b){ if(b){
strPrivateMapFolder.Format(_T("%s\\Log"), GetModuleFilePath()); wsprintf(strPrivateMapFolder, _T("%s\\Log"), GetModuleFilePath());
b = FALSE; b = FALSE;
} }
return strPrivateMapFolder; return strPrivateMapFolder;
...@@ -79,9 +83,12 @@ public: ...@@ -79,9 +83,12 @@ public:
static CLog* GetInstance(){ static CLog* GetInstance(){
m_lockForSingleton.Lock(); m_lockForSingleton.Lock();
static CLog log; if (CLog::m_pInstance == NULL) {
static CLog log;
CLog::m_pInstance = &log;
}
m_lockForSingleton.UnLock(); m_lockForSingleton.UnLock();
return &log; return CLog::m_pInstance;
} }
~CLog(){ ~CLog(){
...@@ -148,8 +155,7 @@ public: ...@@ -148,8 +155,7 @@ public:
} }
} }
catch(...){ catch(...){
AfxMessageBox(_T("CLog::WriteLog")); assert(0);
ASSERT(0);
} }
} }
...@@ -190,8 +196,7 @@ public: ...@@ -190,8 +196,7 @@ public:
} }
} }
catch(...){ catch(...){
AfxMessageBox(_T("CLog::WriteLog")); assert(0);
ASSERT(0);
} }
} }
...@@ -235,8 +240,7 @@ public: ...@@ -235,8 +240,7 @@ public:
} }
} }
catch(...){ catch(...){
AfxMessageBox(_T("CLog::WriteLog")); assert(0);
ASSERT(0);
} }
} }
...@@ -361,14 +365,16 @@ protected: ...@@ -361,14 +365,16 @@ protected:
if(pfile == NULL){ if(pfile == NULL){
_tfopen_s(&pfile, g_szFileName, _T("wb")); _tfopen_s(&pfile, g_szFileName, _T("wb"));
if(pfile == NULL){ if(pfile == NULL){
AfxMessageBox(_T("Create log file failed.")); MessageBox(NULL, _T("Create log file failed."), NULL, 0);
ASSERT(0);
return NULL; return NULL;
} }
}else{ }else{
fclose(pfile); fclose(pfile);
_tfopen_s(&pfile, g_szFileName, _T("ab")); _tfopen_s(&pfile, g_szFileName, _T("ab"));
if(pfile == NULL){ if(pfile == NULL){
AfxMessageBox(_T("Open log file failed.")); MessageBox(NULL, _T("Open log file failed."), NULL, 0);
ASSERT(0);
return NULL; return NULL;
} }
} }
...@@ -380,11 +386,15 @@ protected: ...@@ -380,11 +386,15 @@ protected:
void CreateFileName(){ void CreateFileName(){
SYSTEMTIME st; SYSTEMTIME st;
GetLocalTime(&st); GetLocalTime(&st);
CString path = GetPrivateLogPath(); const wchar_t* path = GetPrivateLogPath();
CreateDirectory(path, NULL); CreateDirectory(path, NULL);
wsprintf(g_szFileName, _T("%s\\%04d-%02d-%02d-w%d-%02d-%02d-%02d"), path, wchar_t exe[1024] = { 0 };
st.wYear, st.wMonth, st.wDay, (st.wDayOfWeek != 0) ? st.wDayOfWeek : 7, GetModuleFileName(NULL, exe, 1023);
st.wHour, st.wMinute, st.wSecond); wsprintf(g_szFileName, _T("%s\\%s-%04d-%02d-%02d-w%d-%02d-%02d-%02d"),
path, CFileOper::GetFileNameFromPathName(exe),
st.wYear, st.wMonth, st.wDay,
(st.wDayOfWeek != 0) ? st.wDayOfWeek : 7,
st.wHour, st.wMinute, st.wSecond);
lstrcat(g_szFileName, _T(".log")); lstrcat(g_szFileName, _T(".log"));
} }
......
// place this macro in your class's header file, in your class's definition
#define DECLARE_OBSERVER(callback, param_type) \
protected: \
typedef struct callback##Info { \
DECLARE_UNCOPYABLE(callback##Info) \
public: \
callback##Info() : _udata(NULL), _on_result(NULL) {} \
callback##Info(void* udata, callback on_result) : _udata(udata), _on_result(on_result) {} \
void* _udata; \
callback _on_result; \
}callback##Info; \
typedef callback##Info _callbackInfo; \
typedef callback _callback; \
typedef param_type _param_type; \
std::list<_callbackInfo *> _observerList; \
public: \
void RegisterObserver(void* udata, callback cb); \
void UnRegisterObserver(void* udata); \
void NotifyObservers(_param_type param);
// place this macro in your class's cpp file
#define IMPLEMENT_OBSERVER(class_name) \
void class_name::RegisterObserver(void* udata, _callback cb) \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo* observer = *iter; \
if (observer->_udata == udata) { \
return; \
} \
iter++; \
} \
_callbackInfo *observer = new _callbackInfo(udata, cb); \
_observerList.insert(iter, observer); \
} \
void class_name::UnRegisterObserver(void* udata) \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo* observer = *iter; \
if (observer->_udata == udata) { \
delete observer; \
_observerList.erase(iter); \
break; \
} \
iter++; \
} \
} \
void class_name::NotifyObservers(_param_type param) \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo * observer = *iter++; \
observer->_on_result(observer->_udata, param); \
} \
}
// place this macro in your class's destruct function.
#define DESTROY_OBSERVER \
{ \
std::list<_callbackInfo *>::iterator iter = _observerList.begin(); \
while (iter != _observerList.end()) { \
_callbackInfo * observer = *iter++; \
delete observer; \
} \
_observerList.clear(); \
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment