Tracking and debugging

Source: Internet
Author: User

/*
When tracking code, many C ++ programmers often define a simple Trace class to print diagnostic information to a log file. Programmer
You can define a Trace object in each function to be tracked. You can write a Trace object in the function Entry and Exit Trace classes.
Disadvantage: To increase program overhead, you must recompile the program to open or close the tracing.
*/
Class Trace
{
Public:
Trace (conse string & name );
~ Trace (); www.2cto.com
Void debug (const string & msg );

Static BOOL traceIsActive;
Private:
String theFunctionName;
};

Inline Trace: Trace (const string & name): theFunctionName (name)
{
If (traceIsActive)
{
Cout <"Enter function" <name <endl;
}
}

Inline Trace: debug (const string & msg)
{
If (traceIsActive)
{
Cout <msg <endl;
}
}

Inline Trace ::~ Trace ()
{
If (traceIsActive)
{
Cout <"Exit function" <theFunctionName <endl;
}
}

Int myFunction (int x)
{
# Ifdef TRACE
Trace t ("myFunction"); // The constructor uses a function name as a parameter.
T. debug ("Some information message ");
# Endif

}

// The above code has performance problems. Optimized version:
Class Trace
{
Public:
Trace (const char * name );
~ Trace ();
Void debug (const char * msg );

Static BOOL traceIsActive;
Private:
String * theFunctionName;
};

Inline Trace: Trace (const char * name): theFunctionName (NULL)
{
If (traceIsActive)
{
Cout <"Enter function" <* name <endl;
TheFunctionName = new string (* name );
}
}

Inline Trace: debug (const char * msg)
{
If (traceIsActive)
{
Cout <* msg <endl;
}
}

Inline Trace ::~ Trace ()
{
If (traceIsActive)
{
Cout <"Exit function" <* theFunctionName <endl;
Delete theFunctionName;
}
}

Int myFunction (int x)
{
# Ifdef TRACE
Char * name = "myFunction ";
Trace t (name); // The constructor uses a function name as a parameter.
# Endif

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.