更多课程 选择中心

C/C++培训
达内IT学院

400-996-5531

C++学习笔记之win32平台日志类

  • 发布:C++培训
  • 来源:学习笔记
  • 时间:2017-09-21 17:22

在win32平台调戏完全不能打印printf,不知道输出到哪里去了,只能打印日志..没办法!~~ 下面我列出日志类

mylog.h

#ifndef MYLOG_H

#define MYLOG_H

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdarg.h>

#include <time.h>

#include <string.h>

#ifdef WIN32

#include <windows.h>

#else

#include <sys/time.h>

#endif

#define LOG_BUF_LEN_MAX 1024

#define LOG_PATH_LEN_MAX 256

/* 日志报警等级 */

typedef enum

{

LEVEL_DEBUG = 0, //调试,级别最低,在系统实际运行过程中一般都是不输出的

LEVEL_INFO = 1, //信息,反馈系统的当前状态给最终用户的

LEVEL_WARN = 2, //警告,这时进行一些修复性的工作,系统能够恢复,可以继续运行下去

LEVEL_ERROR = 3, //错误,导致一个无法修复的错误

LEVEL_FATAL = 4 //致命

} LEVEL_TYPE;

//不同等级对应的字符串

const char LOG_ERROR[][8] = {

"Debug",

"Info",

"Warn",

"Error",

"Fatal"

};

class CMyLog

{

public:

CMyLog();

~CMyLog();

int InitLogCfg(LEVEL_TYPE level, const char* logFileName, unsigned long m_LogFileMaxSize); //初始化日志配置

void WriteLog(LEVEL_TYPE level, const char* format, ...); // 日志打印接口,参数可变

protected:

void PrintLog(const char* szMsg); // 打印输出信息

void GetDateString(char* szDate); // 获得日期,实现省略

void GetTimeString(char* szTime); // 获得时间,实现省略

private:

FILE *m_file; //日志文件句柄

LEVEL_TYPE m_iLevel; //日志级别

char m_logFilePath[LOG_PATH_LEN_MAX]; //日志文件名

unsigned long m_LogFileMaxSize; //日志文件最大大小

#ifdef WIN32

CRITICAL_SECTION m_cs; //函数的重入控制

#else

#endif

};

#endif

mylog.cpp

#include "MyLog.h"

extern LEVEL_TYPE g_OutputLogLevel;

//构造函数

CMyLog::CMyLog()

{

//初始化变量

m_file = NULL;

memset(m_logFilePath, 0, sizeof(m_logFilePath));

#ifdef WIN32

InitializeCriticalSection(&m_cs);

#else

#endif

}

//析构函数

CMyLog::~CMyLog()

{

if (NULL != m_file)

{

fclose(m_file);

m_file = NULL;

}

#ifdef WIN32

DeleteCriticalSection(&m_cs);

#else

#endif

}

//初始化日志配置

int CMyLog::InitLogCfg(LEVEL_TYPE level, const char* logFilePath, unsigned long logMaxSize) //初始化日志配置

{

char logFileName[LOG_PATH_LEN_MAX];

struct tm *nowtime;

time_t long_time;

time(&long_time);

nowtime = localtime(&long_time);

m_iLevel = level;

m_LogFileMaxSize = logMaxSize;

memcpy(m_logFilePath, logFilePath, strlen(logFilePath));

#ifdef WIN32

sprintf(logFileName, "%s\\MyLogFile-%04d%02d%02d%02d%02d%02d.log",

m_logFilePath, nowtime->tm_year + 1900, nowtime->tm_mon + 1, owtime->tm_mday, nowtime->tm_hour, nowtime->tm_min, owtime->tm_sec);

#else

sprintf(logFileName, "%s/MyLogFile-%04d%02d%02d%02d%02d%02d.txt",

m_logFilePath, nowtime->tm_year + 1900, nowtime->tm_mon + 1, owtime->tm_mday, nowtime->tm_hour, nowtime->tm_min, owtime->tm_sec);

#endif

if (logFileName != NULL)

{

m_file = fopen(logFileName, "w+");

if (m_file == NULL)

{

return -1;

}

}

return 1;

}

void CMyLog::WriteLog(LEVEL_TYPE logLevel, const char* format, ...)

{

char logMsg[LOG_BUF_LEN_MAX]; //日志信息字符串

if (NULL == format) //输入检查,判断是否有日志信息输入

{

return;

}

if (logLevel < m_iLevel) //等级控制,例如选择日志级别LEVEL_ERROR,那么WARN信息不会输出日志

{

return;

}

#ifdef WIN32

SYSTEMTIME sysTime; //系统时间

GetLocalTime(&sysTime); //获得系统当前时间

sprintf(logMsg, "%04d%02d%02d-%02d:%02d:%02d:%03d [%5s] ",

sysTime.wYear, sysTime.wMonth, sysTime.wDay,

sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds, LOG_ERROR[logLevel]);

#else

struct tm *nowtime;

time_t long_time;

struct timeval t;

gettimeofday(&t, NULL);

time(&long_time);

nowtime = localtime(&long_time);

sprintf(logMsg, "%04d%02d%02d-%02d:%02d:%02d:%d [%5s] ",

nowtime->tm_year + 1900, nowtime->tm_mon + 1, owtime->tm_mday,

nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec, (#_usec / 1000), LOG_ERROR[logLevel]);

#endif

va_list argp; //内容

va_start(argp, format);

#ifdef WIN32

_vsnprintf(&logMsg[strlen(logMsg)], LOG_BUF_LEN_MAX - strlen(logMsg), format, argp);

#else

vsnprintf(&logMsg[strlen(logMsg)], LOG_BUF_LEN_MAX - strlen(logMsg), format, argp);

#endif

va_end(argp);

//输出日志

#ifdef WIN32

EnterCriticalSection(&m_cs);

this->PrintLog(logMsg);

LeaveCriticalSection(&m_cs);

#else

this->PrintLog(logMsg);

#endif

}

void CMyLog::PrintLog(const char* szMsg)

{

unsigned long ul32Pos = 0; //日志大小

if (m_file == NULL) //日志文件尚未打开

{

return;

}

fseek(m_file, 0, SEEK_END);

ul32Pos = ftell(m_file);

if (ul32Pos >= m_LogFileMaxSize) //超过日志文件大小

{

char logName[256];

struct tm *nowtime;

time_t long_time;

time(&long_time);

nowtime = localtime(&long_time);

#ifdef WIN32

sprintf(logName, "%s\\MyLogFile-%04d%02d%02d%02d%02d%02d.txt",

m_logFilePath, nowtime->tm_year + 1900, nowtime->tm_mon + 1, owtime->tm_mday, nowtime->tm_hour, nowtime->tm_min, owtime->tm_sec);

#else

sprintf(logName, "%s/MyLogFile-%04d%02d%02d%02d%02d%02d.txt",

m_logFilePath, nowtime->tm_year + 1900, nowtime->tm_mon + 1, owtime->tm_mday, nowtime->tm_hour, nowtime->tm_min, owtime->tm_sec);

#endif

m_file = fopen(logName, "w+");

if (m_file == NULL)

{

return;

}

}

fprintf(m_file, "%s \n", szMsg);

fflush(m_file);

}

main.cpp

#include "MyLog.h"

CMyLog cml;

LEVEL_TYPE g_OutputLogLevel = LEVEL_DEBUG;

char logPath[256];

int main()

{

#ifdef WIN32

memcpy(logPath, "D:\\log\\MyLog", sizeof("D:\\log\\MyLog"));

#else

memcpy(logPath, "./", sizeof("./"));

#endif

if (cml.InitLogCfg(LEVEL_WARN, logPath, 3 * 1024 * 1024) < 0) //初始化日志配置,日志级别:WARN,日志最大:3M

{

printf("InitLogCfg() Fail!\n");

}

char buf = 1;

//时间 [等级] 信息内容,例如:20101220-16:32:18:453 [Error] d:\mingyangworkspace\mylog\main.cpp 27

for (int i = 0; i < 2; i++)

{

//cml.WriteLog(LEVEL_DEBUG, "%s %u %s", __FILE__, __LINE__,"dsd");

//cml.WriteLog(LEVEL_INFO, "%s %u %s ", __FILE__, __LINE__, "dsd");

cml.WriteLog(LEVEL_WARN, "%s %u %d", __FILE__, __LINE__, buf);

cml.WriteLog(LEVEL_ERROR, "%s %u %s", __FILE__, __LINE__, "dsd");

//cml.WriteLog(LEVEL_FATAL, "%s %u %s", __FILE__, __LINE__, "dsd");

}

return 0;

}

预约申请免费试听课

填写下面表单即可预约申请免费试听!怕钱不够?可就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:C++进阶-类型转换,你看我就够了
下一篇:C/C++开发程序员这些基础必须熟记!

C语言创建windows窗口实例

C++回调函数是什么?

C++ shared_ptr和动态数组

C语言有哪些关键词,C语言44个关键词大全

Copyright © 2023 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省