C++记录debug信息的log类
取自:http://www.viksoe.dk/code/all_mfc.htm,里面有各种MFC常用的类
// LogFile.h: interface for the CLogFile class.
//
// Written by Bjarke Viksoe (bjarke@viksoe.dk)
// Copyright (c) 2000.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//////////////////////////////////////////////////////////////////////// #if !defined(AFX_LOGFILE_H__0EE45201_E8F9_11D1_93C1_241C08C10000__INCLUDED_)
#define AFX_LOGFILE_H__0EE45201_E8F9_11D1_93C1_241C08C10000__INCLUDED_ #if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000 // A small class implementing a debug log file.
class CLogFile : public CObject
{
public:
CLogFile();
CLogFile(LPCTSTR Filename);
virtual ~CLogFile(); // Methods
public:
// Creates (removes previous) log file.
RETCODE Create(LPCTSTR Filename, LPCTSTR Text);
// Set the filename of the log fil to use.
RETCODE SetFilename(LPCTSTR Filename);
// Creates or appends to an exisiting log file.
RETCODE AppendText(LPCTSTR Text, ...);
// Writes System Information to the log
RETCODE LogSystemInformation(); // Variables
protected:
CString m_Filename; // The log file we're currently writing to.
}; #endif // !defined(AFX_LOGFILE_H__0EE45201_E8F9_11D1_93C1_241C08C10000__INCLUDED_)
LogFile.cpp
// LogFile.cpp: implementation of the CLogFile class.
//
// A small class which can be used for debug logs.
//
////////////////////////////////////////////////////////////////////// #include "stdafx.h"
#include "LogFile.h" #ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif //////////////////////////////////////////////////////////////////////
// Construction/Destruction
////////////////////////////////////////////////////////////////////// CLogFile::CLogFile()
{
} CLogFile::CLogFile(LPCTSTR Filename)
{
SetFilename( Filename );
} CLogFile::~CLogFile()
{
} //////////////////////////////////////////////////////////////////////
// Methods
////////////////////////////////////////////////////////////////////// RETCODE CLogFile::Create(LPCTSTR Filename, LPCTSTR Text)
{
ASSERT( Filename );
ASSERT( Text );
m_Filename = Filename;
ASSERT( !m_Filename.IsEmpty() );
if( m_Filename.IsEmpty() ) return RET_INVALIDARGS;
// Write text to (text)file
CStdioFile f;
TRY
{
BOOL res = f.Open( Filename, CFile::modeCreate|CFile::modeWrite|CFile::typeText );
if( res ) {
f.WriteString( Text );
f.WriteString( _T("\n") );
f.Close();
};
}
CATCH_ALL(e)
{
f.Abort();
#ifdef _DEBUG
e->ReportError();
#endif
return RET_ERROR;
}
END_CATCH_ALL;
return RET_OK;
}; RETCODE CLogFile::AppendText(LPCTSTR Text, ...)
{
ASSERT(AfxIsValidString(Text));
ASSERT(!m_Filename.IsEmpty());
if( m_Filename.IsEmpty() ) return RET_NOTINITIALIZED;
// Append text to (text)file
CStdioFile f;
CString sText;
va_list args;
va_start(args, Text);
sText.FormatV(Text, args);
TRY
{
BOOL res = f.Open( m_Filename, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::typeText );
if( res ) {
f.SeekToEnd();
f.WriteString( sText );
f.WriteString( _T("\n") );
f.Close();
};
}
CATCH_ALL(e)
{
f.Abort();
#ifdef _DEBUG
e->ReportError();
#endif
return RET_FILEERROR;
}
END_CATCH_ALL;
return RET_OK;
}; RETCODE CLogFile::SetFilename(LPCTSTR Filename)
// Sets the log filename. A new log file will
// be created if the file does not exist.
{
ASSERT(AfxIsValidString(Filename));
m_Filename = Filename;
if( m_Filename.IsEmpty() ) return RET_INVALIDARGS;
return RET_OK;
} RETCODE CLogFile::LogSystemInformation()
// Write some standard system information to
// the log file.
{
ASSERT(!m_Filename.IsEmpty());
if( m_Filename.IsEmpty() ) return RET_NOTINITIALIZED; SYSTEMTIME time;
::GetLocalTime( &time );
AppendText(_T("Date: %04d-%02d-%02d Time: %02d:%02d:%02d"),
time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond); OSVERSIONINFO verinfo;
verinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&verinfo);
AppendText(_T("Win%s Version %d.%.2d (build %d) %s\n"),
(verinfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? _T("NT") : _T("")),
verinfo.dwMajorVersion,
verinfo.dwMinorVersion,
verinfo.dwBuildNumber,
verinfo.szCSDVersion); SYSTEM_INFO sysinfo;
LPCTSTR pszProcessor;
::GetSystemInfo(&sysinfo);
switch( sysinfo.dwProcessorType ) {
case PROCESSOR_INTEL_386:
case PROCESSOR_INTEL_486:
case PROCESSOR_INTEL_PENTIUM:
pszProcessor = _T("Intel ");
break;
case PROCESSOR_MIPS_R4000:
pszProcessor = _T("MIPS R");
break;
case PROCESSOR_ALPHA_21064:
pszProcessor = _T("DEC Alpha ");
break;
default:
pszProcessor = _T("Chipset ");
break;
}
return AppendText(_T("%s%d, %d Processor(s)\n"),
(LPCTSTR)pszProcessor,
sysinfo.dwProcessorType,
sysinfo.dwNumberOfProcessors);
};
C++记录debug信息的log类的更多相关文章
- 【技巧】Java工程中的Debug信息分级输出接口
也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...
- 【技巧】Java工程中的Debug信息分级输出接口及部署模式
也许本文的标题你们没咋看懂.但是,本文将带大家领略输出调试的威力. 灵感来源 说到灵感,其实是源于笔者在修复服务器的ssh故障时的一个发现. 这个学期初,同袍(容我来一波广告产品页面,同袍官网)原服务 ...
- 谁记录了mysql error log中的超长信息
[问题] 最近查看MySQL的error log文件时,发现有很多服务器的文件中有大量的如下日志,内容很长(大小在200K左右),从记录的内容看,并没有明显的异常信息. 有一台测试服务器也有类似的问题 ...
- 谁记录了mysql error log中的超长信息(记pt-stalk一个bug的定位过程)
[问题] 最近查看MySQL的error log文件时,发现有很多服务器的文件中有大量的如下日志,内容很长(大小在200K左右),从记录的内容看,并没有明显的异常信息. 有一台测试服务器也有类似的问题 ...
- CentOS下查看最后登录的用户信息以及LOG记录
CentOS下查看最后登录的用户信息tail /var/log/messagestail /var/log/secure 我们知道,在redhat下可以用lastlog查看各用户最后登录的信息,用la ...
- Android学习笔记Log类输出日志信息
Log类提供的方法 代码示例 .. Log.e(TAG,"[错误信息]"); Log.w(TAG,"[警告信息]"); Log.i(TAG,"[普通信 ...
- .net 创建属于自己的log类
实习到现在已经接近三个月了,由于是校企联合培养计划,所以没有工资,所幸公司对于我们这些实习生并没有什么要求,刚开始我还觉得要做点什么才能学得快,可是到了后来,发现公司安排给我们的任务并不紧要,也不算太 ...
- Log4Net在MVC下的配置以及运用线程队列记录异常信息
Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过日志查看系统的运行过程,从而发现系统的问题.日志的作用:将运 ...
- VC++ 一个简单的Log类
在软件开发中,为程序建立Log日志是很必要的,它可以记录程序运行的状态以及出错信息,方便维护和调试. 下面实现了一个简单的Log类,使用非常简单,仅供参考. // CLogHelper.h : hea ...
随机推荐
- Mongodb(3)插入文档,更新文档,删除文档
insert() 方法 要插入数据到 MongoDB 集合,需要使用 MongoDB 的 insert() 或 save() 方法. 插入文档:db.COLLECTION_NAME.insert(d ...
- animateWithDuration 动画的速度选择
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnim ...
- WCF bindings comparison z
Binding Protocol/Transport Message Encoding Security Default Session Transaction Duplex BasicHttpBin ...
- spring声明式事务
一.传统事务 二.通过spring配置完成事务: 1.配置spring,加入spring的jar包,加入spring的配置文件 2.配置数据源,这里使用c3p0,加入c3p0 jar包和mysql数据 ...
- 使用 cURL 获取站点的各类响应时间 – dns解析时间,响应时间,传输时间
http://zhangrenfang8738.blog.163.com/blog/static/95401881201142711450245/ curl监控站点响应时间 2011-05-27 11 ...
- 为什么wait(),notify()和notifyAll()必须在同步块或同步方法中调
我们常用wait(),notify()和notifyAll()方法来进行线程间通信.线程检查一个条件后就行进入等待状态,例如,在"生产者-消费者"模型中,生产者线程发现缓冲区满了就 ...
- Flask + Gunicorn + Nginx 部署
最近很多朋友都在问我关于 Flask 部署的问题,说实在的我很乐意看到和回答这样的问题,至少证明了越来越多人开始用 Flask 了. 之前我曾发表过一篇在 Ubuntu 上用 uwsgi + ngin ...
- How do I implement a cancelable event?
n System.ComponentModel, there's a class called CancelEventArgs which contains a Cancel member that ...
- isPowerOfTwo
//Given an integer, write a function to determine if it is a power of two. public class isPowerOfTwo ...
- SHARED_POOL_RESERVED_SIZE参数的设置及作用 -ZHUANZAI
还有一个参数是需要提及的:shared_pool_reserved_size.该参数指定了保留的共享池空间,用于满足将来的大的连续的共享池空间请求.当共享池出现过多碎片,请求大块空间会导致Oracle ...