Click here to Skip to main content
Click here to Skip to main content

Simple trace format tips

By , 4 Oct 2000
 
  • Download demo project - 6 Kb
  • Introduction

    When writing TRACE statements to the output window in DevStudio it is often useful to be able to go directly to the section of code that produced the TRACE output. Doing this is simple - you just use the __FILE__ and __LINE__ macros in your TRACE statements.

    If you format your TRACE statement as follows:

    TRACE(_T("%s(%i) : Please double click on me!\n"), __FILE__,__LINE__);

    then you can double click on the TRACE output line and be taken directly to the line of code the produced the TRACE statement.

    An example of using this can be find in the enclosed example. Note that in the example I used the CDuration class by Laurent Guinnard.

    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

    About the Author

    Audrius Vasiliauskas
    Business Analyst
    Lithuania Lithuania
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralDebug LogsmemberPaul Grew8 Feb '01 - 2:13 
    At several places i have worked Debug logs have been segregated into several categories.
     
    Fatal
    Error
    Warning
    Debug1
    ...
    Debug9
     
    This seemed sensible until agreeing a strategy for use. Any suggestions?
    Confused | :confused:
    GeneralThis (and more) has been a part of ATL/AUX for 2 years...sussAndrew Nosenko9 Oct '00 - 11:15 
    http://www.codeproject.com/atl/atlaux.asp#Error
    GeneralI like it, but...sussPaul Westcott5 Oct '00 - 6:52 
    G'day Audrius Vasiliauskas,
     
    I like it! But yeah it's a bit of a hassle having to remember to put in all that stuff everytime, so how about something along the lines of :
     
    ---------------------------cut here------------------------
    #ifdef ATLTRACE
    #ifndef TRACE_FUNCTION
    #define TRACE_FUNCTION ATLTRACE
    #endif
    #endif
     
    #ifdef TRACE
    #ifndef TRACE_FUNCTION
    #define TRACE_FUNCTION TRACE
    #endif
    #endif
     
    #define LINETRACE TRACE_FUNCTION(_T("%s(%i) : "), __FILE__,__LINE__);TRACE_FUNCTION
    ---------------------------cut here------------------------
     
    Then one can just use the LINETRACE macro instead of having to put the stuff in manually...
     
    Have fun,
    Paul Westcott
    GeneralHow to make it even shortersussHelmut Mülner5 Oct '00 - 22:31 
    Put this in stdafx.h:
     
    #ifdef _DEBUG
    #undef TRACE
    #define TRACE ::AfxTrace(_T("%s(%i) : "),__FILE__,__LINE__),::AfxTrace
    #endif
     
    No changes in the sources necessary, you just have to recompile. Note the comma instead of the
    semicolon to make the following kind of statements work:
     
    if (someErrorCondition) TRACE("some error text\n");
     
    Have fun,
    Helmut Mülner
    GeneralRe: How to make it even shortersussPaul Westcott6 Oct '00 - 5:24 
    G'day Helmut,
     
    Two problems with what you have suggested:
     
    1. you are replacing all TRACE statements with the altered function, which is not necessary what you want to do (such as in the sample project that Audrius sent:
     

    #define DURATION_TRACE(str) \
    drTimeSpan.Stop(); \
    TRACE(_T("%s(%i) : \n"),__FILE__,__LINE__); \
    TRACE(_T("Duration\n")); \
    TRACE(_T("\tInfo: %s.\n"),str); \
    TRACE(_T("\tCurrent time: %s. \n"),
    COleDateTime::GetCurrentTime().Format()); \
    TRACE(_T("\tTime elapsed: %s. \n"),drTimeSpan.Format())

     
    which you would have to all be put on one line, which would make it less readable, whereas under my scheme, the first line would be a LINETRACE and the rest of the lines would still be TRACE statements
     
    2. Your proposed solution is ok if you are using MFC, but the solution I proposed will work if you are using either a MFC application or a ATL application - it could really have been as follows to be a little bit shorter though [save 1 line!! Smile | :) ]...
     
    #ifndef TRACE_FUNCTION
    #ifdef ATLTRACE
    #define TRACE_FUNCTION ATLTRACE
    #else
    #ifdef TRACE
    #define TRACE_FUNCTION TRACE
    #endif
    #endif
    #endif
     
    #define LINETRACE TRACE_FUNCTION(_T("%s(%i) : "), __FILE__,__LINE__);TRACE_FUNCTION
     
    this (as well as my original snippet), also allow the user to overwrite the TRACE_FUNCTION by defining it before this section of code.
     
    Have fun,
    Paul Westcott
    GeneralRe: I like it, but...memberAnonymous30 Jul '01 - 4:58 
    why not just put this into a macro?

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

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.6.130516.1 | Last Updated 5 Oct 2000
    Article Copyright 2000 by Audrius Vasiliauskas
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid