33_更新日志类

在原有只有Info函数的基础上,增加了各种类型函数(DEBUG,ERROR)的输出,使日志类更专业

CELLLog.hpp的修改

#ifndef _CELL_LOG_HPP_
#define _CELL_LOG_HPP_

//#include"CELL.hpp"
#include"CELLTask.hpp"
#include<ctime>
class CELLLog
{
    //Info  普通信息
    //Debug 调试信息,只在debug模式起作用
    //Warring 警告信息
    //Error 错误信息
#ifdef _DEBUG
    #ifndef CELLLog_Debug
        #define CELLLog_Debug(...) CELLLog::Debug(__VA_ARGS__)
    #endif
#else
    #ifndef CELLLog_Debug
        #define CELLLog_Debug(...)
    #endif
#endif // _DEBUG

#define CELLLog_Info(...) CELLLog::Info(__VA_ARGS__)
#define CELLLog_Warring(...) CELLLog::Warring(__VA_ARGS__)
#define CELLLog_Error(...) CELLLog::Error(__VA_ARGS__)

private:
    CELLLog()
    {
        _taskServer.Start();
    }

    ~CELLLog()
    {
        _taskServer.Close();
        if (_logFile)
        {
            Info("CELLLog fclose(_logFile)");
            fclose(_logFile);
            _logFile = nullptr;
        }
    }
public:
    static CELLLog& Instance()
    {
        static  CELLLog sLog;
        return sLog;
    }

    void setLogPath(const char* logName, const char* mode, bool hasDate)
    {
        if (_logFile)
        {
            Info("CELLLog::setLogPath _logFile != nullptr");
            fclose(_logFile);
            _logFile = nullptr;
        }
        //
        static char logPath[256] = {};
        //
        if (hasDate)
        {
            auto t = system_clock::now();
            auto tNow = system_clock::to_time_t(t);
            std::tm* now = std::localtime(&tNow);
            sprintf(logPath, "%s[%d-%d-%d_%d-%d-%d].txt", logName, now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
        }else {
            sprintf(logPath, "%s.txt", logName);
        }

        //
        _logFile = fopen(logPath, mode);
        if (_logFile)
        {
            Info("CELLLog::setLogPath success,<%s,%s>", logPath, mode);
        }
        else {
            Info("CELLLog::setLogPath failed,<%s,%s>", logPath, mode);
        }
    }

    static void Error(const char* pStr)
    {
        Error("%s", pStr);
    }

    template<typename ...Args>
    static void Error(const char* pformat, Args ... args)
    {
        Echo("Error ", pformat, args...);
    }

    static void Warring(const char* pStr)
    {
        Warring("%s", pStr);
    }

    template<typename ...Args>
    static void Warring(const char* pformat, Args ... args)
    {
        Echo("Warring ", pformat, args...);
    }

    static void Debug(const char* pStr)
    {
        Debug("%s", pStr);
    }

    template<typename ...Args>
    static void Debug(const char* pformat, Args ... args)
    {
        Echo("Debug ", pformat, args...);
    }

    static void Info(const char* pStr)
    {
        Info("%s", pStr);
    }

    template<typename ...Args>
    static void Info(const char* pformat, Args ... args)
    {
        Echo("Info ",pformat, args...);
    }

    template<typename ...Args>
    static void Echo(const char* type, const char* pformat, Args ... args)
    {
        CELLLog* pLog = &Instance();
        pLog->_taskServer.addTask([=]() {
            if (pLog->_logFile)
            {
                auto t = system_clock::now();
                auto tNow = system_clock::to_time_t(t);
                //fprintf(pLog->_logFile, "%s", ctime(&tNow));
                std::tm* now = std::localtime(&tNow);
                fprintf(pLog->_logFile, "%s", type);
                fprintf(pLog->_logFile, "[%d-%d-%d %d:%d:%d]", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);
                fprintf(pLog->_logFile, pformat, args...);
                fprintf(pLog->_logFile, "%s", "\n");
                fflush(pLog->_logFile);

            }
            printf("%s", type);
            printf(pformat, args...);
            printf("%s", "\n");
        });
    }
private:
    FILE* _logFile = nullptr;
    CELLTaskServer _taskServer;
};

#endif // !_CELL_LOG_HPP_

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top