简单的写日志工具类LogUtil

本文介绍了一个简单的日志工具类LogUtil,用于在开发过程中方便地记录各种信息,提高调试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值