输出调试信息

输出调试信息的方法

(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值