Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A Class for Creating a Trace Log

, 17 Nov 1999
////////////////////////////////////////////////////////////////////////
//  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******************************************\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)
	{
		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);

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

Robert Pittenger, MCPD-EAD
President Starpoint Software Inc.
United States United States
Bob Pittenger is founder and President of Starpoint Software Inc. He holds a B.A. degree from Miami University, M.S. and Ph.D. degrees from Purdue University, and an MBA from Xavier University. He has been programming since 1993, starting with Windows application development in C++/MFC and moving to C# and .NET around 2005 and is a .NET Microsoft Certified Professional Developer.
 
Bob is the author of two books:
Billionaire: How the Ultra-Rich Built Their Fortunes Through Good and Evil and What You Can Learn from Them
and
Wealthonomics: The Most Important Economic and Financial Concepts that Can Make You Rich Fast.
Visit http://www.billionairebook.net for more information.

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 18 Nov 1999
Article Copyright 1999 by Robert Pittenger, MCPD-EAD
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid