CubeWorldMods/logger.h

107 lines
2.9 KiB
C
Raw Permalink Normal View History

2019-10-13 13:28:38 +08:00
//logger.h
/*
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CLogger
//<2F><><EFBFBD>ܽ<EFBFBD><DCBD>ܣ<EFBFBD>Winƽ̨<C6BD><CCA8>־<EFBFBD><D6BE>¼<EFBFBD><C2BC><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD>̰߳<DFB3>ȫ<EFBFBD><C8AB>֧<EFBFBD><D6A7>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD>־<EFBFBD><D6BE>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD>־ʱ<D6BE><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><D0BA><EFBFBD>Ϣ
//<2F><><EFBFBD>ߣ<EFBFBD>sunflover 2016-1-15 14:31:27
//ʹ<>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>
1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>logger.h<EFBFBD><EFBFBD>logger.cpp<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD>
2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>logger.cpp<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷѡ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>Ԥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>
3<EFBFBD><EFBFBD>ʹ<EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#include "Logger.h"
using namespace LOGGER;
CLogger logger(LogLevel_Info,CLogger::GetAppPathA().append("log\\"));
void main()
{
logger.TraceFatal("TraceFatal %d", 1);
logger.TraceError("TraceError %s", "sun");
logger.TraceWarning("TraceWarning");
logger.TraceInfo("TraceInfo");
logger.ChangeLogLevel(LOGGER::LogLevel_Error);
logger.TraceFatal("TraceFatal %d", 2);
logger.TraceError("TraceError %s", "sun2");
logger.TraceWarning("TraceWarning");
logger.TraceInfo("TraceInfo");
}
ִ<EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>20160126_101329.log<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Fatal 2016-01-26 10:13:29 TraceFatal 1
Error 2016-01-26 10:13:29 TraceError sun
Warning 2016-01-26 10:13:29 TraceWarning
Info 2016-01-26 10:13:29 TraceInfo
Fatal 2016-01-26 10:13:29 TraceFatal 2
Error 2016-01-26 10:13:29 TraceError sun2
*/
#ifndef _LOGGER_H_
#define _LOGGER_H_
#include <Windows.h>
#include <stdio.h>
#include <string>
namespace LOGGER
{
//<2F><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ϣ
static const std::string strFatalPrefix = "Fatal\t";
static const std::string strErrorPrefix = "Error\t";
static const std::string strWarningPrefix = "Warning\t";
static const std::string strInfoPrefix = "Info\t";
//<2F><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ö<EFBFBD><C3B6>
typedef enum EnumLogLevel
{
LogLevel_Stop = 0, //ʲô<CAB2><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼
LogLevel_Fatal, //ֻ<><D6BB>¼<EFBFBD><C2BC><EFBFBD>ش<EFBFBD><D8B4><EFBFBD>
LogLevel_Error, //<2F><>¼<EFBFBD><C2BC><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
LogLevel_Warning, //<2F><>¼<EFBFBD><C2BC><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>󣬾<EFBFBD><F3A3ACBE><EFBFBD>
LogLevel_Info //<2F><>¼<EFBFBD><C2BC><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>󣬾<EFBFBD><F3A3ACBE><EFBFBD><E6A3AC>ʾ<EFBFBD><CABE>Ϣ(Ҳ<><D2B2><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>¼)
};
class CLogger
{
public:
//nLogLevel<65><6C><EFBFBD><EFBFBD>־<EFBFBD><D6BE>¼<EFBFBD>ĵȼ<C4B5><C8BC><EFBFBD><EFBFBD>ɿ<EFBFBD>
//strLogPath<74><68><EFBFBD><EFBFBD>־Ŀ¼<C4BF><C2BC><EFBFBD>ɿ<EFBFBD>
//strLogName<6D><65><EFBFBD><EFBFBD>־<EFBFBD><D6BE><EFBFBD>ƣ<EFBFBD><C6A3>ɿ<EFBFBD>
CLogger(EnumLogLevel nLogLevel = EnumLogLevel::LogLevel_Info, const std::string strLogPath = "", const std::string strLogName = "");
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
virtual ~CLogger();
public:
//д<><D0B4><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD>Ϣ
void TraceFatal(const char *lpcszFormat, ...);
//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
void TraceError(const char *lpcszFormat, ...);
//д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
void TraceWarning(const char *lpcszFormat, ...);
//д<><D0B4>ʾ<EFBFBD><CABE>Ϣ
void TraceInfo(const char *lpcszFormat, ...);
//<2F>ı<EFBFBD>д<EFBFBD><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
void ChangeLogLevel(EnumLogLevel nLevel);
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
static std::string GetAppPathA();
//<2F><>ʽ<EFBFBD><CABD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
static std::string FormatString(const char *lpcszFormat, ...);
private:
//д<>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
void Trace(const std::string &strLog);
//<2F><>ȡ<EFBFBD><C8A1>ǰϵͳʱ<CDB3><CAB1>
std::string GetTime();
//<2F>ļ<EFBFBD>ȫ·<C8AB><C2B7><EFBFBD>õ<EFBFBD><C3B5>ļ<EFBFBD><C4BC><EFBFBD>
const char *path_file(const char *path, char splitter);
private:
//д<><D0B4>־<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
FILE * m_pFileStream;
//д<><D0B4>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
EnumLogLevel m_nLogLevel;
//<2F><>־Ŀ¼
std::string m_strLogPath;
//<2F><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
std::string m_strLogName;
//<2F><>־<EFBFBD>ļ<EFBFBD>ȫ·<C8AB><C2B7>
std::string m_strLogFilePath;
//<2F>߳<EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>ٽ<EFBFBD><D9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
CRITICAL_SECTION m_cs;
};
}
#endif