Skip to content

Comments

Add C++ fmt-style logging frontend (ulog_fmt)#4

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/add-cpp-logging-frontend
Draft

Add C++ fmt-style logging frontend (ulog_fmt)#4
Copilot wants to merge 4 commits intomainfrom
copilot/add-cpp-logging-frontend

Conversation

Copy link
Contributor

Copilot AI commented Feb 21, 2026

Adds a header-only C++ logging frontend that accepts {}-style format strings (fmt interface) with a spdlog-style class-based API, automatic source location capture without macros, and bundled fmt headers to avoid any network downloads or symbol conflicts.

New files

  • include/ulog/ulog_fmt.h — Header-only C++ frontend in namespace ulog. Provides a Logger class with trace() / debug() / info() / warn() / error() / fatal() / raw() methods, plus free functions using a default global logger. Source location (file, line, function) is captured automatically at the call site via __builtin_FILE() / __builtin_LINE() / __builtin_FUNCTION() default constructor parameters — no macros required. Fully self-contained C++ implementation; does not call any C-API functions (logger_log_with_header, logger_raw, etc.).

  • cmake/fmt.cmake — Uses the fmt headers bundled in third_party/fmt/ (header-only mode, FMT_HEADER_ONLY=1). Falls back to a system find_package(fmt) if the bundled directory is absent. No network downloads.

  • third_party/fmt/include/fmt/ — fmt 9.1.0 headers bundled directly in the repository.

  • tests/ulog_fmt_test.cc — gtest tests covering all log levels, level filtering, automatic source location capture, format specifiers, raw output, and free functions. Compiled as part of the existing ulog_unit_test target.

Modified files

  • CMakeLists.txt — Removed ULOG_BUILD_FMT option; ulog_fmt interface target is always compiled unconditionally.
  • tests/CMakeLists.txtulog_fmt_test.cc added to ulog_unit_test (gtest) target; no separate executable.

Usage

#include "ulog/ulog_fmt.h"

// Free functions using the global default logger
ulog::info("PI = {:.3f}", 3.14159);
ulog::debug("int={} str={} hex={:#x}", 42, "hello", 255);

// Dedicated logger instance
ulog::Logger my_logger;
my_logger.set_output_callback(my_cb);
my_logger.warn("x={:.2f}", value);
target_link_libraries(my_target PRIVATE ulog_fmt)
Original prompt

添加一个C++版本的日志前端,使用fmt库的接口,如果cpp库不支持,就把独立的fmt库依赖进来,并且使用独立的命名空间,不要和原始的fmt库有符号重定义


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 21, 2026 09:53
Co-authored-by: shawnfeng0 <23150404+shawnfeng0@users.noreply.github.com>
Co-authored-by: shawnfeng0 <23150404+shawnfeng0@users.noreply.github.com>
Copilot AI changed the title [WIP] Add C++ logging frontend with fmt library integration Add C++ fmt-style logging frontend (ulog_fmt) Feb 21, 2026
Copilot AI requested a review from shawnfeng0 February 21, 2026 09:55
@shawnfeng0
Copy link
Owner

c++版本的接口不要再使用宏接口,可以参考spdlog的实现

CMakeLists.txt Outdated
option(ULOG_BUILD_EXAMPLES "Build examples" OFF)
option(ULOG_BUILD_TESTS "Build tests" OFF)
option(ULOG_BUILD_TOOLS "Build tools" ON)
option(ULOG_BUILD_FMT "Build C++ fmt frontend (ulog_fmt target)" ON)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认编译即可,不需要添加cmake选项

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 2e1a699 中移除 ULOG_BUILD_FMT 选项,ulog_fmt 目标默认直接编译,无需判断。

CMakeLists.txt Outdated
include(cmake/zstd.cmake)
endif ()

if (ULOG_BUILD_FMT OR ULOG_BUILD_TESTS)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认编译即可,无需判断

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 2e1a699 中移除条件判断,include(cmake/fmt.cmake)ulog_fmt 目标现在无条件编译。

// Step 3: convenience macros mirroring the LOGGER_* / LOGGER_LOCAL_* set
// ---------------------------------------------------------------------------

#define LOGGER_FMT_LOCAL_TRACE(logger, ...) \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpp版本不要再使用宏了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 2e1a699 中完全移除所有宏,改用 Logger 类方法和 ulog::info(...) 等自由函数。

add_test(ulog_unit_test ulog_unit_test)

# C++ fmt frontend test
add_executable(ulog_fmt_test ulog_fmt_test.cc)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

跟随单元测试一起编译即可,不用单独编译fmt的测试

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 2e1a699 中将 ulog_fmt_test.cc 合并进 ulog_unit_test 目标(使用 gtest),移除了单独的 ulog_fmt_test 可执行文件和测试条目。

#else
auto msg = fmt::format(fmt_str, std::forward<Args>(args)...);
#endif
logger_log_with_header(logger, level, file, func, line, true, true, "%s",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpp版本不要使用任何原有c语言版本的接口,以后会逐步淘汰c版本接口

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 2e1a699 中完全重写 ulog_fmt.hLogger 类不再调用任何 C 语言接口(logger_log_with_headerlogger_raw 等),时间戳、PID/TID、颜色、等级标记、源码位置等所有格式化逻辑均用 C++ + fmt 独立实现。

cmake/fmt.cmake Outdated
target_link_libraries(ulog_fmt_dep INTERFACE fmt::fmt)
message(STATUS "ulog_fmt: using system {fmt} ${fmt_VERSION}")
else ()
include(FetchContent)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以提前把fmt的源码拷贝到源码目录,不要再执行下载流程

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 2e1a699 中将 fmt 9.1.0 的头文件拷贝至 third_party/fmt/include/fmt/cmake/fmt.cmake 优先使用该本地目录(header-only 模式),不再执行下载流程。

…tion, tests with unit tests

Co-authored-by: shawnfeng0 <23150404+shawnfeng0@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 21, 2026

c++版本的接口不要再使用宏接口,可以参考spdlog的实现

已重写为 spdlog 风格的 C++ 接口(commit 2e1a699):

  • Logger 类提供 trace() / debug() / info() / warn() / error() / fatal() / raw() 方法
  • 全局 ulog::get_default_logger() 和自由函数 ulog::info(...) 等,与 spdlog 的用法一致
  • 不再有任何宏,源码位置(文件/行号/函数名)通过 __builtin_FILE() / __builtin_LINE() / __builtin_FUNCTION() 作为构造函数默认参数在调用点自动捕获,无需宏即可获取完整位置信息
  • 不调用任何 C 语言接口(logger_log_with_header 等),日志格式化和输出完全在 C++ 中实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants