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.
- Make a static CLogTrace object as a member of the application class
- Add the initialization lines in the programs InitInstance function.
- 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...
#ifndef __LOGTRACE_H__
#define __LOGTRACE_H__
class CLogTrace
{
public:
CLogTrace();
~CLogTrace();
public:
CString m_strAppName;
protected:
BOOL m_bActive;
CString m_strFileName;
BOOL m_bTimeStamp;
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:
public:
inline void SetActive(BOOL bSet)
{
m_bActive = bSet;
}
inline CString GetFileName()
{
return m_strFileName;
}
};
#endif
Here is the class implementation...
#include "stdafx.h"
#include "LogTrace.h"
CLogTrace::CLogTrace()
{
m_bActive = FALSE;
m_bTimeStamp = TRUE;
CString s;
}
CLogTrace::~CLogTrace()
{
}
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();
}
void CLogTrace::OnStartup(BOOL bActive, BOOL bTimeStamp)
{
m_bActive = bActive;
m_bTimeStamp = bTimeStamp;
if (bTimeStamp == FALSE) return;
CString s;
WriteLine("\n\n******************************************\n\n");
s.Format("%s Log Trace %s\n\n", m_strAppName, COleDateTime::GetCurrentTime().Format());
WriteLine(s);
}
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();
}
void CLogTrace::WriteLine(LPCTSTR szFormat, LPCTSTR szAddInfo)
{
if (m_bActive == FALSE) return;
CString s;
s.Format(szFormat, szAddInfo);
WriteLine(s);
}
void CLogTrace::WriteLine(LPCTSTR szFormat, int nAddInfo)
{
if (m_bActive == FALSE) return;
CString s;
s.Format(szFormat, nAddInfo);
WriteLine(s);
}
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);
}