CubeWorldMods/LocalizationMod/logger.h

107 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//logger.h
/*
//类名CLogger
//功能介绍Win平台日志记录功能多线程安全支持写日志级别的设置日志格式包含日志等级日志时间文件名行号信息
//作者sunflover 2016-1-15 14:31:27
//使用方法:
1将logger.hlogger.cpp添加到项目中
2设置logger.cpp的预编译头选项为“不使用预编译头”
3使用代码示例
#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");
}
执行结果20160126_101329.log文件内容如下
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
{
//日志级别的提示信息
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";
//日志级别枚举
typedef enum EnumLogLevel
{
LogLevel_Stop = 0, //什么都不记录
LogLevel_Fatal, //只记录严重错误
LogLevel_Error, //记录严重错误,普通错误
LogLevel_Warning, //记录严重错误,普通错误,警告
LogLevel_Info //记录严重错误,普通错误,警告,提示信息(也就是全部记录)
};
class CLogger
{
public:
//nLogLevel日志记录的等级可空
//strLogPath日志目录可空
//strLogName日志名称可空
CLogger(EnumLogLevel nLogLevel = EnumLogLevel::LogLevel_Info, const std::string strLogPath = "", const std::string strLogName = "");
//析构函数
virtual ~CLogger();
public:
//写严重错误信息
void TraceFatal(const char *lpcszFormat, ...);
//写错误信息
void TraceError(const char *lpcszFormat, ...);
//写警告信息
void TraceWarning(const char *lpcszFormat, ...);
//写提示信息
void TraceInfo(const char *lpcszFormat, ...);
//改变写日志级别
void ChangeLogLevel(EnumLogLevel nLevel);
//获取程序运行路径
static std::string GetAppPathA();
//格式化字符串
static std::string FormatString(const char *lpcszFormat, ...);
private:
//写文件操作
void Trace(const std::string &strLog);
//获取当前系统时间
std::string GetTime();
//文件全路径得到文件名
const char *path_file(const char *path, char splitter);
private:
//写日志文件流
FILE * m_pFileStream;
//写日志级别
EnumLogLevel m_nLogLevel;
//日志目录
std::string m_strLogPath;
//日志的名称
std::string m_strLogName;
//日志文件全路径
std::string m_strLogFilePath;
//线程同步的临界区变量
CRITICAL_SECTION m_cs;
};
}
#endif