This class is pretty good. If you want to see the original text, please go to codeproject.

Source: Internet
Author: User

Http://www.codeproject.com/debug/logtrace.asp
Below I put the source code. /**///////////////////////////////////// ////////////////////////////////////
// Logtrace. cpp -- interface for the clogtrace class
// A class to do Debug Logging

# Ifndef _ logtrace_h __
# DEFINE _ logtrace_h __

Class clogtrace
{
// Construction/destruction
Public:
Clogtrace ();
~ Clogtrace ();

// Attributes
Public:
Cstring m_strappname;

Protected:
Bool m_bactive;
Cstring m_strfilename;
Bool m_btimestamp;

// Operations
Public:
Void writeline (lpctstr szline );
Void writeline (maid, maid );
Void writeline (lpctstr szformat, int naddinfo );
Void resetfile ();
Void onstartup (bool bactive, bool btimestamp );
Void setfilename (lpctstr szfilename );

Protected:

 

// Inlines
Public:
Inline void setactive (bool bset)
{
M_bactive = bset;
}
Inline cstring getfilename ()
{
Return m_strfilename;
}
};

# Endif/_ logtrace_h __

 

 

 

 

 

 

 

 

/**///////////////////////////////////// ////////////////////////////////////
// Logtrace. cpp -- Implementation of the clogtrace class

# Include "stdafx. H"
# Include <afxdisp. h>
# Include "logtrace. H"

/**//*********************************** ***************

How to Use clogtrace

1. Make a static clogtrace object as a member of the application class

2. Add the following lines to the initinstance of the program

M_logtrace.m_strappname = "MyApp"; // use appropriate name here

M_logtrace.setfilename ("log.txt"); // sets the log file name and puts it in the EXE path

M_logtrace.onstartup (True, true); // activates the log trace

3. Also in initinstance, add the following line if you want to empty the log file
Each time the application starts

M_logtrace.resetfile ();

4. Any time you want to write to the log file, use the clogtrace: writeline Functions
These will write the text along with date and time

**************************************** ***************/

 

/**///////////////////////////////////// //////////////////
// Construction/destruction

Clogtrace: clogtrace ()
{
M_bactive = false;
M_btimestamp = true;

Cstring S;
}

Clogtrace ::~ Clogtrace ()
{

}

/**///////////////////////////////////// ////////////////////
// Clogtrace operations

Void clogtrace: resetfile ()
{
Cstdiofile F;
Cfileexception Fe;
Cstring S;

If (m_strfilename.isempty () return;

If (F. Open (m_strfilename, cfile: modewrite | cfile: modecreate, & Fe) = false)
{
Return;
}

F. Close ();
}

 

// Bactive tells us if we want the trace to be active or not
// Btimestamp tells us if we want time stamps on each line
// Eliminating the time stamp allows us to use this class for a regular Log File
Void clogtrace: onstartup (bool bactive, bool btimestamp)
{
M_bactive = bactive;
M_btimestamp = btimestamp;
If (btimestamp = false) return;
Cstring S;

// These ***'s help to indicate when one Ru of the program ends and another starts
// Because we don't always overwrite the file each time

Writeline ("/n ********************************* * ********/n ");
S. Format ("% s log trace % s/n", m_strappname, coledatetime: getcurrenttime (). Format ());
Writeline (s );
}

 

// Function to write a line of text to the log file
Void clogtrace: writeline (lpctstr szline)
{
Cstdiofile F;
Cfileexception Fe;
Cstring S;

If (m_bactive = false) return;
If (m_strfilename.isempty () return;

If (F. Open (m_strfilename, cfile: modewrite | cfile: modecreate |
Cfile: modenotruncate, & Fe) = false)
{
Return;
}

Try
{
F. seektoend ();
Trace ("Loggin % s/n", szline );
If (m_btimestamp)
{
S. Format ("% S/T % s/n", coledatetime: getcurrenttime (). Format (),
Szline );
}
Else
{
S. Format ("% s/n", szline );
}
F. writestring (s );
}
Catch (cexception * E)
{
E-> Delete ();
}
F. Close ();
}

// Function to write a line of text, with an extra string
Void clogtrace: writeline (maid, maid)
{
If (m_bactive = false) return;
Cstring S;
S. Format (szformat, szaddinfo );
Writeline (s );
}

// Funtion to write a line of text with an extra integer
Void clogtrace: writeline (lpctstr szformat, int naddinfo)
{
If (m_bactive = false) return;
Cstring S;
S. Format (szformat, naddinfo );
Writeline (s );
}

// Function to set the log file name. Don't pass a fill path!
// Just pass something like "log.txt"
// The file will be placed in the same dir as the EXE file
Void clogtrace: setfilename (lpctstr szfilename)
{
Tchar drive [_ max_path], dir [_ max_path], name [_ max_path], ext [_ max_path];

Const char * Path = _ pgmptr;

_ Splitpath (path, drive, Dir, name, ext );

M_strfilename.format ("% S % s", drive, Dir, szfilename );

}

 

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.