Add logger.h and locker.h

This commit is contained in:
yuanyuanxiang
2025-05-09 03:09:25 +08:00
parent a21706f350
commit 82e09d945d
5 changed files with 379 additions and 176 deletions

View File

@@ -24,12 +24,6 @@
#pragma once
#endif // _MSC_VER > 1000
#ifdef _DEBUG
#define Mprintf(format, ...) printf(format, ##__VA_ARGS__)
#else
#define Mprintf(format, ...)
#endif
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>й©<D0B9><C2A9><EFBFBD>谲װVLD<4C><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD>
#include "vld.h"
@@ -49,50 +43,6 @@
#include <MMSystem.h>
#pragma comment(lib, "winmm.lib")
// <20>߾<EFBFBD><DFBE>ȵ<EFBFBD>˯<EFBFBD>ߺ<EFBFBD><DFBA><EFBFBD>
#define Sleep_m(ms) { timeBeginPeriod(1); Sleep(ms); timeEndPeriod(1); }
// <20>Բ<EFBFBD><D4B2><EFBFBD>n<EFBFBD><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD>µȴ<C2B5>T<EFBFBD><54>(n<>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1000)
#define WAIT_n(C, T, n) {assert(!(1000%(n)));int s=(1000*(T))/(n);do{Sleep(n);}while((C)&&(--s));}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD>ȴ<EFBFBD>T<EFBFBD><54>(<28><><EFBFBD><EFBFBD>10ms)
#define WAIT(C, T) { timeBeginPeriod(1); WAIT_n(C, T, 10); timeEndPeriod(1); }
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>ʱ<EFBFBD>ȴ<EFBFBD>T<EFBFBD><54>(<28><><EFBFBD><EFBFBD>1ms)
#define WAIT_1(C, T) { timeBeginPeriod(1); WAIT_n(C, T, 1); timeEndPeriod(1); }
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
// <20><><EFBFBD>ܼ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3BAAF><EFBFBD>ĺ<EFBFBD>ʱ
class auto_tick
{
private:
const char *func;
int span;
clock_t tick;
__inline clock_t now() const { return clock(); }
__inline int time() const { return now() - tick; }
public:
auto_tick(const char *func_name, int th = 5) : func(func_name), span(th), tick(now()) { }
~auto_tick() { stop(); }
__inline void stop() {
if (span != 0) { int s(this->time()); if (s > span)Mprintf("[%s]ִ<><D6B4>ʱ<EFBFBD><CAB1>: [%d]ms.\n", func, s); span = 0; }
}
};
#ifdef _DEBUG
// <20><><EFBFBD>ܼ<EFBFBD><DCBC>㵱ǰ<E3B5B1><C7B0><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ӡ
#define AUTO_TICK(thresh) auto_tick TICK(__FUNCTION__, thresh)
#define STOP_TICK TICK.stop()
#else
#define AUTO_TICK(thresh)
#define STOP_TICK
#endif
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) if(NULL !=(p)){ delete (p);(p) = NULL;}
#endif
@@ -105,42 +55,5 @@ public:
#define SAFE_DELETE_AR(p) if(NULL !=(p)){ delete[] (p);(p) = NULL;}
#endif
class CLock
{
private:
CRITICAL_SECTION m_cs;
public:
CLock()
{
InitializeCriticalSection(&m_cs);
}
~CLock()
{
DeleteCriticalSection(&m_cs);
}
void Unlock()
{
LeaveCriticalSection(&m_cs);
}
void Lock()
{
EnterCriticalSection(&m_cs);
}
};
class CAutoLock
{
private:
CRITICAL_SECTION &m_cs;
public:
CAutoLock(CRITICAL_SECTION& cs) : m_cs(cs)
{
EnterCriticalSection(&m_cs);
}
~CAutoLock()
{
LeaveCriticalSection(&m_cs);
}
};
#include "common/logger.h"
#include "common/locker.h"