tags:

views:

1686

answers:

4

The TRACE macro can be used to output diagnostic messages to the debugger when the code is compiled in Debug mode. I need the same messages while in Release mode. Is there a way to achieve this?

(Please do not waste your time discussing why I should not be using TRACE in Release mode :-)

+1  A: 

In MFC, TRACE is defined as ATLTRACE. And in release mode that is defined as:

define ATLTRACE __noop

So, using the out-the-box TRACE from MFC, you won't actually be able to read any TRACE text, because it won't even be written out. You could write your own TRACE function instead, then re-define the TRACE macro. You could do something like this:

void MyTrace(const CString& text)
{
  ::OutputDebugString(text); // Outputs to console, same as regular TRACE
  // TODO: Do whatever output you need here. Write to event log / write to text file / write to pipe etc.
}

Hope that helps.

Mark

Mark Ingram
+1  A: 

TRACE is just a macro for OutputDebugString. So you can easily just make your own TRACE macro (or call it something else) that will call OutputDebugString.

Magnus Westin
+2  A: 

Actually, the TRACE macro is a lot more flexible than OutputDebugString. It takes a printf() style format string and parameter list whereas OutputDebugString just takes a single string. In order to implement the full TRACE functionality in release mode you need to do something like this:

void trace(const char* format, ...)
{
   char buffer[1000];

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

   OutputDebugString(buffer);
}
Ferruccio
I know this is just an example, but there is a huge buffer overrun potential in this example take as is.
Aardvark
A: