C++之LOG文件

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

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

        以此笔记!

相关推荐

  1. C++LOG文件

    2024-04-02 12:18:01       40 阅读
  2. ElasticSearchSlow Log

    2024-04-02 12:18:01       57 阅读
  3. Python模块logging

    2024-04-02 12:18:01       97 阅读
  4. MySQL 8.0 架构 错误日志文件(Error Log)(1)

    2024-04-02 12:18:01       42 阅读
  5. qt log 输出为文件

    2024-04-02 12:18:01       23 阅读
  6. log-01-日志组件 Log4j 入门介绍

    2024-04-02 12:18:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 12:18:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 12:18:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 12:18:01       87 阅读
  4. Python语言-面向对象

    2024-04-02 12:18:01       96 阅读

热门阅读

  1. 区块链到底是啥呢?

    2024-04-02 12:18:01       39 阅读
  2. 第十六章 Redies

    2024-04-02 12:18:01       31 阅读
  3. springmvc实现文件上传功能

    2024-04-02 12:18:01       32 阅读
  4. wpf datagrid显示列

    2024-04-02 12:18:01       37 阅读
  5. 3453: 【PY】餐厅AA制付费

    2024-04-02 12:18:01       36 阅读
  6. 音视频技术应用方向概述

    2024-04-02 12:18:01       29 阅读
  7. 4.1作业

    4.1作业

    2024-04-02 12:18:01      33 阅读
  8. ElasticSearch 实战:ElasticSearch文档多条件查询

    2024-04-02 12:18:01       33 阅读