<p>The <strong><a href="http://msdn.microsoft.com/en-us/library/4wyz8787(VS.80).aspx" rel="noreferrer">TRACE macro</a></strong> can be used to output diagnostic messages to the debugger when the code is compiled in <strong>Debug</strong> mode. I need the same messages while in <strong>Release</strong> mode. Is there a way to achieve this?</p>

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

[{'answer_id': 27628, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 1, 'selected': False, 'text': "<p>In MFC, TRACE is defined as ATLTRACE. And in release mode that is defined as:</p>\n\n<pre><code>#define ATLTRACE            __noop\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>void MyTrace(const CString&amp; text)\n{\n  ::OutputDebugString(text); // Outputs to console, same as regular TRACE\n  // TODO: Do whatever output you need here. Write to event log / write to text file / write to pipe etc.\n}\n</code></pre>\n"}, {'answer_id': 27632, 'author': 'Magnus Westin', 'author_id': 2957, 'author_profile': 'https://Stackoverflow.com/users/2957', 'pm_score': 2, 'selected': False, 'text': '<p>TRACE is just a macro for <a href="http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx" rel="nofollow noreferrer">OutputDebugString</a>. So you can easily just make your own TRACE macro (or call it something else) that will call <a href="http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx" rel="nofollow noreferrer">OutputDebugString</a>.</p>\n'}, {'answer_id': 41046, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 4, 'selected': True, 'text': '<p>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:</p>\n\n<pre><code>void trace(const char* format, ...)\n{\n   char buffer[1000];\n\n   va_list argptr;\n   va_start(argptr, format);\n   wvsprintf(buffer, format, argptr);\n   va_end(argptr);\n\n   OutputDebugString(buffer);\n}\n</code></pre>\n'}, {'answer_id': 777634, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>A few years back I needed similar functionality so I cobbled together the following code. Just save it into a file, e.g. rtrace.h, include it at the end of your stdafx.h, and add _RTRACE to the release mode Preprocessor defines.</p>\n\n<p>Maybe someone will find a use for it :-)</p>\n\n<p>John</p>\n\n<pre>\n#pragma once\n\n//------------------------------------------------------------------------------------------------\n//\n// Author:   John Cullen\n// Date:     2006/04/12\n// Based On: MSDN examples for variable argument lists and ATL implementation of TRACE.\n//\n// Description: Allows the use of TRACE statements in RELEASE builds, by overriding the\n// TRACE macro definition and redefining in terms of the RTRACE class and overloaded\n// operator (). Trace output is generated by calling OutputDebugString() directly.\n//\n//\n// Usage:    Add to the end of stdafx.h and add _RTRACE to the preprocessor defines (typically\n//           for RELEASE builds, although the flag will be ignored for DEBUG builds.\n//\n//------------------------------------------------------------------------------------------------\n\n#ifdef _DEBUG\n\n// NL defined as a shortcut for writing FTRACE(_T("\\n")); for example, instead write FTRACE(NL);\n#define NL _T("\\n") \n#define LTRACE TRACE(_T("%s(%d): "), __FILE__, __LINE__); TRACE\n#define FTRACE TRACE(_T("%s(%d): %s: "), __FILE__, __LINE__, __FUNCTION__); TRACE\n\n#else   // _DEBUG\n\n#ifdef _RTRACE\n#undef TRACE\n#define TRACE  RTRACE()\n#define LTRACE RTRACE(__FILE__, __LINE__)\n#define FTRACE RTRACE(__FILE__, __LINE__, __FUNCTION__)\n#define NL _T("\\n") \n\nclass RTRACE\n{\npublic:\n    // default constructor, no params\n    RTRACE(void) : m_pszFileName( NULL ), m_nLineNo( 0 ), m_pszFuncName( NULL ) {};\n\n    // overloaded constructor, filename and lineno\n    RTRACE(PCTSTR const pszFileName, int nLineNo) :\n        m_pszFileName(pszFileName), m_nLineNo(nLineNo), m_pszFuncName(NULL) {};\n\n    // overloaded constructor, filename, lineno, and function name\n    RTRACE(PCTSTR const pszFileName, int nLineNo, PCTSTR const pszFuncName) :\n        m_pszFileName(pszFileName), m_nLineNo(nLineNo), m_pszFuncName(pszFuncName) {};\n\n    virtual ~RTRACE(void) {};\n\n    // no arguments passed, e.g. RTRACE()()\n    void operator()() const\n    {\n        // no arguments passed, just dump the file, line and function if requested\n        OutputFileAndLine();\n        OutputFunction();\n    }\n\n    // format string and parameters passed, e.g. RTRACE()(_T("%s\\n"), someStringVar)\n    void operator()(const PTCHAR pszFmt, ...) const\n    {\n        // dump the file, line and function if requested, followed by the TRACE arguments\n        OutputFileAndLine();\n        OutputFunction();\n\n        // perform the standard TRACE output processing\n        va_list ptr; va_start( ptr, pszFmt );\n        INT len = _vsctprintf( pszFmt, ptr ) + 1;\n        TCHAR* buffer = (PTCHAR) malloc( len * sizeof(TCHAR) );\n        _vstprintf( buffer, pszFmt, ptr );\n        OutputDebugString(buffer);\n        free( buffer );\n    }\n\nprivate:\n    // output the current file and line\n    inline void OutputFileAndLine() const\n    {\n        if (m_pszFileName && _tcslen(m_pszFileName) > 0)\n        {\n            INT len = _sctprintf( _T("%s(%d): "), m_pszFileName, m_nLineNo ) + 1;\n            PTCHAR buffer = (PTCHAR) malloc( len * sizeof(TCHAR) );\n            _stprintf( buffer, _T("%s(%d): "), m_pszFileName, m_nLineNo );\n            OutputDebugString( buffer );\n            free( buffer );\n        }\n    }\n\n    // output the current function name\n    inline void OutputFunction() const\n    {\n        if (m_pszFuncName && _tcslen(m_pszFuncName) > 0)\n        {\n            INT len = _sctprintf( _T("%s: "), m_pszFuncName ) + 1;\n            PTCHAR buffer = (PTCHAR) malloc( len * sizeof(TCHAR) );\n            _stprintf( buffer, _T("%s: "), m_pszFuncName );\n            OutputDebugString( buffer );\n            free( buffer );\n        }\n    }\n\nprivate:\n    PCTSTR const m_pszFuncName;\n    PCTSTR const m_pszFileName;\n    const int m_nLineNo;\n};\n\n#endif // _RTRACE\n\n#endif // NDEBUG\n\n</pre>\n'}, {'answer_id': 25657721, 'author': 'deniro.wang', 'author_id': 2261302, 'author_profile': 'https://Stackoverflow.com/users/2261302', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s most simply code that I had see</p>\n\n<pre><code>#undef ATLTRACE\n#undef ATLTRACE2\n\n#define ATLTRACE2 CAtlTrace(__FILE__, __LINE__, __FUNCTION__)\n#define ATLTRACE ATLTRACE2\n</code></pre>\n\n<p>see\n<a href="http://alax.info/blog/1351" rel="nofollow">http://alax.info/blog/1351</a></p>\n'}]
['https://Stackoverflow.com/questions/27622', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1630/']