log.h更加高级的CNT_TIME

This commit is contained in:
xinyang
2019-08-07 02:36:01 +08:00
parent 05b7991d57
commit 19c07c441d

View File

@@ -6,6 +6,7 @@
#define _LOG_H_ #define _LOG_H_
#include <stdio.h> #include <stdio.h>
#include <systime.h>
/************** Define the control code *************/ /************** Define the control code *************/
#define START_CTR "\033[0" #define START_CTR "\033[0"
@@ -130,28 +131,22 @@
/******************** the time counter API ************************/ /******************** the time counter API ************************/
#if !defined(DO_NOT_CNT_TIME) && LOG_LEVEL > LOG_NONE #if !defined(DO_NOT_CNT_TIME) && LOG_LEVEL > LOG_NONE
#ifdef Windows #define CNT_TIME(tag, codes, ...) do{ \
#include <Windows.h> class TimeCounter{ \
#define CNT_TIME(tag, codes, ...) do{ \ private: \
static SYSTEMTIME ts, te; \ systime begin; \
GetLocalTime(&ts); \ public: \
codes; \ TimeCounter(){ \
GetLocalTime(&te); \ getsystime(begin); \
LOGM(tag": %dms", (te.wSecond-ts.wSecond)*1000+(te.wMilliseconds-ts.wMilliseconds)); \ } \
}while (0) ~TimeCounter(){ \
#elif defined(Linux) systime end; \
#include <sys/time.h> getsystime(end); \
#define CNT_TIME(tag, codes, ...) do{ \ LOGM(tag": %.1lfms", ##__VA_ARGS__, getTimeIntervalms(end, begin)); \
static timeval ts, te; \ } \
gettimeofday(&ts, NULL); \ } time_cnt; \
codes; \ codes; \
gettimeofday(&te, NULL); \ }while (0)
LOGM(tag": %.1lfms", ##__VA_ARGS__, (te.tv_sec-ts.tv_sec)*1000.0+(te.tv_usec-ts.tv_usec)/1000.0); \
}while (0)
#else
#warning "Unsupport plantform for CNT_TIME"
#define CNT_TIME(tag, codes, ...) codes
#endif
#else #else
#define CNT_TIME(tag, codes, ...) codes #define CNT_TIME(tag, codes, ...) codes
#endif #endif