C/C++预定义宏与调试日志输出模板

前言: 在个人的上一篇博客C/C++常见的预定义宏与基本使用方法-CSDN博客中对C/C++预定义宏的定义、常见的预定义宏及其用法做了初步的介绍,并可以了解到预定义宏可以用于C/C++程序的调试日志输出,本文基于预定义宏与调试日志输出需要出发定义调试日志输出宏模板,以用于程序开发者对程序错误的定位与运行日志的记录等。

调试与日志宏模板定义:

1、调试输出宏模板

cpp 复制代码
#include <iostream>

#ifdef _DEBUG
#define LOG_DEBUG(msg) \
std::cout << "[DEBUG] " << __FILE__ << ":" << __LINE__ << " (" << __FUNCTION__ << ") - " << msg << std::endl;
#else
#define LOG_DEBUG(msg)
#endif

调试输出样例

cpp 复制代码
LOG_DEBUG("This is debug output!")

[DEBUG] main.cpp:13 (main) This is debug output!

2、带有详细时间戳的调试输出宏模板

cpp 复制代码
#include <iostream>
#include <chrono>
#include <iomanip>

#ifdef _DEBUG
#define LOG_DEBUG(msg) { \
    std::time_t now = std::time(nullptr);
    std::tm* local_time = std::localtime(&now);
    std::cout << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "] " \
              << "[DEBUG] " << __FILE__ << ":" << __LINE__ \
              << " (" << __FUNCTION__ << ") - " \
              << msg << std::endl; \
}
#else
#define LOG_DEBUG(msg)
#endif

调试输出样例:

cpp 复制代码
LOG_DEBUG("This is debug output!")

[2025-09-29 10:32:16][DEBUG] main.cpp:13 (main) - This is debug output!

3、文件日志输出模板

cpp 复制代码
#include <iostream>
#include <fstream>

// 模板主动缺省输出路径log_output_path,直接使用报错,以提醒使用时指定特定的日志输出路径
#define LOG_TO_FILE(msg) { \
    std::string log_output_path = ; \
    std::ofstream logFile(log_output_path, std::ios::app); \
    logFile << "[INFO] " << __FILE__ << ":" << __LINE__ << " (" << __FUNCTION__ << ") - " << msg << std::endl; \
}

文件日志输出样例:

cpp 复制代码
#include <iostream>
#include <fstream>

// 模板主动缺省输出路径log_output_path,直接使用报错,以提醒使用时指定特定的日志输出路径
#define LOG_TO_FILE(msg) { \
    // 此处使用是指定输出路径为"/home/user/temp/log.txt"
    std::string log_output_path = "/home/user/temp/log.txt"; \
    std::ofstream logFile(log_output_path, std::ios::app); \
    logFile << "[INFO] " << __FILE__ << ":" << __LINE__ << " (" << __FUNCTION__ << ") - " << msg << std::endl; \
}

LOG_TO_FILE("This is a log info!")

[INFO] main.cpp:14 (main) - This is a log info!

4、带有时间戳的文件日志输出模板

cpp 复制代码
#include <iostream>
#include <fstream>

// 模板主动缺省输出路径log_output_path,直接使用报错,以提醒使用时指定特定的日志输出路径
#define LOG_TO_FILE(msg) { \
    std::string log_output_path = ; \
    std::ofstream logFile(log_output_path, std::ios::app); \
    std::time_t now = std::time(nullptr);
    std::tm* local_time = std::localtime(&now);
    logFile << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "] " \
            << "[INFO] " << __FILE__ << ":" << __LINE__ << " (" << __FUNCTION__ << ") - " \
            << msg << std::endl; \
}

文件日志输出样例:

cpp 复制代码
#include <iostream>
#include <fstream>

// 模板主动缺省输出路径log_output_path,直接使用报错,以提醒使用时指定特定的日志输出路径
#define LOG_TO_FILE(msg) { \
    // 此处使用是指定输出路径为"/home/user/temp/log.txt"
    std::string log_output_path = "/home/user/temp/log.txt"; \
    std::ofstream logFile(log_output_path, std::ios::app); \
    std::time_t now = std::time(nullptr);
    std::tm* local_time = std::localtime(&now);
    logFile << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "] " \
            << "[INFO] " << __FILE__ << ":" << __LINE__ << " (" << __FUNCTION__ << ") - " \
            << msg << std::endl; \
}

LOG_TO_FILE("This is a log info!")

[2025-09-29 10:41:45][INFO] main.cpp:14 (main) - This is a log info!

参考博客:在 C++ 中实现调试日志输出_c++输出调试信息-CSDN博客

相关推荐
yuyanjingtao3 分钟前
动态规划 背包 之 凑钱
c++·算法·青少年编程·动态规划·gesp·csp-j/s
编程武士30 分钟前
Python 各版本主要变化速览
开发语言·python
hqwest31 分钟前
码上通QT实战29--系统设置04-用户操作管理
开发语言·qt·模态窗体·addbindvalue·bindvalue
专注于大数据技术栈1 小时前
java学习--LinkedHashSet
java·开发语言·学习
这个图像胖嘟嘟1 小时前
前端开发的基本运行环境配置
开发语言·javascript·vue.js·react.js·typescript·npm·node.js
scx201310041 小时前
20260112树状数组总结
数据结构·c++·算法·树状数组
星竹晨L1 小时前
【C++内存安全管理】智能指针的使用和原理
开发语言·c++
宵时待雨2 小时前
数据结构(初阶)笔记归纳3:顺序表的应用
c语言·开发语言·数据结构·笔记·算法
智者知已应修善业2 小时前
【C语言 dfs算法 十四届蓝桥杯 D飞机降落问题】2024-4-12
c语言·c++·经验分享·笔记·算法·蓝桥杯·深度优先
旺仔小拳头..2 小时前
Java ---变量、常量、类型转换、默认值、重载、标识符、输入输出、访问修饰符、泛型、迭代器
java·开发语言·python