您的位置:首页 > 其它

简单的写日志工具类LogUtil

2014-01-08 23:23 375 查看
1、LogUtil.h

#ifndef __LOG_UTIL_H_
#define __LOG_UTIL_H_

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
#include <windows.h>

using namespace std;

#define  PATH_LOG_FILE ".\\LogFile.txt"

class CLogUtil
{
public:
typedef enum LOGTYPE
{
LOG_SYSTEM=0,
LOG_CRASH,
LOG_ERROR,
LOG_WARNING,
LOG_INFO,
LOG_DEBUG
} LOGTYPE;
public:
CLogUtil(const char *logname = PATH_LOG_FILE);
~CLogUtil();
public:
void begin();
void clear();
void add_time();
void record(LOGTYPE lgtype, const char *filename,const char *module,const char *info);
void add_separator(bool bHasTitle, const char *title_name);
void end();
public:
std::ofstream& operator << (const char *txt);
std::ofstream& operator << (const unsigned *txt);
std::ofstream& operator << (int txt);
private:
std::ofstream o;
std::string log_name;
std::string module_name;
};

#endif // __FCC_LOG_H__


2、LogUtil.cpp

#include "stdafx.h"
#include"logutil.h"

#define TIME_LENGTH 50

/*class CLogUtil constructor*/
CLogUtil::CLogUtil(const char *logname) : o(logname, std::ios_base::out | std::ios_base::trunc)
{
log_name = logname;
}

/*class CLogUtil destructor*/
CLogUtil::~CLogUtil()
{
if (o.is_open()) o.close();
}

void CLogUtil::begin()
{
if (!o.is_open())
{
o.open(log_name.c_str(), std::ios_base::app|std::ios_base::out);
}

}

/*clear log*/
void CLogUtil::clear()
{
o.flush();
}

/*add time in the log*/
void CLogUtil::add_time()
{
if (!o.is_open()) return;

SYSTEMTIME stCurTime;
GetLocalTime(&stCurTime);
char tmp_time[TIME_LENGTH] = {0};
char strTime[TIME_LENGTH] = {0};
sprintf(tmp_time, "%d-%d-%d ", stCurTime.wYear,stCurTime.wMonth,stCurTime.wDay);
sprintf(strTime, "%d:%d:%d", stCurTime.wHour,stCurTime.wMinute,stCurTime.wSecond);
strcat(tmp_time,strTime);

o <<"Log Time: "<<tmp_time<<std::endl;
}

void CLogUtil::record(LOGTYPE lgtype, const char *filename,const char *module,const char *info)
{
if (!o.is_open())
{
return;
}
module_name = module;
o <<"/********************************************************************/"<<std::endl;
add_time();
o <<"Log Level: ";
switch (lgtype)
{
case LOG_SYSTEM:
o << "SYSTEM";
break;
case LOG_CRASH:
o << "CRASH";
break;
case LOG_ERROR:
o << "ERROR";
break;
case LOG_WARNING:
o << "WARNING";
break;
case LOG_INFO:
o << "INFO";
break;
case LOG_DEBUG:
o << "DEBUG";
break;
default:
o << "OTHER";
break;
};

o << std::endl;
o << "Module: "<<filename<<"  ["<<module_name<<"]"<<std::endl;
o << "Log Event: "<<info << std::endl;
o << "Result: "<<std::endl;
o << "Description:"<<std::endl;
o << "========================================================================"<<std::endl;
}

void CLogUtil::add_separator(bool bHasTitle, const char *title_name)
{
}

void CLogUtil::end()
{
if (o.is_open())
o.close();
}

std::ofstream& CLogUtil::operator << (const char *txt)
{
if (o.is_open())
o << txt;
return o;
}

std::ofstream& CLogUtil::operator << (const unsigned *txt)
{
if (o.is_open())
o << txt;
return o;
}

std::ofstream& CLogUtil::operator << (int txt)
{
if (o.is_open())
o << txt;
return o;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: