从CodeProject那里找到并且剥离出来的一个Trace Log类
2012-09-09 21:27
363 查看
这个类还不错的说,希望看到原文的请到CodeProject去看,原文地址是:
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 -- 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);
}
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 -- 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类
- 找到一个二维矩阵中所有包含0的,并且把0元素所在行与列全部转换成0的算法!
- 终于找到一个不用注册的HDD Regenerator了,并且用他修复了我的一个分区的物理坏道,共享给大家。
- 问题:运行项目,却不能登录了。并且抛出来一个异常
- <仅是自己做笔记。。。系列15>实现一个挺高级的字符匹配算法: 给一串很长字符串,要求找到符合要求的字符串,例如目的串:123 1******3***2 ,12*****3这些都要找出来
- Python读取文件中一串字符串并且把其中所有的数字组成一个新的字符串,并且打印出来
- HTML一个页面输入值跳转到另一个页面并且显示出来
- CQuickList一个codeproject上的超级好用的控件
- 找到一个二维矩阵中所有包含0的,并且把0元素所在行与列全部转换成0的算法!
- 在code project上找到的声音调节类
- django系列2:安装psycopg2并且创建一个django project
- codeproject上看到的一个好的日历控件
- 软件工程设计:分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来。
- 【转帖】对codeproject上一个IOCP框架的修改
- www.codeproject.com上一个控制Windows服务的程序,用C#写服务控制程序时可以参考!
- MFC将一个位图添加到数据库中并且将其读出来到指定的Picture控件上
- 一个绚丽的带动画的.NET CF列表控件(转载from Codeproject)
- 写一个程序,分析一个文本文件(英文文章)中各个单词出现的频率,并且把频率最高的10词打印出来
- codeproject上看到的一个好的日历控件
- 12个球一个天平,现知道只有一个和其它的重量不同,问怎样称才能用三次就找到那个球,并且知道此球比其它球是轻还是重,如果13个球呢