C++之LOG文件

文章介绍了如何在Qt开发中使用Log.h和Log.cpp文件来编写和记录日志,包括获取当前时间生成文件名、初始化日志类型常量以及NetUVLog函数的使用方法。
摘要由CSDN通过智能技术生成

        可以进行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, 日志信息);

        至此,可完成日志的记录!

        以此笔记!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农永闯天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值