QA: How to turn off messages sent by MFC to Debug window?

Alexander Shargin (rudankort@softspb.com), April 25, 2001.

Question

I use TRACE command to output debug information to Debug window of eVC. But it's hard to find my own debug messages since Debug window is flooded by messages from MFC. How can I turn these off?

Answer

All debug messages sent by MFC are controlled by a global variable afxTraceFlags. MFC debug messages are devided into several categories. Each category has its own flag in afxTraceFlags. If a flag of some category is set, messages of this category will appear in the Debug window. Trace flags are defined in afxwin.h:

enum AfxTraceFlags { traceMultiApp = 1, // multi-app debugging traceAppMsg = 2, // main message pump trace (includes DDE) traceWinMsg = 4, // Windows message tracing traceCmdRouting = 8, // Windows command routing trace // (set 4+8 for control notifications) traceOle = 16, // special OLE callback trace traceDatabase = 32, // special database trace traceInternet = 64 // special Internet client trace }

afxTraceFlags variable is available in Debug builds only. It's initialized at the very beginning of your program's execution. In the desktop version of MFC afxTraceFlags is initialized from Windows registry. Visual Studio 6.0 has a handy utility called MFC tracer, which allows the user to edit trace flags in registry. Using MFC tracer one can choose debug messages, which he wants to receive. But on CE afxTraceFlags is always initialized by 8:

#if defined(_WIN32_WCE) afxTraceEnabled = TRUE; afxTraceFlags = 8; #else // _WIN32_WCE afxTraceEnabled = ::GetPrivateProfileInt( szDiagSection, szTraceEnabled, TRUE, szIniFile); afxTraceFlags = ::GetPrivateProfileInt( szDiagSection, szTraceFlags, 0, szIniFile); #endif // _WIN32_WCE

If you refer to the enumeration listed above you will see that 8 is actually a traceCmdRouting flag. That's why MFC constantly pumps messages like "SENDING command id XXXX to YYYY target" to Debug window.

To suppress these messages or turn on messages from some other category you have to modify afxTraceFlags variable. You can do it in your InitInstance function, e. g.:

BOOL CSampleApp::InitInstance() { #ifdef _DEBUG afxTraceFlags = 0; // *** UNCOMMENT NEXT LINE if you want multi-app debugging *** // afxTraceFlags |= traceMultiApp; // *** UNCOMMENT NEXT LINE if you want main message pump trace (includes DDE) *** // afxTraceFlags |= traceAppMsg; // *** UNCOMMENT NEXT LINE if you want Windows message tracing *** // afxTraceFlags |= traceWinMsg; // *** UNCOMMENT NEXT LINE if you want Windows command routing trace *** // afxTraceFlags |= traceCmdRouting; // *** UNCOMMENT NEXT LINE if you want special OLE callback trace *** // afxTraceFlags |= traceOle; // *** UNCOMMENT NEXT LINE if you want special database trace *** // afxTraceFlags |= traceDatabase; // *** UNCOMMENT NEXT LINE if you want special Internet client trace *** // afxTraceFlags |= traceInternet; #endif /* DEBUG */ ... }

Note #ifdef/#endif pair of macros. Since afxTraceFlags is only available in Debug builds you have to exclude any operations on it from Release build.

Related resources: