📄 debug.h
字号:
#if !defined( __CYH_DEBUG_H__ )
#define __CYH_DEBUG_H__
/*
\author Hannosset Christophe
\author c.hannosset@ainenn.org
\version 1.2
\date 01 - 01 - 2003
\brief collection of various way provided to debug, trace and/or check for leaks.
\todo Creating a flag that would allow at least during development to generate cortrectly a full log system information
\bug
\warning
*/
#include <assert.h>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#ifdef _DEBUG
# ifdef WIN32
# pragma warning( disable : 4996 )
# ifdef Platform_SDK
# include <windows.h>
# else
# include <time.h>
# endif
# include <crtdbg.h>
# define _CRTDBG_MAP_ALLOC
# define DEBUG_CLIENTBLOCK new( 1 , __FILE__, __LINE__)
# define new DEBUG_CLIENTBLOCK
# define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define calloc(c, s) _calloc_dbg(c, s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define realloc(p, s) _realloc_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define _expand(p, s) _expand_dbg(p, s, _NORMAL_BLOCK, __FILE__, __LINE__)
# define free(p) _free_dbg(p, _NORMAL_BLOCK)
# define _msize(p) _msize_dbg(p, _NORMAL_BLOCK)
# define CHECK_MEM assert( _CrtCheckMemory() == TRUE );
# define BREAK DebugBreak()
/*! Force the creation of a leak as ref point - another way to find leaks! */
# define MARK_LEAK new char[1]
# define INIT_DBG_MEM { int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); /* Get current flag */ \
tmpFlag |= _CRTDBG_LEAK_CHECK_DF; /* Turn on leak-checking bit */ \
tmpFlag &= ~_CRTDBG_CHECK_CRT_DF; /* Turn off CRT block checking bit */ \
_CrtSetDbgFlag( tmpFlag ); /* Set flag to the new value */ \
assert( _CrtCheckMemory() != 0 ); \
}
# endif
# include <stdarg.h>
extern ofstream tron;
/*! The trace class is for outputting informational strings to std::cerr
* In non-debug builds, Trace will do nothing (it's #defined to nothing)
*/
class BqTrace
{
public:
/*! Standard constructor
* \param sMessage The message to print ot std::cerr
*/
BqTrace( const std::string sMessage )
{
tron << "Trace: " << sMessage << std::endl;
tron.flush();
#ifdef Platform_SDK
OutputDebugString( sMessage.c_str() );
OutputDebugString( "\n" );
#endif
}
/*! Redundant of the standard constructor
* \param sMessage The message to print ot std::cerr
*/
BqTrace( char *sMessage = "" )
{
tron << "Trace: " << sMessage << std::endl;
tron.flush();
#ifdef Platform_SDK
OutputDebugString( sMessage );
OutputDebugString( "\n" );
#endif
}
/*! Smart constructor
* \param sMessage The message to print ot std::cerr under the form of a printf
*/
BqTrace( const char *fmt , ... )
{
static char buf[8192];
va_list ap;
va_start( ap, fmt );
vsprintf( buf, fmt, ap );
#ifdef Platform_SDK
OutputDebugString( buf );
OutputDebugString( "\n" );
#endif
tron << "Trace: " << buf << std::endl;
tron.flush();
va_end( ap );
}
/*! Standard destructor */
~BqTrace(void) { }
};
/*! TraceIf will output sMessage to std::cerr if bTest is true
* In non-debug builds, TraceIf will do nothing (it's #defined to nothing)
*/
class TraceIf
{
public:
/*! Standard constructor
* \param bTest The message will only be outputted if this is true
* \param sMessage The message to print ot std::cerr
*/
TraceIf(bool bTest, const std::string sMessage)
{
if (bTest)
{
tron << "Trace: " << sMessage << std::endl;
}
}
/*! Standard destructor */
~TraceIf(void) { }
};
/*! Compute the effective effisciency, works only in window... */
class PfmtMeasure {
public:
const string lbl , file;
# ifdef Platform_SDK
__int64 t0, t1, f;
# else
long t0, t1, f;
# endif
PfmtMeasure( const string albl = "Measure Performance of " , const string aFile = "" )
: lbl( albl ) , file( aFile ) , t0( 0 ) , t1( 0 ) , f( 1 )
{
Start();
}
void Start( void )
{
t1 = 0;
f = 1;
# ifdef Platform_SDK
QueryPerformanceCounter( (LARGE_INTEGER *) &t0 );
# else
t0 = (int)clock(); // SDL_GetTicks();
# endif
}
void Stop( void )
{
# ifdef Platform_SDK
QueryPerformanceCounter( (LARGE_INTEGER *) &t1 );
QueryPerformanceFrequency( (LARGE_INTEGER *) &f );
# else
t1 = (int)clock(); // SDL_GetTicks();
# endif
}
friend ostream &operator <<( ostream &of , PfmtMeasure &pm )
{
if( pm.t1 == 0 )
pm.Stop();
of << pm.lbl << pm.file << " : total time = " << (double)( pm.t1 - pm.t0 ) * 1000.0 / (double) pm.f << " msec\n";
pm.Start();
return of;
}
};
# define SET_TRACE_MEASURE( var , lbl ) PfmtMeasure var( lbl , __FILE__ ); var.Start()
# define TRACE_MEASURE( var , lbl ) var.Stop(); tron << lbl << " :\t" << var; var.Start();
/*! DEBUG_ONLY is a macro for marking code as only being included in DEBUG builds */
# ifndef DEBUG_ONLY
# define DEBUG_ONLY(s) s
# endif
#else // DEBUG
class BqTrace {
public:
BqTrace( const char *fmt , ... ) {}
BqTrace( const std::string sMessage) {}
BqTrace( char *sMessage = "" ) {}
};
# define TraceIf(b, s)
# define DEBUG_ONLY(s)
# define SET_TRACE_MEASURE( var , lbl )
# define TRACE_MEASURE( var , lbl )
#endif // DEBUG
#ifndef CHECK_MEM
# define CHECK_MEM
#endif
#ifndef BREAK
# define BREAK
#endif
#ifndef MARK_LEAK
# define MARK_LEAK
#endif
#ifndef INIT_DBG_MEM
# define INIT_DBG_MEM
#endif
//#define TRACE_MSG
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -