可以进行Log日志文件的编写,便于记录代码运行的Log情况,尤其是在qt开发中,无法显示到终端的情况。
Log.h文件
#include "Log.h"
std::ofstream file(getLogFilePath());
/*
Function:
name:getLogFilePath;
param_1:none
introduce:用于为日志提供存储位置及命名
Auto:Qax
Modify History:
v1:2024/3/19 12:00 编写完成
*/
std::string getLogFilePath() {
CTime currenttime = CTime::GetCurrentTime();
std::string month, day, hour, minute;
if (currenttime.GetMonth() < 10) month = "0" + std::to_string(currenttime.GetMonth());
else month = std::to_string(currenttime.GetMonth());
if (currenttime.GetDay() < 10) day = "0" + std::to_string(currenttime.GetDay());
else day = std::to_string(currenttime.GetDay());
if (currenttime.GetHour() < 10) hour = "0" + std::to_string(currenttime.GetHour());
else hour = std::to_string(currenttime.GetHour());
if (currenttime.GetMinute() < 10) minute = "0" + std::to_string(currenttime.GetMinute());
else minute = std::to_string(currenttime.GetMinute());
std::string str = "LOG_" + std::to_string(currenttime.GetYear()) + month + day + hour + minute + ".txt";
return "C:\\Users\\Administrator\\Desktop\\log_files\\" + str;
}
/*
Function:用于初始化静态常量
*/
static const char* gs_netUvLogName[4] = {
"INFO",
"WARNING",
"ERROR",
"FATAL"
};
/*
Function:
name:NetUVLog;
param_1:用于传入日志类型
param_2:用于传入日志信息
introduce:用于写入日志信息函数
Auto:Qax
Modify History:
v1:2024/3/19 12:21 测试成功完成
*/
void NetUVLog(std::string sourcefilename, int line, NET_UV_LOG_TYPE type, std::string message) {
time_t timer;
char logTime[TIME_BUF_LEN]; //#define TIME_BUF_LEN 64
std::string logInfo;
//获取当前事件,年月日时分秒
time(&timer);
strftime(logTime, TIME_BUF_LEN, "%Y-%m-%d %H:%M:%S", localtime(&timer));
logInfo = logTime;
logInfo.append(" " + sourcefilename);
logInfo.append(" Line:" + std::to_string(line));
logInfo.append(" [");
logInfo.append(gs_netUvLogName[(int)type]);
logInfo.append("] ");
logInfo.append(message);
file << logInfo.c_str() << std::endl;
}
Log.cpp文件
#include "Log.h"
std::ofstream file(getLogFilePath());
/*
Function:
name:getLogFilePath;
param_1:none
introduce:用于为日志提供存储位置及命名
Auto:Qax
Modify History:
v1:2024/3/19 12:00 编写完成
*/
std::string getLogFilePath() {
CTime currenttime = CTime::GetCurrentTime();
std::string month, day, hour, minute;
if (currenttime.GetMonth() < 10) month = "0" + std::to_string(currenttime.GetMonth());
else month = std::to_string(currenttime.GetMonth());
if (currenttime.GetDay() < 10) day = "0" + std::to_string(currenttime.GetDay());
else day = std::to_string(currenttime.GetDay());
if (currenttime.GetHour() < 10) hour = "0" + std::to_string(currenttime.GetHour());
else hour = std::to_string(currenttime.GetHour());
if (currenttime.GetMinute() < 10) minute = "0" + std::to_string(currenttime.GetMinute());
else minute = std::to_string(currenttime.GetMinute());
std::string str = "LOG_" + std::to_string(currenttime.GetYear()) + month + day + hour + minute + ".txt";
//此处是保存地址
return "C:\\Users\\Administrator\\Desktop\\log_files\\" + str;
}
/*
Function:用于初始化静态常量
*/
static const char* gs_netUvLogName[4] = {
"INFO",
"WARNING",
"ERROR",
"FATAL"
};
/*
Function:
name:NetUVLog;
param_1:用于传入日志类型
param_2:用于传入日志信息
introduce:用于写入日志信息函数
Auto:Qax
Modify History:
v1:2024/3/19 12:21 测试成功完成
*/
void NetUVLog(std::string sourcefilename, int line, NET_UV_LOG_TYPE type, std::string message) {
time_t timer;
char logTime[TIME_BUF_LEN]; //#define TIME_BUF_LEN 64
std::string logInfo;
//获取当前事件,年月日时分秒
time(&timer);
strftime(logTime, TIME_BUF_LEN, "%Y-%m-%d %H:%M:%S", localtime(&timer));
logInfo = logTime;
logInfo.append(" " + sourcefilename);
logInfo.append(" Line:" + std::to_string(line));
logInfo.append(" [");
logInfo.append(gs_netUvLogName[(int)type]);
logInfo.append("] ");
logInfo.append(message);
file << logInfo.c_str() << std::endl;
}
在Log文件使用的时候,用
NetUVLog(__FILE__, __LINE__, NET_UV_LOG_TYPE::NET_UV_LOG_INFO, 日志信息);
至此,可完成日志的记录!
以此笔记!