从CodeProject那里找到并且剥离出来的一个Trace Log类

简介:
这个类还不错的说,希望看到原文的请到CodeProject去看,原文地址是:
http://www.codeproject.com/debug/logtrace.asp

下面我把源代码放上来.
ExpandedBlockStart.gif ////////////////////////////////////////////////////////////////////////
None.gif //   LogTrace.cpp -- Interface for the CLogTrace class
None.gif
//   A class to do debug logging
None.gif

None.gif
None.gif#ifndef __LOGTRACE_H__
None.gif #define __LOGTRACE_H__
None.gif
None.gif class CLogTrace
ExpandedBlockStart.gif {
InBlock.gif// Construction/Destruction
InBlock.gif
public:
InBlock.gif    CLogTrace();
InBlock.gif    ~CLogTrace();
InBlock.gif
InBlock.gif
InBlock.gif// Attributes
InBlock.gif
public:
InBlock.gif    CString m_strAppName;
InBlock.gif
InBlock.gifprotected:
InBlock.gif    BOOL m_bActive;
InBlock.gif    CString m_strFileName;
InBlock.gif    BOOL m_bTimeStamp;
InBlock.gif
InBlock.gif// Operations
InBlock.gif
public:
InBlock.gif    void WriteLine(LPCTSTR szLine);
InBlock.gif    void WriteLine(LPCTSTR szFormat, LPCTSTR szAddInfo);
InBlock.gif    void WriteLine(LPCTSTR szFormat, int nAddInfo);
InBlock.gif    void ResetFile();
InBlock.gif    void OnStartup(BOOL bActive, BOOL bTimeStamp);
InBlock.gif    void SetFileName(LPCTSTR szFileName);
InBlock.gif
InBlock.gif
InBlock.gifprotected:
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif// Inlines
InBlock.gif
public:
InBlock.gif    inline void SetActive(BOOL bSet)
ExpandedSubBlockStart.gif    {
InBlock.gif        m_bActive = bSet;
ExpandedSubBlockEnd.gif    }

InBlock.gif    inline CString GetFileName()
ExpandedSubBlockStart.gif    {
InBlock.gif        return m_strFileName;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif
None.gif
None.gif #endif  //  __LOGTRACE_H__

 

 

ExpandedBlockStart.gif ////////////////////////////////////////////////////////////////////////
None.gif //   LogTrace.cpp -- Implementation of the CLogTrace class
None.gif

None.gif
None.gif#include "stdafx.h"
None.gif#include <afxdisp.h>
None.gif#include "LogTrace.h"
None.gif
ExpandedBlockStart.gif /**************************************************
InBlock.gif
InBlock.gif How to use CLogTrace
InBlock.gif
InBlock.gif    1.  Make a static CLogTrace object as a member of the application class
InBlock.gif
InBlock.gif    2.    Add the following lines to the InitInstance of the program
InBlock.gif
InBlock.gif    
InBlock.gif    m_LogTrace.m_strAppName = "MyApp"; // use appropriate name here
InBlock.gif
InBlock.gif    m_LogTrace.SetFileName("Log.txt"); // sets the log file name and puts it in the exe path
InBlock.gif
InBlock.gif    m_LogTrace.OnStartup(TRUE, TRUE); // activates the log trace
InBlock.gif
InBlock.gif    3.  Also in InitInstance, add the following line if you want to empty the log file
InBlock.gif    each time the application starts
InBlock.gif    
InBlock.gif    m_LogTrace.ResetFile();
InBlock.gif
InBlock.gif
InBlock.gif    4.  Any time you want to write to the log file, use the CLogTrace::WriteLine functions
InBlock.gif    these will write the text along with date and time
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif******************************************************
*/

None.gif
None.gif
None.gif
ExpandedBlockStart.gif //////////////////////////////////////////////////////
None.gif //   Construction/Destruction
None.gif

None.gifCLogTrace::CLogTrace()
ExpandedBlockStart.gif {
InBlock.gif    m_bActive = FALSE;
InBlock.gif    m_bTimeStamp = TRUE;
InBlock.gif
InBlock.gif    CString s;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gifCLogTrace::~CLogTrace()
ExpandedBlockStart.gif {
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif ////////////////////////////////////////////////////////
None.gif //   CLogTrace operations
None.gif

None.gif
None.gif void CLogTrace::ResetFile()
ExpandedBlockStart.gif {
InBlock.gif    CStdioFile f;
InBlock.gif    CFileException fe;
InBlock.gif    CString s;
InBlock.gif
InBlock.gif    if (m_strFileName.IsEmpty()) return;
InBlock.gif
InBlock.gif    if (f.Open(m_strFileName, CFile::modeWrite | CFile::modeCreate, &fe) == FALSE)
ExpandedSubBlockStart.gif    {
InBlock.gif        return;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    f.Close();
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif //  bActive tells us if we want the trace to be active or not
None.gif
//  bTimeStamp tells us if we want time stamps on each line
None.gif
//  eliminating the time stamp allows us to use this class for a regular log file
None.gif
void CLogTrace::OnStartup(BOOL bActive, BOOL bTimeStamp)
ExpandedBlockStart.gif {
InBlock.gif    m_bActive = bActive;
InBlock.gif    m_bTimeStamp = bTimeStamp;
InBlock.gif    if (bTimeStamp == FALSE) return;
InBlock.gif    CString s;
InBlock.gif
InBlock.gif    // these ***'s help to indicate when one ru of the program ends and another starts
InBlock.gif    
// because we don't always overwrite the file each time
InBlock.gif

InBlock.gif    WriteLine("\n\n******************************************\n\n");
InBlock.gif    s.Format("%s Log Trace %s\n\n", m_strAppName, COleDateTime::GetCurrentTime().Format());
InBlock.gif    WriteLine(s);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif //  function to write a line of text to the log file
None.gif
void CLogTrace::WriteLine(LPCTSTR szLine)
ExpandedBlockStart.gif {
InBlock.gif    CStdioFile f;
InBlock.gif    CFileException fe;
InBlock.gif    CString s;
InBlock.gif
InBlock.gif    if (m_bActive == FALSE) return;
InBlock.gif    if (m_strFileName.IsEmpty()) return;
InBlock.gif
InBlock.gif    if (f.Open(m_strFileName, CFile::modeWrite | CFile::modeCreate |
InBlock.gif        CFile::modeNoTruncate, &fe) == FALSE)
ExpandedSubBlockStart.gif    {
InBlock.gif        return;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    try
ExpandedSubBlockStart.gif    {
InBlock.gif        f.SeekToEnd();
InBlock.gif        TRACE("LOGGIN %s\n", szLine);
InBlock.gif        if (m_bTimeStamp)
ExpandedSubBlockStart.gif        {
InBlock.gif            s.Format("%s\t%s\n", COleDateTime::GetCurrentTime().Format(),
InBlock.gif                szLine);
ExpandedSubBlockEnd.gif        }

InBlock.gif        else
ExpandedSubBlockStart.gif        {
InBlock.gif            s.Format("%s\n", szLine);
ExpandedSubBlockEnd.gif        }

InBlock.gif        f.WriteString(s);
ExpandedSubBlockEnd.gif    }

InBlock.gif    catch (CException* e)
ExpandedSubBlockStart.gif    {
InBlock.gif        e->Delete();
ExpandedSubBlockEnd.gif    }

InBlock.gif    f.Close();
ExpandedBlockEnd.gif}

None.gif
None.gif //  function to write a line of text, with an extra string
None.gif
void CLogTrace::WriteLine(LPCTSTR szFormat, LPCTSTR szAddInfo)
ExpandedBlockStart.gif {
InBlock.gif    if (m_bActive == FALSE) return;
InBlock.gif    CString s;
InBlock.gif    s.Format(szFormat, szAddInfo);
InBlock.gif    WriteLine(s);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif //  funtion to write a line of text with an extra integer
None.gif
void CLogTrace::WriteLine(LPCTSTR szFormat,  int nAddInfo)
ExpandedBlockStart.gif {
InBlock.gif    if (m_bActive == FALSE) return;
InBlock.gif    CString s;
InBlock.gif    s.Format(szFormat, nAddInfo);
InBlock.gif    WriteLine(s);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif //  function to set the log file name.  don't pass a fill path!
None.gif
//  just pass something like "log.txt"
None.gif
//  the file will be placed in the same dir as the exe file
None.gif
void CLogTrace::SetFileName(LPCTSTR szFileName)
ExpandedBlockStart.gif {
InBlock.gif    TCHAR drive[_MAX_PATH], dir[_MAX_PATH], name[_MAX_PATH], ext[_MAX_PATH];
InBlock.gif
InBlock.gif    const char *path = _pgmptr ;
InBlock.gif
InBlock.gif    _splitpath(path, drive, dir, name, ext);
InBlock.gif
InBlock.gif    m_strFileName.Format("%s%s%s", drive, dir, szFileName);
InBlock.gif
ExpandedBlockEnd.gif}
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
2月前
|
Java 计算机视觉 Python
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进1】
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进1】
31 1
|
2月前
|
人工智能 算法 计算机视觉
我的自描外挂制作日志——FPS类游戏的自瞄【构思准备】
我的自描外挂制作日志——FPS类游戏的自瞄【构思准备】
42 0
|
3月前
|
前端开发
muduo源码剖析之AsyncLogging异步日志类
AsyncLogging是muduo的日志,程序如果直接让文件写日志可能会发生阻塞,muduo前端设计了2个BufferPtr,分别是currentBuffer_和nextBuffer_,还有一个存放BufferPtr的vector(buffers_)。多个前端线程往currentBuffer_写数据,currentBuffer_写满了将其放入buffers_,通知后端线程读。前端线程将currentBuffer_和nextBuffer_替换继续写currentBuffer_。
24 0
|
2月前
|
Java 计算机视觉
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进2】
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进2】
21 0
|
2月前
|
计算机视觉
我的自描外挂制作日志——FPS类游戏的自瞄【验证猜想】
我的自描外挂制作日志——FPS类游戏的自瞄【验证猜想】
25 1
|
3月前
重写 AppiumService 类,添加默认启动参数,并实时显示启动日志
重写 AppiumService 类,添加默认启动参数,并实时显示启动日志
24 0
|
9月前
|
缓存 Python
python使用类装饰器生成函数的使用日志
在了解类装饰器之前,建议大家先了解装饰器的概念。 装饰器知识快速入门链接 类装饰器是 Python 中的一种特殊类型的装饰器,它是一个类而不是一个函数。与函数装饰器不同,类装饰器可以在运行时接收参数并返回一个可调用的对象,而不是直接替换被装饰的函数。
|
10月前
|
存储 SQL 缓存
MySQL-四大类日志
MySQL-四大类日志
|
10月前
|
编译器 C++
c++入门学习日志 -- 类 和 对象
c++入门学习日志 -- 类 和 对象
49 0
|
10月前
|
编译器 C语言 C++
C++服务器框架开发9——日志系统LogFormatter_4/各个类的关系梳理/std::function/std::get
C++服务器框架开发9——日志系统LogFormatter_4/各个类的关系梳理/std::function/std::get

热门文章

最新文章