一個簡單的 C++ Log文本輸出類

來源:互聯網
上載者:User
// 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.