一个简单的 C++ Log文本输出类

// Log.h
#ifndef LOG_H
#define LOG_H

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

using namespace std;


/**
 * 用于输出log文件的类.
 */
class Log
{
public:
    Log();
    ~Log();

    bool Open(string strFileName);
    void Close();

    bool CommonLogInit(); //打开默认的log 文件

    void Enable();
    void Disable();

    string GetTimeStr();

    template <typename T> void LogOut(const T& value)
    {
        if (m_bEnabled)
        {
            m_tOLogFile << value;
        }
    }

    template <typename T> void LogOutLn(const T& value)
    {
        if (m_bEnabled)
        {
            m_tOLogFile << value << endl;
        }
    }

    void LogOutLn()
    {
        if (m_bEnabled)
        {
            m_tOLogFile << endl;
        }
    }

    template <typename T> Log& operator<<(const T& value)
    {
        if (m_bEnabled)
        {
            m_tOLogFile << value;
        }
        return (*this);
    }

    Log& operator<<(ostream& (*_Pfn)(ostream&))
    {
        if (m_bEnabled)
        {
            (*_Pfn)(m_tOLogFile);
        }
        return (*this);
    }

private:
    template<typename T> string ValueToStr(T value)
    {
        ostringstream ost;
        ost << value;
        return ost.str();
    }
private:
    ofstream m_tOLogFile;

    bool m_bEnabled;
};


#endif

//========================
//Log.cpp

#include "Log.h"

Log::Log()
    :m_bEnabled(true)
{
}

Log::~Log()
{
}

bool Log::Open(string sFileName)
{
    m_tOLogFile.open(sFileName.c_str(), ios_base::out | ios_base::app);

    if( !m_tOLogFile )
    {
        return false;
    }

    return true;
}

void Log::Close()
{
    if(m_tOLogFile.is_open())
    {
        m_tOLogFile.close();
    }
}

bool Log::CommonLogInit()
{
    time_t tNowTime;
    time(&tNowTime);

    tm* tLocalTime = localtime(&tNowTime);

    //得到日期的字符串
    string sDateStr = ValueToStr(tLocalTime->tm_year+1900) + "-" +
        ValueToStr(tLocalTime->tm_mon+1) + "-" +
        ValueToStr(tLocalTime->tm_mday);

    return Open("Log_" + sDateStr + ".log");
}

void Log::Enable()
{
    m_bEnabled = true;
}

void Log::Disable()
{
    m_bEnabled = false;
}

//得到当前时间的字符串
string Log::GetTimeStr()
{
    time_t tNowTime;
    time(&tNowTime);

    tm* tLocalTime = localtime(&tNowTime);

    //"2011-07-18 23:03:01 ";
    string strFormat = "%Y-%m-%d %H:%M:%S ";

    char strDateTime[30] = {'\0'};
    strftime(strDateTime, 30, strFormat.c_str(), tLocalTime);

    string strRes = strDateTime;

    return strRes;
}

//本Log类,用于一般简单的Log文本文件输出.

//test: main.cpp
#include "Log.h"

int main()
{
    Log mainLog;
    mainLog.CommonLogInit();

    mainLog << mainLog.GetTimeStr() << "测试Log类." << endl;
}

http://blog.csdn.net/aheroofeast/article/details/2313150


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值