A Class for Creating a Trace Log
来源:互联网 发布:三冠电商淘宝店怎么样 编辑:程序博客网 时间:2024/06/02 17:28
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 PMStarted the application11/4/99 10:16:44 PMCreated doc template11/4/99 10:16:44 PMCDoc constructor11/4/99 10:16:44 PMPrecreate window11/4/99 10:16:44 PMPrecreate window11/4/99 10:16:44 PMCView Constructor11/4/99 10:16:44 PMCView::Precreate window11/4/99 10:16:44 PMCreated main window11/4/99 10:16:44 PMCreated tool bar11/4/99 10:16:44 PMCreated status bar11/4/99 10:16:44 PMOnNew Document11/4/99 10:16:44 PMLeaving InitInstance11/4/99 10:16:46 PMLeaving 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/Destructionpublic:CLogTrace();~CLogTrace();// Attributespublic:CString m_strAppName;protected:BOOL m_bActive;CString m_strFileName;BOOL m_bTimeStamp;// Operationspublic: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:// Inlinespublic: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 CLogTrace1. Make a static CLogTrace object as a member of the application class2.Add the following lines to the InitInstance of the programm_LogTrace.m_strAppName = "MyApp"; // use appropriate name herem_LogTrace.SetFileName("Log.txt"); // sets the log file name and puts it in the exe pathm_LogTrace.OnStartup(TRUE, TRUE); // activates the log trace3. Also in InitInstance, add the following line if you want to empty the log fileeach time the application startsm_LogTrace.ResetFile();4. Any time you want to write to the log file, use the CLogTrace::WriteLine functionsthese will write the text along with date and time*******************************************************///////////////////////////////////////////////////////// Construction/DestructionCLogTrace::CLogTrace(){m_bActive = FALSE;m_bTimeStamp = TRUE;CString s;}CLogTrace::~CLogTrace(){}////////////////////////////////////////////////////////// CLogTrace operationsvoid 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 filevoid 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 timeWriteLine("\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 filevoid 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 stringvoid 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 integervoid 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 filevoid 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);}
- A Class for Creating a Trace Log
- Creating custom View - Creating a View Class
- Creating a workspace for catkin
- Capture a Debug Log or Trace File and Diagnostics for AutoInvoice
- Creating a Base Window Class in WPF
- python 'type' - dynamically creating a class
- Creating a View Class//建立视图类
- 自定义view(creating a View Class)
- Creating a class from scratch with Soot
- Creating a View Class 创建自定义视图
- Creating ActionScript components-for Customerlize a Combox
- Creating a Static Library for iPhone
- iPhone Tutorial for Creating a Splash Screen
- Creating a CAB file for your application
- Creating a Static Library for iPhone
- Creating a Unity Launcher for Java Applications
- A free software for easily creating setups
- 17.1.1.3 Creating a User for Replication
- Spring MVC源码分析——初始化过程
- 2013年9月7日 阿里巴巴内推面试内容
- C++基础杂记
- C++编程思想第四章笔记
- HSQLDB 和 H2 数据库比较
- A Class for Creating a Trace Log
- Windows 驱动中的IO堆栈
- Linux SVN 服务器配置以及客户端使用
- js跨域问题小结
- POJ 2387 Til the Cows Come Home
- 武大郎新闻发布会
- Android实训课程总结之四
- grep , egrep ,fgrep
- 移动互联,一场没有硝烟的战争