Home > C++, VC++, Visual studio > AfxTrace trick!

AfxTrace trick!

Ever felt the need for a simple thing like, you print something to developer studio output window and when you double click on that line it takes us to the place from where this was output.

For eg: You have an unknown exception and you put a trace statement which prints the file name and line number to developer studio output window, so when you double click on that line in developer studio it takes us to the place where this exception was traced out. Much like the error statements in output window of developer studio. You press F4/F8 and our cursor is on the line which caused the error.

Here is a macro that does this…

#if ! defined UNKNOWN_EXCEPTION
#   ifdef _DEBUG
#       define UNKNOWN_EXCEPTION AfxTrace(_T("\n%s(%i): Unknown Exception occurred!!"), _T(__FILE__),__LINE__)
#   else
#       define UNKNOWN_EXCEPTION 0
#   endif
#endif

Now just use this from somewhere you want to do a trace for eg:

void SomeFunkyStuff()
{
     try
     {
           char *Blah = 0;
           *Blah = 0; // Hope this throws an exception
     }
     catch( ... )
     {
          UNKNOWN_EXCEPTION;
     }// End try
}// End SomeFunkyStuff

So now when exception is thrown, a trace statment will be printed to developer studio output window, double click on the line your cursor should be on the line which traced out this line to developer studio output window. Well if it doesn’t then 😛

This is not my idea, shamelessly stolen from MSDN, which was originally given by someone.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: