我们都知道MFC有个很有用的宏,叫TRACE,它用起来跟printf一样简单,能直接在VC++的output窗口中输出调试信息,但非MFC的project却没有这个宏,只能用OutputDebugString这个API输出,而OutputDebugString这个API却不支持类似printf这样的输出格式,而且不能在Release版本中自动关闭
(Reports warnings to an output device, such as the debugger windows, according to the indicated flags and levels
ATLTRACE2( exp ); ATLTRACE2( DWORD category, UINT level, LPCSTR lpszFormat, ... );
Parameters:
exp
[in] The string and variables to send to the Visual C++ output window or any application that traps these messages.
[in] Type of event or method on which to report. See the Remarks for a list of categories.
[in] The level of tracing to report. See the Remarks for details.
[in] The formatted string to send to the dump device.
Remarks :
The short form of ATLTRACE2 writes output to the debugger's output window. The second form of ATLTRACE2 also writes output to the debugger's output window, but is subject to the settings of the ATL/MFC Trace Tool (see ATLTraceTool Sample). For example, if you setlevel to 4 and the ATL/MFC Trace Tool to level 0, you will not see the message. level can be 0, 1, 2, 3, or 4. The default, 0, reports only the most serious problems.
The category parameter lists the trace flags to set. These flags correspond to the types of methods for which you want to report. The tables below list the valid trace flags you can use for the category parameter.
ATL Category | Description |
---|---|
atlTraceGeneral | Reports on all ATL applications. The default. |
atlTraceCOM | Reports on COM methods. |
atlTraceQI | Reports on QueryInterface calls. |
atlTraceRegistrar | Reports on the registration of objects. |
atlTraceRefcount | Reports on changing reference count. |
atlTraceWindowing | Reports on windows methods; for example, reports an invalid message map ID. |
atlTraceControls | Reports on controls; for example, reports when a control or its window is destroyed. |
atlTraceHosting | Reports hosting messages; for example, reports when a client in a container is activated. |
atlTraceDBClient | Reports on OLE DB Consumer Template; for example, when a call to GetData fails, the output can contain the HRESULT. |
atlTraceDBProvider | Reports on OLE DB Provider Template; for example, reports if the creation of a column failed. |
atlTraceSnapin | Reports for MMC SnapIn application. |
atlTraceNotImpl | Reports that the indicated function is not implemented. |
atlTraceAllocation | Reports messages printed by the memory debugging tools in atldbgmem.h. |
MFC Category | Description |
---|---|
traceAppMsg | General purpose, MFC messages. Always recommended. |
traceDumpContext | Messages from CDumpContext. |
traceWinMsg | Messages from MFC's message handling code. |
traceMemory | Messages from MFC's memory management code. |
traceCmdRouting | Messages from MFC's Windows command routing code. |
traceHtml | Messages from MFC's DHTML dialog support. |
traceSocket | Messages from MFC's socket support. |
traceOle | Messages from MFC's OLE support. |
traceDatabase | Messages from MFC's database support. |
traceInternet | Messages from MFC's Internet support. |
To declare a custom trace category, declare a global instance of the CTraceCategory class as follows:
Requirments
Header: atltrace.h
方法如下:
1.在MFC中加入TRACE语句
2.在TOOLS->MFC TRACER中选择 “ENABLE TRACING”点击OK
3.进行调试运行,GO(F5)(特别注意:不是执行‘!’以前之所以不能看到TRACE内容,是因为不是调试执行,而是‘!’了,切记,切记)
4.然后就会在OUTPUT中的DEBUG窗口中看到TRACE内容了,调试执行会自动从BUILD窗口跳到DEBUG窗口,在那里就看到TRACE的内容了,^_^
以下是找的TRACE的详细介绍:
==============================
TRACE宏对于VC下程序调试来说是很有用的东西,有着类似printf的功能;该宏仅仅在程序的DEBUG版本中出现,当RELEASE的时候该宏就完全消息了,从而帮助你调式也在RELEASE的时候减少代码量。
使用非常简单,格式如下:
TRACE("DDDDDDDDDDD");
TRACE("wewe%d",333);
同样还存在TRACE0,TRACE1,TRACE2。。。分别对应0,1,2。。个参数
TRACE信息输出到VC IDE环境的输出窗口(该窗口是你编译项目出错提示的哪个窗口),但仅限于你在VC中运行你的DEBUG版本的程序。
TRACE信息还可以使用DEBUGVIEW来捕获到。这种情况下,你不能在VC的IDE环境中运行你的程序,而将BUILD好的DEBUG版本的程序单独运行,这个时候可以在DEBUGVIEW的窗口看到DEBUGVIE格式的输出了。
VC中TRACE的用法有以下四种:
1:
TRACE ,就是不带动态参数输出字符串, 类似C的printf("输出字符串");
2:
TRACE 中的字符串可以带一个参数输出 , 类似C的printf("...%d",变量);
3:
TRACE 可以带两个参数输出,类似C的printf("...%d...%f",变量1,变量2);
4:
TRACE 可以带三个参数输出,类似C的printf("...%d,%d,%d",变量1,变量2,变量3);
TRACE 宏有点象我们以前在C语言中用的Printf函数,使程序在运行过程中输出一些调试信息,使我们能了解程序的一些状态。但有一点不同的是:
TRACE 宏只有在调试状态下才有所输出,而以前用的Printf 函数在任何情况下都有输出。和Printf 函数一样,TRACE函数可以接受多个参数如:
int x = 1;
int y = 16;
float z = 32.0;
TRACE( "This is a TRACE statement/n" );
TRACE( "The value of x is %d/n", x );
TRACE( "x = %d and y = %d/n", x, y );
TRACE( "x = %d and y = %x and z = %f/n", x, y, z );
要注意的是TRACE宏只对Debug 版本的工程产生作用,在Release 版本的工程中,TRACE宏将被忽略。
- 在MFC中,你可以使用TRACE和AfxOutputDebugString宏、CObject::Dump虚拟函数和AfxDumpStack函数。TRACE宏由AfxDump实现,AfxDump由AfxOutputDebugString实现。AfxOutputDebugString宏和AfxDumpStack函数可以在所有版本中编译,其他只能在调试版本中编译。
- TRACE宏有以下形式:
_TRACE(reportType,format);
_TRACE0(reportType,format,arg1);
_TRACE1(reportType,format,arg1,arg2);
_TRACE2(reportType,format,arg1,arg2,arg3);
_TRACE3(reportType,format,arg1,arg2,arg3,arg4);
- 在MFC中,推荐使用TRACEn宏,当使用TRACE宏时需要使用_T宏来格式化参数以正确解决Unicode的校正,而TRACEn不需要。
- MFC TRACE宏中的一个缺点是AfxTrace函数使用一个512字符固定大小的缓冲区,这使得它在跟踪长字符串时是无用的。
- CObject::Dump
CObject类有一个转储(dump)虚拟函数,所有继承CObject的类都可以通过重载这个函数,输出它们的值。