头文件Log.h
#pragma once
#define LOG_MAXCOUNT 10
#define LOG_LEVEL_INFO 0
#define LOG_LEVEL_DEBUG 1
#define LOG_LEVEL_ERROR 2
typedef struct LogInfo_T
{
public:
char AppName[255];
char LogFilePath[255];
CStringList * DebugStringList;
CStringList * ErrorStringList;
__int64 LastLogPos; //日志文件的最后读取位置
~LogInfo_T()
{
DebugStringList->RemoveAll();
ErrorStringList->RemoveAll();
delete DebugStringList;
delete ErrorStringList;
}
}LogInfi;
void SetLogLevel(int level);
//设置日志文件路径
void SetLogFilePath(const char * Path);
//写入日志文件
void WriteLog(const char * LogInfo, int LogLevel);
源文件Log.cpp
#include "StdAfx.h"
#include "commom.h"
char LogFilePath[255];
int LogLevelLimit = 2;
void SetLogFilePath(const char * path)
{
strcpy(LogFilePath, path);
}
void SetLogLevel(int level)
{
LogLevelLimit = level;
}
//LogInfo日志信息;LogLevel为日志级别,分为三级0 info, 1 Debug, 2 错误
void WriteLog(const char * info, int LogLevel)
{
if(info == NULL || strlen(info) == 0)
return;
if(LogLevel > LogLevelLimit)
return;
//获取当前时间
SYSTEMTIME tSystime;
GetLocalTime(&tSystime);
FILE * logf = fopen(LogFilePath, "a+");
if(logf == NULL)
return;
fprintf(logf, "%04d-%o2d-%02d %02d:%02d:%02d", tSystime.wYear, tSystem.wMonth, ...);
if(LogLevel == 0)
fprintf(logf, "info");
else if(logLevel == 1)
fprintf(logf, "Debug");
else if(logLevel == 2)
fprintf(logf, "Error");
fprintf(logf, "%s\n", info);
fclose(logf);
}