Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RFoe/debug_macro

Repository files navigation

🌈 debug_macro

一个现代、强大的C++23日志输出库
document »

bug report · feature request

容器适配器 项目状态

GitHub stars GitHub forks License C++23 Build Status Benchmark Clang Support GCC Support MSVC Support

📋 目录

🌟 项目概述

debug_macro 是一个现代化的C++23日志输出库,专为开发者提供直观、强大的调试输出工具。受Rust dbg!() 宏启发,致力于简化日志记录和调试过程。

✨ 特性

  • 🌈 彩色日志输出
  • 🔧 跨平台支持 (Unix/Windows)
  • 📦 Header-only 库,零依赖
  • 🖥️ 支持几乎所有C++可打印类型
  • 🛠️ 编译器兼容性 (Clang/GCC/MSVC)
  • 🚀 C++23 标准支持
  • 💡 智能类型推导和格式化输出

🖼️ 性能基准测试

基准测试结果

🔧 安装

要求

  • C++23 兼容编译器
  • 无额外依赖

方法一:直接包含

#include "debug_macro.hpp"

方法二:CMake集成

# CMakeLists.txt
cmake_minimum_required(VERSION 3.11) # FetchContent added in cmake 3.11
project(app VERSION 0.1.0 LANGUAGES C CXX) # name of executable
set(CMAKE_CXX_STANDARD 23)
set (CMAKE_CXX_STANDARD_REQUIRED on)
# debug-macro
include(FetchContent)
FetchContent_Declare(debug_macro GIT_REPOSITORY https://github.com/RFoe/debug_macro)
FetchContent_MakeAvailable(debug_macro)
add_executable(${PROJECT_NAME} main.cpp) # your source files goes here
target_link_libraries(${PROJECT_NAME} PRIVATE debug_macro) # make debug_macro.hxx available

📖 使用指南

基本用法

int x = 10;
std::string name = "debug_macro";
debug_macro(x, name);

复杂类型

struct X
{
 int a = 8;
 double b = 2.32;
};
enum E : unsigned char
{
 RED = 8,
 BLUE = 122,
};
namespace {
void
test()
{
 int const lval = 8;
 int&& xval = 8;
 std::string s{ "hello world" };
 int multi_rank_array[][2]{
 { 1, 2 },
 { 3, 4 },
 };
 std::tuple tuple{ 1, 2.2, "hello" };
 std::queue<int> queue{ { 1, 2, 3 } };
 
 debug_macro(8);
 debug_macro(lval, (int&&)xval, s);
 debug_macro("hello", queue);
 debug_macro(multi_rank_array, tuple, &X::a);
 debug_macro((&X::a), (1, 2));
 debug_macro(X{}, RED, BLUE);
}
}

控制台输出

🎨 类型定制

// 正确的自定义 formatter 实现
template <>
struct std::formatter<YourCustomType> : std::formatter<std::string> {
 // 注意 const 修饰符
 auto format(const YourCustomType& obj, format_context& ctx) const {
 return std::format_to(ctx.out(), "CustomType(value={})", obj.value);
 }
};

Formatter 的 const 要求

⚠️ 重要提示:在 libstdc++ 中,std::formatterformat() 方法必须声明为 const。这是由于 __formattable_withrequires 表达式强制要求 formatter 对象在格式化过程中保持不变。

🌟 类型支持

debug_macro 支持广泛的类型输出,包括但不限于:

字符串相关类型

  • std::string
  • std::string_view
  • char
  • char[]
  • const char*

容器类型 (std::ranges::range)

  • std::vector
  • std::list
  • std::deque
  • std::array
  • std::set/std::multiset
  • std::map/std::multimap

容器适配器

  • std::queue
  • std::priority_queue
  • std::stack

Tuple-like

  • std::tuple
  • std::pair
  • aggregate struct

枚举(支持打印枚举名称)

  • std::is_enum_v
  • std::is_scoped_enum_v

Others

  • std::optional
  • std::unique_ptr | std::shared_ptr | std::weak_ptr
  • ... And more

🗺️ 路线图

Task Time Required Assigned To Current Status Progress
性能优化 > 5 days 未分配 进行中
基本完成
更多平台测试 > 5 days 未分配 进行中
主平台测试
⬜️
部分兼容性
扩展格式化选项 > 5 days 未分配 进行中
核心功能
⬜️
次要功能
性能基准测试 > 5 days 未分配 进行中
初步测试
⬜️
部分优化

🤝 贡献

我们欢迎各种形式的贡献!请参见 CONTRIBUTING.md

  1. Fork 项目
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 打开 Pull Request

📄 许可证

基于 MIT 许可证发布。详见 LICENSE

📞 联系


💡 提示: 持续更新中,欢迎 Star 和 Fork!🌟

AltStyle によって変換されたページ (->オリジナル) /