Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/babylon/logging/async_log_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/babylon/logging/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

// 用于设置和访问日志系统的接口层
Expand Down
5 changes: 3 additions & 2 deletions src/babylon/logging/log_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/babylon/logging/log_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename... Args>
inline LogStream& begin(const Args&... args) noexcept;
inline LogStream& noflush() noexcept;
Expand Down Expand Up @@ -137,6 +140,7 @@ class LogStream : protected ::std::ostream {
LogSeverity _severity {LogSeverity::DEBUG};
int _line {-1};
StringView _file;
StringView _function;
};

// 便于实现LOG宏的RAII控制器
Expand Down Expand Up @@ -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 <typename... Args>
inline LogStream& LogStream::begin(const Args&... args) noexcept {
if (ABSL_PREDICT_FALSE(++_depth != 1)) {
Expand Down
5 changes: 3 additions & 2 deletions src/babylon/logging/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/babylon/logging/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -137,7 +138,7 @@ BABYLON_NAMESPACE_END
: ::babylon::Voidify() & \
::babylon::ScopedLogStream( \
(logger).stream(::babylon::LogSeverity::severity, __FILE__, \
__LINE__), \
__LINE__, __func__), \
##__VA_ARGS__) \
.stream()

Expand Down
16 changes: 14 additions & 2 deletions test/logging/test_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};

Expand All @@ -46,12 +56,14 @@ 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<MockLogStreamProvider&>(LogInterface::provider());
ASSERT_NE(::std::string::npos,
provider.ss.str().find("this line should appear in provider"));
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);
}
7 changes: 4 additions & 3 deletions test/logging/test_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(::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());
}
}

Expand Down
Loading