Click here to Skip to main content
15,868,164 members
Articles / Mobile Apps

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

Rate me:
Please Sign up or sign in to vote.
3.22/5 (36 votes)
14 Oct 2005CPOL1 min read 155K   19   5
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:

C++
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
JIANG,Sheng, a Microsoft Most Valuable Professional in Visual C++ since 2004, is a student at Austin Community College. He is active in Visual C++ forums, blogs and newsgroups, such as CSDN, Netease, Joycode and Microsoft forums and newsgroups. He prefer plays computer games (espacially RTS ones) in his spare time.

www.jiangsheng.net

Comments and Discussions

 
GeneralVisual Studio's "Task List" or "Error List" Pin
JesseChisholm1-May-09 8:00
JesseChisholm1-May-09 8:00 
GeneralWon't work for UNICODE Pin
yafan17-Oct-05 11:15
yafan17-Oct-05 11:15 
GeneralRe: Won't work for UNICODE Pin
JesseChisholm1-May-09 7:54
JesseChisholm1-May-09 7:54 
GeneralCompile error Pin
vulcy30-May-05 22:16
vulcy30-May-05 22:16 
Generalgood article Pin
kingzai21-Apr-03 20:33
kingzai21-Apr-03 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.