Click here to Skip to main content
6,595,444 members and growing! (17,038 online)
Email Password   helpLost your password?
Development Lifecycle » Debug Tips » General     Intermediate

A Class for Creating a Trace Log

By Robert Pittenger

VC6, Windows, Dev, QA
Posted:17 Nov 1999
Views:59,641
Bookmarked:28 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
24 votes for this article.
Popularity: 5.38 Rating: 3.90 out of 5

1
1 vote, 10.0%
2

3
3 votes, 30.0%
4
6 votes, 60.0%
5
  • Download demo project - 170 Kb
  • So the Debug build works great, but the Release build crashes? Or the program works fine on your computer, but crashes on the customer's computers? You need to add trace functionality to the Release build.

    CLogTrace is a class that allows you to easily add trace functionality to the release build of a program. You make the program write lines of text to a log file at various stages in the program. You provide the code to turn the logging on or off. Much like using the TRACE macro, you get a text file that tells you what functions were called, with a date/time stamp. Except you can get this information in release builds.

    The class is easy to use.

    1. Make a static CLogTrace object as a member of the application class
    2. Add the initialization lines in the programs InitInstance function.
    3. 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

    Here is an example log file output...

    11/4/99 10:16:44 PM	Started the application
    11/4/99 10:16:44 PM	Created doc template
    11/4/99 10:16:44 PM	CDoc constructor
    11/4/99 10:16:44 PM	Precreate window
    11/4/99 10:16:44 PM	Precreate window
    11/4/99 10:16:44 PM	CView Constructor
    11/4/99 10:16:44 PM	CView::Precreate window
    11/4/99 10:16:44 PM	Created main window
    11/4/99 10:16:44 PM	Created tool bar
    11/4/99 10:16:44 PM	Created status bar
    11/4/99 10:16:44 PM	OnNew Document
    11/4/99 10:16:44 PM	Leaving InitInstance
    11/4/99 10:16:46 PM	Leaving the application
    
    

    Here is the class interface...

    ////////////////////////////////////////////////////////////////////////
    
    //  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(LPCTSTR szFormat, LPCTSTR szAddInfo);
    	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__

    Here is the class implementation...

    ////////////////////////////////////////////////////////////////////////
    
    //  LogTrace.cpp -- Implementation of the CLogTrace class
    
    
    
    #include "stdafx.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******************************************\n\n");
    	s.Format("%s Log Trace %s\n\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)
    	{
    		// Note that there is not much we can do if there is an exception
    
    		// It is not practical to tell the user each time
    
    		// and we can't write the error to a log file!!!!
    
    		e->Delete();
    	}
    	f.Close();
    }
    
    // function to write a line of text, with an extra string
    
    void CLogTrace::WriteLine(LPCTSTR szFormat, LPCTSTR szAddInfo)
    {
    	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%s", drive, dir, szFileName);
    
    }

    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

    Robert Pittenger


    Member

    Location: United States United States

    Other popular Debug Tips articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
    Questionfunction to write a line of text, with an extra string [modified] PinmemberVasanth MV1:43 26 Mar '07  
    GeneralSome suggestions... PinsussNVRAM5:52 19 Dec '03  
    GeneralProblems with code in debug version PinmemberRichard Dalling2:11 9 Nov '02  
    GeneralHow would I use this in a DLL... PinmemberMel Riffe8:42 17 Jan '02  
    GeneralThanks a lot! Pinmemberswitchwang15:56 5 Jun '01  
    Generalgreat stuff !!! PinsussJohan2:44 18 Aug '00  
    GeneralBe better not to use MFC... PinsussMaksym Schipka23:18 15 Apr '00  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 17 Nov 1999
    Editor: Chris Maunder
    Copyright 1999 by Robert Pittenger
    Everything else Copyright © CodeProject, 1999-2009
    Web13 | Advertise on the Code Project