您的位置:首页 > 编程语言 > C语言/C++

一个小巧的C++Log输出到文件类

2016-07-05 10:56 288 查看


一个小巧的C++Log输出到文件类 (转)

作者:wangyin159




http://www.cnblogs.com/mazhenyu/p/4139352.html




一个小巧的C++Log输出到文件类 (转)

 
http://blog.csdn.net/dpsying/article/details/17122739
有时候需要输出一些程序运行的信息,供我们不需要调试就可以直接查看程序运行状态。所以我们需要在程序中加入一些LOG输出。

适合涉及到虚拟机调试一些关于驱动等的程序时,或进行远程调试时。

搜了些log工具,不够轻……还是简单实现下吧

贴上来,可能有用的上:

Log.h

/**
* 用于输出log文件的类.
*/

#ifndef LOG_H
#define LOG_H

//log文件路径
#define LOG_FILE_NAME "log.txt"

//启用开关
#define LOG_ENABLE

#include <fstream>
#include <string>
#include <ctime>

using namespace std;

class CLog
{
public:
static void GetLogFilePath(CHAR* szPath)
{
GetModuleFileNameA( NULL, szPath, MAX_PATH ) ;
ZeroMemory(strrchr(szPath,_T('\\')), strlen(strrchr(szPath,_T('\\') ) )*sizeof(CHAR)) ;
strcat(szPath,"\\");
strcat(szPath,LOG_FILE_NAME);
}
//输出一个内容,可以是字符串(ascii)、整数、浮点数、布尔、枚举
//格式为:[2011-11-11 11:11:11] aaaaaaa并换行
template <class T>
static void WriteLog(T x)
{
CHAR szPath[MAX_PATH] = {0};
GetLogFilePath(szPath);

ofstream fout(szPath,ios::app);
fout.seekp(ios::end);
fout << GetSystemTime() << x <<endl;
fout.close();
}

//输出2个内容,以等号连接。一般用于前面是一个变量的描述字符串,后面接这个变量的值
template<class T1,class T2>
static void WriteLog2(T1 x1,T2 x2)
{
CHAR szPath[MAX_PATH] = {0};
GetLogFilePath(szPath);
ofstream fout(szPath,ios::app);
fout.seekp(ios::end);
fout << GetSystemTime() << x1 <<" = "<<x2<<endl;
fout.close();
}

//输出一行当前函数开始的标志,宏传入__FUNCTION__
template <class T>
static void WriteFuncBegin(T x)
{
CHAR szPath[MAX_PATH] = {0};
GetLogFilePath(szPath);
ofstream fout(szPath,ios::app);
fout.seekp(ios::end);
fout << GetSystemTime() << " --------------------"<<x<<" Begin--------------------" <<endl;
fout.close();
}

//输出一行当前函数结束的标志,宏传入__FUNCTION__
template <class T>
static void WriteFuncEnd(T x)
{
CHAR szPath[MAX_PATH] = {0};
GetLogFilePath(szPath);
ofstream fout(szPath,ios::app);
fout.seekp(ios::end);
fout << GetSystemTime() << "--------------------"<<x<<" End --------------------" <<endl;
fout.close();
}

private:
//获取本地时间,格式如"[2011-11-11 11:11:11] ";
static string GetSystemTime()
{
time_t tNowTime;
time(&tNowTime);
tm* tLocalTime = localtime(&tNowTime);
char szTime[30] = {'\0'};
strftime(szTime, 30, "[%Y-%m-%d %H:%M:%S] ", tLocalTime);
string strTime = szTime;
return strTime;
}

};

#ifdef LOG_ENABLE

//用下面这些宏来使用本文件
#define LOG(x) CLog::WriteLog(x); //括号内可以是字符串(ascii)、整数、浮点数、bool等
#define LOG2(x1,x2) CLog::WriteLog2(x1,x2);
#define LOG_FUNC LOG(__FUNCTION__) //输出当前所在函数名
#define LOG_LINE LOG(__LINE__) //输出当前行号
#define LOG_FUNC_BEGIN CLog::WriteFuncBegin(__FUNCTION__); //形式如:[时间]"------------FuncName Begin------------"
#define LOG_FUNC_END CLog::WriteFuncEnd(__FUNCTION__); //形式如:[时间]"------------FuncName End------------"

#else

#define LOG(x)
#define LOG2(x1,x2)
#define LOG_FUNC
#define LOG_LINE
#define LOG_FUNC_BEGIN
#define LOG_FUNC_END

#endif

#endif


使用:

直接在需要输出日志的地方使用宏LOG(text)就可以了,记得包含头文件Log.h。

#include "Log.h"  

 

BOOL   

  

  

  

int float BOOL enum )  

return  

效果: 


分类: Bug调试C/C++
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息