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

Double-click on the Trace Message in the Output Window in Developer Studio to Get to the Line of Code

0.00/5 (No votes)
14 Oct 2005 1  
Take advantage of the output window in Developer Studio

Introduction

In some instances, you may find it advantageous to format your debug dump like:

filepathname.cpp(123) : something

When such a line is displayed in the Output window of your Developer Studio/Visual Studio, you can double-click on it, and jump to the location specified by the file path/line number pattern. It is very handy to generate debug dumps with easy access to the original line, so I wrote a macro to format debug dumps with the __FILE__ and __LINE__ preprocessors.

I first found this feature of the build output window in the Knowledge Base article HOWTO: Use #pragma to Generate User-Defined Warning Messages, which is almost ideal to insert TODO blocks. Later, I tried it in the debug output, and found it also existed in the debug output window. It may work in other output windows too.

More Information

The following code illustrates how to tell the compiler to format the debug dump with file path and line numbers:

// collisions.h
#ifdef _DEBUG
#define TRACE_LINE(string) \
{\
  CString  strTrace;           \
  strTrace.Format("%s(%d) :  \t%ld:\t%s",  \
    __FILE__,__LINE__,timeGetTime(),string);       \
  if (strTrace.GetLength() > 512) \
    TRACE("TRACE string too long !\r\n"); \
  else TRACE(strTrace);}
#else
#define TRACE_LINE(string)              void(0)
#endif

// collisions.cpp
TRACE_LINE("Need to do 3D collision testing") ;

Debug Output

collisions.cpp.cpp(123) : Need to do 3D collision testing

I have other varieties of this such as TRACE_LINE1 which takes a format string and a parameter, and TRACE_LINE2 which takes a format string and two parameters, and so on. You can surely write yourself one without the MFC support also.

For additional information concerning the __FILE__ and __LINE__ predefined macros, see the Visual C++ Help file; Search on: "predefined macros", Topic: "Preprocessor Reference", and click on "ANSI".

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