//MyDebug.h
#ifndef MYDEBUG_H_
#define MYDEBUG_H_
//define _MY_DEBUG_EXTERN
#ifdef _DBG_EXTERN
#undef _DBG_EXTERN
#endif
#ifndef __MY_DEBUG_IMPLEMENTATION
#define _DBG_EXTERN extern
#else
#define _DBG_EXTERN
#endif
//define _MY_DEBUG_LOG
#ifdef _DEBUG
#ifndef _MY_DEBUG_LOG
//在各个文件头上定义该符号
#define _MY_DEBUG_LOG
#endif
#else
#ifdef _MY_DEBUG_LOG
#undef _MY_DEBUG_LOG
#endif
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//define __DEBUG_LOG
#ifdef _MY_DEBUG_LOG
#ifndef __DEBUG_LOG
//#define __DEBUG_LOG
#endif
#else
#ifdef __DEBUG_LOG
#undef __DEBUG_LOG
#endif
#endif //_MY_DEBUG_LOG
//define __BUFF_DEBUG
#ifdef _MY_DEBUG_LOG
#ifndef __BUFF_DEBUG
//#define __BUFF_DEBUG
#endif
#else
#ifdef __BUFF_DEBUG
#undef __BUFF_DEBUG
#endif
#endif //_MY_DEBUG_LOG
//define __MEM_ALLOC_DEBUG
#ifdef _MY_DEBUG_LOG
#ifndef __MEM_ALLOC_DEBUG_
//#define __MEM_ALLOC_DEBUG_
#endif
#else
#ifdef __MEM_ALLOC_DEBUG_
#undef __MEM_ALLOC_DEBUG_
#endif
#endif //_MY_DEBUG_LOG
//define __DEBUG_ZONE
#ifdef _MY_DEBUG_LOG
#ifndef __DEBUG_ZONE
#define __DEBUG_ZONE
#endif
#else
#ifdef __DEBUG_ZONE
#undef __DEBUG_ZONE
#endif
#endif //_MY_DEBUG_LOG
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//define WRITELOG
#ifdef __DEBUG_LOG
void _write_debug_log(LPCTSTR fmt, ...);
#define WRITELOG _write_debug_log
#else
inline void _write_debug_log_null(LPCTSTR fmt, ...) { }
#define WRITELOG 1 ? (void)0 : _write_debug_log_null
#endif //__DEBUG_LOG
//define WRITEBUFF
#ifdef __BUFF_DEBUG
void WriteDebugLogBuf(const BYTE* buff, unsigned short len);
#define WRITELOGBUF(buff,len) WriteDebugLogBuf((buff),(len))
#else
#define WRITELOGBUF(buff,len)
#endif //__BUFF_DEBUG
//define MEMINFO
#ifdef __MEM_ALLOC_DEBUG_
void WriteDebugLogMemInfo();
#define WRITELOGMEMINFO() WriteDebugLogMemInfo()
#else
#define WRITELOGMEMINFO()
#endif`//__MEM_ALLOC_DEBUG_
//define DEBUGZONE_LOG, STARTDEBUGZONE, ENDDEBUGZONE, INITDEBUGZONEARRAY
#ifdef __DEBUG_ZONE
_DBG_EXTERN unsigned char gb_dbz_id_sheild[32];
void _debug_zone_log(unsigned char db_zone_id, LPCTSTR fmt, ...);
inline void _start_dbg_zone(unsigned char db_zone_id) { gb_dbz_id_sheild[db_zone_id/8] |= (0x01<<(db_zone_id%8)); }
inline void _end_dbg_zone(unsigned char db_zone_id) { gb_dbz_id_sheild[db_zone_id/8] &= ~(0x01<<(db_zone_id%8)); }
//void _start_dbg_zone(unsigned char db_zone_id);
//void _end_dbg_zone(unsigned char db_zone_id);
void _init_dbg_zone_array();
#define DEBUGZONE_LOG _debug_zone_log
#define STARTDEBUGZONE(db_zone) _start_dbg_zone((db_zone))
#define ENDDEBUGZONE(db_zone) _end_dbg_zone((db_zone))
#define INITDEBUGZONEARRAY() _init_dbg_zone_array()
#else
inline void _debug_zone_log_null(unsigned char db_zone_id, LPCTSTR fmt, ...) { }
#define DEBUGZONE_LOG 1 ? (void)0 : _write_debug_log_null
#define STARTDEBUGZONE(db_zone)
#define ENDDEBUGZONE(db_zone)
#define INITDEBUGZONEARRAY()
#endif //__DEBUG_ZONE
#endif /*MYDEBUG_H_*/
#ifndef __MY_DEBUG_IMPLEMENTATION
#define __MY_DEBUG_IMPLEMENTATION
#endif
#include <stdio.h>
#include <stdarg.h>
#include <windows.h>
#include <tchar.h>
#include "MyDebug.h"
#ifdef _MY_DEBUG_LOG
#define MAX_STR_LEN 1024
//void _trace(LPCTSTR fmt, ...)
//{
// //TCHAR out[1024];
// TCHAR* out="NULL";
// out=(TCHAR*)malloc(MAX_STR_LEN);
// if(out){
// memset(out, 0, sizeof(TCHAR)*MAX_STR_LEN);
// va_list body;
// va_start(body, fmt);
// _vstprintf(out, fmt, body); // 译注:格式化输入的字符串 fmtt
// va_end(body); // 到输出字符串 ou
// OutputDebugString(out); // 译注:输出格式化后的字符串到调试器
// }
//
// if(out){
// free(out);
// }
//}
#ifdef _DEBUG_LOG
const TCHAR* nmFileLog=_T("\\MyDebugLog.txt");
void _write_debug_log(LPCTSTR fmt, ...)
{
//TCHAR out[1024];
BOOL tmp_rst=TRUE;
TCHAR* out="NULL";
out=(TCHAR*)malloc(MAX_STR_LEN*sizeof(TCHAR));
if(!out){
tmp_rst=FALSE;
}
char* tstr="NULL";
if(tmp_rst){
tstr=(char*)malloc((MAX_STR_LEN+64)*sizeof(char));
if(!tstr){
tmp_rst=FALSE;
}
}
if(tmp_rst){
memset(out, 0, sizeof(TCHAR)*MAX_STR_LEN);
memset(tstr, 0, (MAX_STR_LEN+64)*sizeof(char));
va_list body;
va_start(body, fmt);
_vstprintf(out, fmt, body); // 译注:格式化输入的字符串 fmtt
va_end(body); // 到输出字符串 ou
//char tstr[1024]={0};
//HANDLE hFileLog="NULL";
FILE* pf;
DWORD sLen="0";
SYSTEMTIME sysTime;
size_t i;
::GetSystemTime(&sysTime);
sprintf(tstr,"Time>>%d-%d-%d %d:%d:%d\n ",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
sLen=strlen(tstr);
for(i=0;i<_tcslen(out);i++)
{
memcpy((void*)(tstr+i+sLen),(out+i),sizeof(char));
}
sLen=strlen(tstr);
memcpy((void*)(tstr+sLen),"\n",sizeof(char));
memset((void*)(tstr+sLen+1),0,((MAX_STR_LEN+64)*sizeof(char)-sLen-1)*sizeof(char));
sLen=strlen(tstr);
pf=_tfopen(nmFileLog,_T("a+t"));
if(pf!=NULL){
fseek(pf,1,SEEK_END);
fwrite(tstr,sLen+1,sizeof(char),pf);
fclose(pf);
}
}
if(out){
free(out);
}
if(tstr){
free(tstr);
}
}
#endif //_DEBUG_LOG
#ifdef __DEBUG_ZONE
const TCHAR* dbz_file_log_name=_T("\\MyDebugZoneLog.txt");
bool IsActiveDebugZone(unsigned char db_zone_id)
{
return (gb_dbz_id_sheild[db_zone_id/8]&(0x01<<(db_zone_id%8)))?true:false;
}
void _debug_zone_log(unsigned char db_zone_id, LPCTSTR fmt, ...)
{
//TCHAR out[1024];
BOOL tmp_rst=TRUE;
TCHAR* out="NULL";
char* tstr="NULL";
if(!IsActiveDebugZone(db_zone_id)){
tmp_rst=FALSE;
}
if(tmp_rst){
out=(TCHAR*)malloc(MAX_STR_LEN*sizeof(TCHAR));
if(!out){
tmp_rst=FALSE;
}
}
if(tmp_rst){
tstr=(char*)malloc((MAX_STR_LEN+64)*sizeof(char));
if(!tstr){
tmp_rst=FALSE;
}
}
if(tmp_rst){
memset(out, 0, sizeof(TCHAR)*MAX_STR_LEN);
memset(tstr, 0, (MAX_STR_LEN+64)*sizeof(char));
va_list body;
va_start(body, fmt);
_vstprintf(out, fmt, body); // 译注:格式化输入的字符串 fmtt
va_end(body); // 到输出字符串 ou
//char tstr[1024]={0};
//HANDLE hFileLog="NULL";
FILE* pf;
DWORD sLen="0";
SYSTEMTIME sysTime;
size_t i;
::GetSystemTime(&sysTime);
sprintf(tstr,"Time>>%d-%d-%d %d:%d:%d\n ",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
sLen=strlen(tstr);
for(i=0;i<_tcslen(out);i++)
{
memcpy((void*)(tstr+i+sLen),(out+i),sizeof(char));
}
sLen=strlen(tstr);
memcpy((void*)(tstr+sLen),"\n",sizeof(char));
memset((void*)(tstr+sLen+1),0,((MAX_STR_LEN+64)*sizeof(char)-sLen-1)*sizeof(char));
sLen=strlen(tstr);
pf=_tfopen(dbz_file_log_name,_T("a+t"));
if(pf!=NULL){
fseek(pf,1,SEEK_END);
fwrite(tstr,sLen+1,sizeof(char),pf);
fclose(pf);
}
}
if(out){
free(out);
}
if(tstr){
free(tstr);
}
}
//void _start_dbg_zone(unsigned char db_zone_id)
//{
// gb_dbz_id_sheild[db_zone_id/8] |= (0x01<<(db_zone_id%8));
//}
//
//void _end_dbg_zone(unsigned char db_zone_id)
//{
// gb_dbz_id_sheild[db_zone_id/8] &= ~(0x01<<(db_zone_id%8));
//}
void _init_dbg_zone_array()
{
memset(gb_dbz_id_sheild, 0, sizeof(gb_dbz_id_sheild));
STARTDEBUGZONE(0);
}
#endif //__DEBUG_ZONE
#ifdef __BUFF_DEBUG
void WriteDebugLogBuf( const BYTE* buff, unsigned short len )
{
CHAR tstr[1024]={0};
//HANDLE hFileLog="NULL";
FILE* pf;
DWORD sLen="0";
SYSTEMTIME sysTime;
DWORD tmpLen;
DWORD len_bk;
if(len>1000){
len=1000;
}
::GetSystemTime(&sysTime);
sprintf(tstr,"Time>>%d-%d-%d %d:%d:%d\n",sysTime.wYear,sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute,sysTime.wSecond);
sLen=strlen(tstr);
len_bk=len;
while(len>0){
tmpLen=(len>16)?16:len;
//tmpLen=len;
sprintf(tstr+sLen," >");
sLen=strlen(tstr);
for(unsigned short i="0";i {
//memcpy((void*)(tstr+i+sLen),(str+i),1);
sprintf(tstr+sLen,"0x%.2x ",buff[len_bk-len+i]);
sLen=strlen(tstr);
}
sLen=strlen(tstr);
memcpy((void*)(tstr+sLen),_T("\n"),sizeof(CHAR));
memset((void*)(tstr+sLen+1),0,(255-sLen)*sizeof(CHAR));
sLen=strlen(tstr);
pf=_tfopen(nmFileLog,_T("a+t"));
if(pf!=NULL){
fseek(pf,1,SEEK_END);
fwrite(tstr,sLen,sizeof(CHAR),pf);
fclose(pf);
}
memset(tstr,0,sizeof(tstr));
sLen=0;
len-=(unsigned short)tmpLen;
}
}
#endif //__BUFF_DEBUG
#ifdef __MEM_ALLOC_DEBUG_
void WriteDebugLogMemInfo()
{
MEMORYSTATUS mem_stru;
CHAR tstr[64]={0};
FILE* pf;
DWORD sLen="0";
mem_stru.dwLength=sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&mem_stru);
// __SPRINTF_DBG(gs_debug_info,_T(" 可用物理内存大小:0x%x\n 全部物理内存大小:0x%x\n 可用虚拟内存大小:0x%x\n 全部虚拟内存大小:0x%x\n 可用页文件数目:0x%x\n 全部页文件数目:0x%x\n 已经使用内存的百分比:%d%%\n "),
// mem_stru.dwAvailPhys,
// mem_stru.dwTotalPhys,
// mem_stru.dwAvailVirtual,
// mem_stru.dwTotalVirtual,
// mem_stru.dwAvailPageFile,
// mem_stru.dwTotalPageFile,
// mem_stru.dwMemoryLoad);
pf=_tfopen(nmFileLog,_T("a+t"));
if(NULL==pf){
return;
}
fseek(pf,1,SEEK_END);
sprintf(tstr," 可用物理内存大小:0x%x\n", mem_stru.dwAvailPhys);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
sprintf(tstr," 全部物理内存大小:0x%x\n", mem_stru.dwTotalPhys);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
sprintf(tstr," 可用虚拟内存大小:0x%x\n", mem_stru.dwAvailVirtual);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
sprintf(tstr," 全部虚拟内存大小:0x%x\n", mem_stru.dwTotalVirtual);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
sprintf(tstr," 可用页文件数目:0x%x\n", mem_stru.dwAvailPageFile);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
sprintf(tstr," 全部页文件数目:0x%x\n", mem_stru.dwTotalPageFile);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
sprintf(tstr," 已经使用内存的百分比:0x%x\n", mem_stru.dwMemoryLoad);
sLen=strlen(tstr);
fwrite(tstr,sLen,sizeof(CHAR),pf);
fclose(pf);
}
#endif //__MEM_ALLOC_DEBUG_
#endif //_MY_DEBUG_LOG
文章评论(0条评论)
登录后参与讨论