From 93326d47358e1545d6a0504e81c60266b9105a38 Mon Sep 17 00:00:00 2001 From: chenBright Date: Thu, 10 Jul 2025 00:07:16 +0800 Subject: [PATCH] Support log function name --- src/babylon/logging/async_log_stream.cpp | 5 +++-- src/babylon/logging/interface.h | 4 ++++ src/babylon/logging/log_stream.cpp | 5 +++-- src/babylon/logging/log_stream.h | 12 ++++++++++++ src/babylon/logging/logger.cpp | 5 +++-- src/babylon/logging/logger.h | 5 +++-- test/logging/test_interface.cpp | 16 ++++++++++++++-- test/logging/test_logger.cpp | 7 ++++--- 8 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/babylon/logging/async_log_stream.cpp b/src/babylon/logging/async_log_stream.cpp index cfe39a68..0f4d1ef1 100644 --- a/src/babylon/logging/async_log_stream.cpp +++ b/src/babylon/logging/async_log_stream.cpp @@ -27,12 +27,13 @@ void AsyncLogStream::default_header_formatter(AsyncLogStream& ls) noexcept { StringView severity_name = ls.severity(); ::babylon::localtime(&now.tv_sec, &time_struct); thread_local int tid = ::syscall(__NR_gettid); - ls.format("%.*s %d-%02d-%02d %02d:%02d:%02d.%06d %d %.*s:%d] ", + ls.format("%.*s %d-%02d-%02d %02d:%02d:%02d.%06d %d %.*s:%d %.*s] ", severity_name.size(), severity_name.data(), time_struct.tm_year + 1900, time_struct.tm_mon + 1, time_struct.tm_mday, time_struct.tm_hour, time_struct.tm_min, time_struct.tm_sec, now.tv_usec, tid, ls.file().size(), - ls.file().data(), ls.line()); + ls.file().data(), ls.line(), ls.function().size(), + ls.function().data()); } AsyncLogStream::AsyncLogStream(AsyncFileAppender* appender, diff --git a/src/babylon/logging/interface.h b/src/babylon/logging/interface.h index 806889aa..5344f7d4 100644 --- a/src/babylon/logging/interface.h +++ b/src/babylon/logging/interface.h @@ -26,6 +26,10 @@ class LogStreamProvider { // 根据日志等级,文件名和行号获得日志流 virtual LogStream& stream(int severity, StringView file, int line) noexcept = 0; + virtual LogStream& stream(int severity, StringView file, + int line, StringView /*function*/) noexcept { + return stream(severity, file, line); + } }; // 用于设置和访问日志系统的接口层 diff --git a/src/babylon/logging/log_stream.cpp b/src/babylon/logging/log_stream.cpp index 0bd62373..ebb46650 100644 --- a/src/babylon/logging/log_stream.cpp +++ b/src/babylon/logging/log_stream.cpp @@ -57,10 +57,11 @@ void DefaultLogStream::do_begin() noexcept { mutex().lock(); (*this) << severity(); thread_local int tid = ::syscall(__NR_gettid); - format(" %d-%02d-%02d %02d:%02d:%02d.%06d %d %.*s:%d] ", + format(" %d-%02d-%02d %02d:%02d:%02d.%06d %d %.*s:%d %.*s] ", time_struct.tm_year + 1900, time_struct.tm_mon + 1, time_struct.tm_mday, time_struct.tm_hour, time_struct.tm_min, - time_struct.tm_sec, us, tid, file().size(), file().data(), line()); + time_struct.tm_sec, us, tid, file().size(), file().data(), line(), + function().size(), function().data()); } void DefaultLogStream::do_end() noexcept { diff --git a/src/babylon/logging/log_stream.h b/src/babylon/logging/log_stream.h index 3bf8179a..47fec815 100644 --- a/src/babylon/logging/log_stream.h +++ b/src/babylon/logging/log_stream.h @@ -51,6 +51,9 @@ class LogStream : protected ::std::ostream { inline void set_line(int line) noexcept; inline int line() const noexcept; + inline void set_function(StringView function) noexcept; + inline StringView function() const noexcept; + template inline LogStream& begin(const Args&... args) noexcept; inline LogStream& noflush() noexcept; @@ -137,6 +140,7 @@ class LogStream : protected ::std::ostream { LogSeverity _severity {LogSeverity::DEBUG}; int _line {-1}; StringView _file; + StringView _function; }; // 便于实现LOG宏的RAII控制器 @@ -205,6 +209,14 @@ inline int LogStream::line() const noexcept { return _line; } +inline void LogStream::set_function(StringView function) noexcept { + _function = function; +} + +inline StringView LogStream::function() const noexcept { + return _function; +} + template inline LogStream& LogStream::begin(const Args&... args) noexcept { if (ABSL_PREDICT_FALSE(++_depth != 1)) { diff --git a/src/babylon/logging/logger.cpp b/src/babylon/logging/logger.cpp index 8f03ed64..092a2caf 100644 --- a/src/babylon/logging/logger.cpp +++ b/src/babylon/logging/logger.cpp @@ -41,7 +41,7 @@ Logger& Logger::operator=(const Logger& other) noexcept { } LogStream& Logger::stream(LogSeverity severity, StringView file, - int line) noexcept { + int line, StringView function) noexcept { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" #pragma GCC diagnostic ignored "-Wunknown-warning-option" @@ -52,7 +52,7 @@ LogStream& Logger::stream(LogSeverity severity, StringView file, // TODO(oathdruid): remove this after remove LogStreamProvider in interface.h if (ABSL_PREDICT_FALSE(!_initialized)) { if (severity >= LogInterface::min_severity()) { - return LogInterface::provider().stream(severity, file, line); + return LogInterface::provider().stream(severity, file, line, function); } return nls; } @@ -66,6 +66,7 @@ LogStream& Logger::stream(LogSeverity severity, StringView file, stream.set_severity(severity); stream.set_file(file); stream.set_line(line); + stream.set_function(function); return stream; } diff --git a/src/babylon/logging/logger.h b/src/babylon/logging/logger.h index e3344bde..588bbb9f 100644 --- a/src/babylon/logging/logger.h +++ b/src/babylon/logging/logger.h @@ -19,7 +19,8 @@ class Logger final { inline bool initialized() const noexcept; inline LogSeverity min_severity() const noexcept; - LogStream& stream(LogSeverity severity, StringView file, int line) noexcept; + LogStream& stream(LogSeverity severity, StringView file, + int line, StringView function) noexcept; private: using ThreadLocalLogStream = @@ -137,7 +138,7 @@ BABYLON_NAMESPACE_END : ::babylon::Voidify() & \ ::babylon::ScopedLogStream( \ (logger).stream(::babylon::LogSeverity::severity, __FILE__, \ - __LINE__), \ + __LINE__, __func__), \ ##__VA_ARGS__) \ .stream() diff --git a/test/logging/test_interface.cpp b/test/logging/test_interface.cpp index e842ab9b..33c66902 100644 --- a/test/logging/test_interface.cpp +++ b/test/logging/test_interface.cpp @@ -9,18 +9,28 @@ using ::babylon::LogStreamProvider; using ::babylon::StringView; struct MockLogStreamProvider : public LogStreamProvider { - virtual LogStream& stream(int severity, StringView file, - int line) noexcept override { + LogStream& stream(int severity, StringView file, + int line) noexcept override { this->file = file; this->line = line; this->severity = severity; return ls; } + LogStream& stream(int severity, StringView file, + int line, StringView function) noexcept override { + this->file = file; + this->line = line; + this->function = function; + this->severity = severity; + return ls; + } + ::std::stringstream ss; LogStream ls {*ss.rdbuf()}; ::std::string file; int line {-1}; + ::std::string function; int severity {-1}; }; @@ -46,6 +56,7 @@ TEST_F(LogInterfaceTest, change_log_backend) { auto before_line = __LINE__; BABYLON_LOG(INFO) << "this line should appear in provider"; auto after_line = __LINE__; + auto function = __func__; MockLogStreamProvider& provider = dynamic_cast(LogInterface::provider()); ASSERT_NE(::std::string::npos, @@ -53,5 +64,6 @@ TEST_F(LogInterfaceTest, change_log_backend) { ASSERT_EQ(file, provider.file); ASSERT_LT(before_line, provider.line); ASSERT_GT(after_line, provider.line); + ASSERT_EQ(function, provider.function); ASSERT_TRUE(LogInterface::SEVERITY_INFO == provider.severity); } diff --git a/test/logging/test_logger.cpp b/test/logging/test_logger.cpp index a0dc318d..5dbe0b67 100644 --- a/test/logging/test_logger.cpp +++ b/test/logging/test_logger.cpp @@ -85,9 +85,10 @@ TEST_F(LoggerTest, stream_has_correct_basic_info) { ASSERT_EQ(::babylon::LogSeverity::DEBUG, logger.min_severity()); for (auto i = 0; i < static_cast(::babylon::LogSeverity::NUM); ++i) { auto severity = static_cast<::babylon::LogSeverity>(i); - ASSERT_EQ(severity, logger.stream(severity, __FILE__, __LINE__).severity()); - ASSERT_EQ(__FILE__, logger.stream(severity, __FILE__, __LINE__).file()); - ASSERT_EQ(__LINE__, logger.stream(severity, __FILE__, __LINE__).line()); + ASSERT_EQ(severity, logger.stream(severity, __FILE__, __LINE__, __func__).severity()); + ASSERT_EQ(__FILE__, logger.stream(severity, __FILE__, __LINE__, __func__).file()); + ASSERT_EQ(__LINE__, logger.stream(severity, __FILE__, __LINE__, __func__).line()); + ASSERT_EQ(__func__, logger.stream(severity, __FILE__, __LINE__, __func__).function()); } }