C++linux下使用clog和重定向实现写日志

C++linux下使用clog和重定向实现写日志

实现文件

LogUtil.hpp

/**
* 通用日志实现
* lsl
* 2024-06-04
*/

#ifndef LOGUTIL_HPP
#define LOGUTIL_HPP
#include<iostream>
#include <time.h>
#include <cstring>
#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1):__FILE__)
char* __DATETIME__(){ char format[32]="%Y-%m-%d %X";time_t tt = time(NULL);struct tm ltm;localtime_r(&tt, &ltm);char*s=new char[32];strftime(s, 32, format, &ltm);return s; };
#define LogInfo(msg) (std::clog<<"["<<__DATETIME__()<<"][INFO]["<<__FILENAME__<<"]["<<__FUNCTION__<<":"<<__LINE__<<"] "<<msg<<std::endl)
#define LogWarn(msg) (std::clog<<"["<<__DATETIME__()<<"][WARN]["<<__FILENAME__<<"]["<<__FUNCTION__<<":"<<__LINE__<<"] "<<msg<<std::endl)
#define LogError(msg) (std::clog<<"["<<__DATETIME__()<<"][ERROR]["<<__FILENAME__<<"]["<<__FUNCTION__<<":"<<__LINE__<<"] "<<msg<<std::endl)
#endif // LOGUTIL_HPP

DATETIME()的实现输出当前的日期时间,只有年月日时分秒,没有毫秒。如果需要毫秒,可以使用下面的方法是实现。

#include <iostream>
#include <chrono>
#include <iomanip>
 
int main() {
    // 获取当前时间点
    auto now = std::chrono::system_clock::now();
 
    // 将时间转换为time_t以便进一步转换为tm结构
    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
 
    // 转换为tm结构
    std::tm* now_tm = std::localtime(&now_time);
 
    // 获取秒和毫秒
    int millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
 
    // 输出时间和毫秒
    std::cout << std::put_time(now_tm, "%Y-%m-%d %H:%M:%S") << '.' << std::setfill('0') << std::setw(3) << millis << std::endl;
 
    return 0;
}

基本功能

三种日志类型,INFO,WARN,ERROR
日志携带日期时间
日志携带文件名、函数名和日志所在行。

测试

prog1.cc

#include<LogUtil.hpp>
int main(){
    LogInfo("测试"<<11<<"hahah ");
    LogWarn("测试2"<<11<<"hahah ");
    LogError("测试3"<<11<<"hahah ");
    return 0;
}

编译

g++ prog1.cc LogUtil.hpp -I./ -o logutil

运行

./logutil &>log_20240604.log
[root@localhost myPractice]# ./logutil &>log_20240604.log
[root@localhost myPractice]# cat log_20240604.log 
[2024-06-04 18:02:01][INFO][prog1.cc][main:3] 测试11hahah 
[2024-06-04 18:02:01][WARN][prog1.cc][main:4] 测试211hahah 
[2024-06-04 18:02:01][ERROR][prog1.cc][main:5] 测试311hahah 

额外知识点

  1. main函数是int类型,return 0说明程序正常退出。 通过命令 echo &? 能查看到退出情况。
  2. 重定向 < 输入,对应cin, 标准输出1>,对应cout, 错误输出2>,对应cerr,clog。
  3. 1>>tt.log 追加输出, 2>>tt.log, &>>tt.log 所有追加输出到一个文件。

相关推荐

  1. C++linux使用clog定向实现

    2024-06-08 20:36:02       23 阅读

最近更新

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

    2024-06-08 20:36:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 20:36:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 20:36:02       82 阅读
  4. Python语言-面向对象

    2024-06-08 20:36:02       91 阅读

热门阅读

  1. 使用安装包安装飞桨寒武纪版本@启智(未通过)

    2024-06-08 20:36:02       34 阅读
  2. 《青少年编程与数学》课程方案:4、课程策略

    2024-06-08 20:36:02       29 阅读
  3. 速盾:DDoS高防IP上设置转发规则

    2024-06-08 20:36:02       31 阅读
  4. 在Pycharm中的命令行窗口中实现清屏的命令

    2024-06-08 20:36:02       32 阅读
  5. reset database to incarnation rman 恢复最早的全备方法

    2024-06-08 20:36:02       24 阅读
  6. C++的内存管理

    2024-06-08 20:36:02       30 阅读
  7. Vue进阶(八十八)前端测试工具介绍

    2024-06-08 20:36:02       25 阅读
  8. 一个可以自动生成随机区组试验的excel VBA小程序2

    2024-06-08 20:36:02       35 阅读
  9. 使用 Python 的 Tkinter 来创建 GUI 应用程序

    2024-06-08 20:36:02       27 阅读
  10. Debian的常用命令

    2024-06-08 20:36:02       22 阅读