Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Supressing TRACE calls

0.00/5 (No votes)
12 Jul 2005 1  
How to prevent TRACE calls from appearing in the output window.

Introduction

The necessity: bloated/unnecessary/too many TRACE calls in the previously written code that disturb the debugging process.

How it works

The variable that controls the TRACE calls is afxTraceEnabled. It is defined in the file C:\Program Files\Microsoft Visual Studio\VC98\MFC\Include\AFX.H (the default installation path of Visual C++ 6.0).

The afxTraceEnabled flag is global, so it will affect all the TRACE calls.

#include "StdAfx.h"

#include "MyClass.h"
#ifdef _DEBUG
__declspec(dllimport) extern BOOL afxTraceEnabled;
#endif
CMyClass::CMyClass()
{
#ifdef _DEBUG
  afxTraceEnabled = FALSE;
#endif
}

That is all. Here, the TRACE calls are suppressed and only the OutputDebugString calls remain. The variable can be set/unset as and when necessary to allow or disable TRACE calls in certain sections, however this should be done by modifying the source code of all the files affected.

Another approach is to implement enable/disable of the flag in a separate file. Calling two functions, let' say __EnableTrace and __DisableTrace allows the implementation to be controlled in a single place, and the source code of other files can call these functions as and when necessary.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here