C++ 如何在非 MFC 项目中使用 TRACE 宏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/494653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:39:13  来源:igfitidea点击:

How can I use the TRACE macro in non-MFC projects?

c++visual-c++visual-studio-2005

提问by jagttt

I want to use the TRACE() macro to get output in the debug window in Visual Studio 2005in a non-MFCC++ project, but which additional header or library is needed?

我想在非MFCC++ 项目中使用 TRACE() 宏在Visual Studio 2005的调试窗口中获取输出,但是需要哪些额外的头文件或库?

Is there a way of putting messages in the debug output window and how can I do that?

有没有办法在调试输出窗口中放置消息,我该怎么做?

回答by Ferruccio

Build your own.

建立你自己的。

trace.cpp:

跟踪.cpp:

#ifdef _DEBUG
bool _trace(TCHAR *format, ...)
{
   TCHAR buffer[1000];

   va_list argptr;
   va_start(argptr, format);
   wvsprintf(buffer, format, argptr);
   va_end(argptr);

   OutputDebugString(buffer);

   return true;
}
#endif

trace.h:

跟踪.h:

#include <windows.h>
#ifdef _DEBUG
bool _trace(TCHAR *format, ...);
#define TRACE _trace
#else
#define TRACE false && _trace
#endif

then just #include "trace.h" and you're all set.

然后只需 #include "trace.h" 就可以了。

Disclaimer: I just copy/pasted this code from a personal project and took out some project specific stuff, but there's no reason it shouldn't work. ;-)

免责声明:我只是从个人项目中复制/粘贴了此代码并取出了一些特定于项目的内容,但没有理由它不起作用。;-)

回答by Ulf Lindback

If you use ATL you can try ATLTRACE.

如果您使用 ATL,您可以尝试 ATLTRACE。

TRACE is defined in afx.h as (at least in vs 2008):

TRACE 在 afx.h 中定义为(至少在 vs 2008 中):

// extern ATL::CTrace TRACE;
#define TRACE ATLTRACE

And ATLTRACE can be found in atltrace.h

并且 ATLTRACE 可以在 atltrace.h 中找到

回答by Fredrik Jansson

You can try the DebugOutputString function. TRACE is only enabled in debug builds.

您可以尝试 DebugOutputString 函数。TRACE 仅在调试版本中启用。

回答by sarat

In my understanding wvsprintf has problem with formatting. Use _vsnprintf (or thcar version _vsntprintf ) instead

在我的理解 wvsprintf 有格式问题。使用 _vsnprintf (或 thcar 版本 _vsntprintf )代替

回答by olibre

Thanks to these answers I have fixed my bug :-)

由于这些答案,我已经修复了我的错误 :-)

Here I share my TRACE macro in C++ based on ideas from Ferruccio and enthusiasticgeek.

在这里,我根据 Ferruccio 和热情极客的想法分享我在 C++ 中使用的 TRACE 宏。

#ifdef ENABLE_TRACE
#  ifdef _MSC_VER
#    include <windows.h>
#    include <sstream>
#    define TRACE(x)                           \
     do {  std::stringstream s;  s << (x);     \
           OutputDebugString(s.str().c_str()); \
        } while(0)
#  else
#    include <iostream>
#    define TRACE(x)  std::clog << (x)
#  endif        // or std::cerr << (x) << std::flush
#else
#  define TRACE(x)
#endif

example:

例子:

#define ENABLE_TRACE  //can depend on _DEBUG or NDEBUG macros
#include "my_above_trace_header.h"

int main (void)
{
   int     v1 = 123;
   double  v2 = 456.789;
   TRACE ("main() v1="<< v1 <<" v2="<< v2 <<'\n');
}

Any improvements/suggestions/contributions are welcome ;-)

欢迎任何改进/建议/贡献;-)