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博客

相关推荐
Lhan.zzZ7 小时前
笔记_2026.4.28_004
c++·ide·笔记·qt
MATLAB代码顾问7 小时前
5大智能算法优化标准测试函数对比(Python实现)
开发语言·python
wuminyu8 小时前
专家视角看Java字节码加载与存储指令机制
java·linux·c语言·jvm·c++
万粉变现经纪人8 小时前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
清风明月一壶酒9 小时前
OpenClaw自动处理Word文档全流程
开发语言·c#·word
其实防守也摸鱼9 小时前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学
木喃的井盖9 小时前
无锁队列细节
c++·工程
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串基础】:输出亲朋字符串
c++·字符串·csp·高频考点·信奥赛·专项训练·输出亲朋字符串
WBluuue10 小时前
数据结构与算法:莫队(一):普通莫队与带修莫队
c++·算法
小郑加油10 小时前
python学习Day12:pandas安装与实际运用
开发语言·python·学习