输出调试信息的方法
(1)控制台
在调试程序的时候,可以将控制台打开,来输出调试信息。
有以下的好处:
1 不像窗口控件,依赖消息和重绘
2 程序飞掉了,控制台还在
3 方便的重定向到文本,可以用比较工具比较
打开控制台的函数为AllocConsole();
解释如下:
##########################################
##Parameters
This function has no parameters.
##Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
##Remarks
A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.
If the calling process creates a child process, the child inherits the new console.
AllocConsole initializes standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.
This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console. Console applications are initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).
##########################################
FILE *freopen(const char *filename,const char *type, FILE *stream);
它用来替换一个流,或者说重新分配文件指针,实现重定向。如果stream流已经打开,则先关闭该流。如果该流已经定向,则freopen将会清除该定向。此函数一般用于将一个指定的文件打开一个预定义的流:标准输入、标准输出或者标准出错。
(2)输出到调试器
输出到调试器的话,采用函数OutputDebugString
#include <windows.h>
void OutputDebugString(
LPCTSTR lpOutputString
);
调用的话会在调试器的输出窗口显示出消息。
OutputDebugString虽然可以输出信息,但更多的时候,希望像printf那样格式化输出信息。
这种时候可以利用宏来包一层函数
#ifdef _DEBUG
# define MyOutputDebugString( str, ... ) \
{ \
TCHAR c[256]; \
_stprintf( c, str, __VA_ARGS__ ); \
OutputDebugString( c ); \
}
#else
# define MyOutputDebugString( str, ... ) //Nothing
#endif
这样的话就可以下面这样
void TestFunc( D3DXVECTOR3 *inVec ) {
D3DXVECTOR3 tmp( inVec );
D3DXVec3Normalize( &tmp, &tmp );
MyOutputDebugString( _T("Vector( %f, %f, %f )\n"), tmp.x, tmp.y, tmp.z );
*inVec = tmp;
}
(1)控制台
在调试程序的时候,可以将控制台打开,来输出调试信息。
有以下的好处:
1 不像窗口控件,依赖消息和重绘
2 程序飞掉了,控制台还在
3 方便的重定向到文本,可以用比较工具比较
打开控制台的函数为AllocConsole();
解释如下:
##########################################
##Parameters
This function has no parameters.
##Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
##Remarks
A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.
If the calling process creates a child process, the child inherits the new console.
AllocConsole initializes standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.
This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console. Console applications are initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).
##########################################
相对应的,释放控制台的函数为BOOL WINAPI FreeConsole(void)
FILE *freopen(const char *filename,const char *type, FILE *stream);
它用来替换一个流,或者说重新分配文件指针,实现重定向。如果stream流已经打开,则先关闭该流。如果该流已经定向,则freopen将会清除该定向。此函数一般用于将一个指定的文件打开一个预定义的流:标准输入、标准输出或者标准出错。
(2)输出到调试器
输出到调试器的话,采用函数OutputDebugString
#include <windows.h>
void OutputDebugString(
LPCTSTR lpOutputString
);
调用的话会在调试器的输出窗口显示出消息。
OutputDebugString虽然可以输出信息,但更多的时候,希望像printf那样格式化输出信息。
这种时候可以利用宏来包一层函数
#ifdef _DEBUG
# define MyOutputDebugString( str, ... ) \
{ \
TCHAR c[256]; \
_stprintf( c, str, __VA_ARGS__ ); \
OutputDebugString( c ); \
}
#else
# define MyOutputDebugString( str, ... ) //Nothing
#endif
这样的话就可以下面这样
void TestFunc( D3DXVECTOR3 *inVec ) {
D3DXVECTOR3 tmp( inVec );
D3DXVec3Normalize( &tmp, &tmp );
MyOutputDebugString( _T("Vector( %f, %f, %f )\n"), tmp.x, tmp.y, tmp.z );
*inVec = tmp;
}