从CodeProject那里找到并且剥离出来的一个Trace Log类
来源:互联网 发布:js获取屏幕分辨率 编辑:程序博客网 时间:2024/04/28 17:19
这个类还不错的说,希望看到原文的请到CodeProject去看,原文地址是:
http://www.codeproject.com/debug/logtrace.asp
下面我把源代码放上来.
http://www.codeproject.com/debug/logtrace.asp
下面我把源代码放上来.
/**/////////////////////////////////////////////////////////////////////////
// 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__
// 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__
/**/////////////////////////////////////////////////////////////////////////
// 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);
}
// 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);
}
- 从CodeProject那里找到并且剥离出来的一个Trace Log类
- 从CodeProject那里找到并且剥离出来的一个Trace Log类
- 找到一个Delphi xe帮助文档剥离的方法
- 把业务流程从操作中剥离出来
- 把业务流程从操作中剥离出来
- 把业务流程从操作中剥离出来
- 把业务流程从操作中剥离出来
- 一个简单明了的c++ trace log 实现
- 从一个老工程师那里得到的终告
- 把控件中的代码剥离出来单独成一个dll
- 分散處理將是未來的 mud 必需具備的能力 (转载:不知道自己从那里找到的:))
- uiwdt,从LOG中找到相应的信息
- 查看自己trace出来的文件路径
- 从高手那里转载的,关于容器
- 从朋友那里转的链表。
- 从波利亚那里偷来的一些提问
- 从后台传内容到前台并且打印出来的方法
- 从网上找到的一个quartz的一个例子
- 移动互联,一场没有硝烟的战争
- 愿意为你少吃点
- 修身齐家治国平天下
- CreateIoCompletionPort函数和完成端口
- hdu 4276 The Ghost Blows Light(2012 ACM/ICPC Asia Regional Changchun Online )
- 从CodeProject那里找到并且剥离出来的一个Trace Log类
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions
- Red Hat Enterprise Linux 6.3 (RHEL6.3)ISO抢鲜下载
- shell流程控制学习
- u-boot2010.03 移植篇(二)-----修改start.S,支持nand启动
- 一起读nodejs(六)----工具类(Utilties)
- 提取QQ游戏图标并显示
- 0909 - 天津网络赛 - hdu 4278 - Faulty Odometer
- 国产jquery表单验证控件