From f1e3cb418e5911a2c9e5b03100ff305204e2a631 Mon Sep 17 00:00:00 2001 From: Paulina Hensman Date: Wed, 20 Jun 2018 14:07:05 +0200 Subject: [PATCH] Add OnLogMessage(msg, sev, tag) to logsinks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes a bug where tags are not saved by FileRotatingLogSink. It is also a preparation step for injectable logging. Bug: webrtc:9225 Change-Id: I06ae0810073492bd2f103fefd64bd3cd02659fbc Reviewed-on: https://webrtc-review.googlesource.com/84361 Reviewed-by: Karl Wiberg Reviewed-by: Sami Kalliomäki Commit-Queue: Paulina Hensman Cr-Commit-Position: refs/heads/master@{#23695} --- rtc_base/logging.cc | 19 ++++++++++++++++++- rtc_base/logging.h | 5 ++++- rtc_base/logging_unittest.cc | 22 ++++++++++++++++++++++ rtc_base/logsinks.cc | 12 ++++++++++++ rtc_base/logsinks.h | 3 +++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc index 37d23ce492..c87698add7 100644 --- a/rtc_base/logging.cc +++ b/rtc_base/logging.cc @@ -81,6 +81,13 @@ std::ostream& GetNoopStream() { CriticalSection g_log_crit; } // namespace +// Inefficient default implementation, override is recommended. +void LogSink::OnLogMessage(const std::string& msg, + LoggingSeverity severity, + const char* tag) { + OnLogMessage(tag + (": " + msg)); +} + ///////////////////////////////////////////////////////////////////////////// // LogMessage ///////////////////////////////////////////////////////////////////////////// @@ -126,8 +133,14 @@ LogMessage::LogMessage(const char* file, print_stream_ << "[" << std::dec << id << "] "; } - if (file != nullptr) + if (file != nullptr) { +#if defined(WEBRTC_ANDROID) + tag_ = FilenameFromPath(file); + print_stream_ << "(line " << line << "): "; +#else print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): "; +#endif + } if (err_ctx != ERRCTX_NONE) { char tmp_buf[1024]; @@ -216,7 +229,11 @@ LogMessage::~LogMessage() { CritScope cs(&g_log_crit); for (auto& kv : streams_) { if (severity_ >= kv.second) { +#if defined(WEBRTC_ANDROID) + kv.first->OnLogMessage(str, severity_, tag_); +#else kv.first->OnLogMessage(str); +#endif } } } diff --git a/rtc_base/logging.h b/rtc_base/logging.h index f07ed8dbfe..b5af95928b 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h @@ -116,6 +116,9 @@ class LogSink { public: LogSink() {} virtual ~LogSink() {} + virtual void OnLogMessage(const std::string& msg, + LoggingSeverity severity, + const char* tag); virtual void OnLogMessage(const std::string& message) = 0; }; @@ -495,7 +498,7 @@ class LogMessage { LoggingSeverity severity_; #if defined(WEBRTC_ANDROID) - // The Android debug output tag. + // The default Android debug output tag. const char* tag_ = "libjingle"; #endif diff --git a/rtc_base/logging_unittest.cc b/rtc_base/logging_unittest.cc index 8f96095a79..44d5805b2f 100644 --- a/rtc_base/logging_unittest.cc +++ b/rtc_base/logging_unittest.cc @@ -45,6 +45,9 @@ class LogMessageForTesting : public LogMessage { const std::string& get_extra() const { return extra_; } bool is_noop() const { return is_noop_; } +#if defined(WEBRTC_ANDROID) + const char* get_tag() const { return tag_; } +#endif // Returns the contents of the internal log stream. // Note that parts of the stream won't (as is) be available until *after* the @@ -215,9 +218,28 @@ TEST(LogTest, CheckFilePathParsed) { log_msg.stream() << "<- Does this look right?"; const std::string stream = log_msg.GetPrintStream(); +#if defined(WEBRTC_ANDROID) + const char* tag = log_msg.get_tag(); + EXPECT_NE(nullptr, strstr(tag, "myfile.cc")); + EXPECT_NE(std::string::npos, stream.find("100")); +#else EXPECT_NE(std::string::npos, stream.find("(myfile.cc:100)")); +#endif } +#if defined(WEBRTC_ANDROID) +TEST(LogTest, CheckTagAddedToStringInDefaultOnLogMessageAndroid) { + std::string str; + LogSinkImpl stream(&str); + LogMessage::AddLogToStream(&stream, LS_INFO); + EXPECT_EQ(LS_INFO, LogMessage::GetLogToStream(&stream)); + + RTC_LOG_TAG(LS_INFO, "my_tag") << "INFO"; + EXPECT_NE(std::string::npos, str.find("INFO")); + EXPECT_NE(std::string::npos, str.find("my_tag")); +} +#endif + TEST(LogTest, CheckNoopLogEntry) { if (LogMessage::GetLogToDebug() <= LS_SENSITIVE) { printf("CheckNoopLogEntry: skipping. Global severity is being overridden."); diff --git a/rtc_base/logsinks.cc b/rtc_base/logsinks.cc index d5febee948..662b1f20de 100644 --- a/rtc_base/logsinks.cc +++ b/rtc_base/logsinks.cc @@ -41,6 +41,18 @@ void FileRotatingLogSink::OnLogMessage(const std::string& message) { stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr); } +void FileRotatingLogSink::OnLogMessage(const std::string& message, + LoggingSeverity sev, + const char* tag) { + if (stream_->GetState() != SS_OPEN) { + std::fprintf(stderr, "Init() must be called before adding this sink.\n"); + return; + } + stream_->WriteAll(tag, strlen(tag), nullptr, nullptr); + stream_->WriteAll(": ", 2, nullptr, nullptr); + stream_->WriteAll(message.c_str(), message.size(), nullptr, nullptr); +} + bool FileRotatingLogSink::Init() { return stream_->Open(); } diff --git a/rtc_base/logsinks.h b/rtc_base/logsinks.h index 315ef9698b..caf4a5f728 100644 --- a/rtc_base/logsinks.h +++ b/rtc_base/logsinks.h @@ -35,6 +35,9 @@ class FileRotatingLogSink : public LogSink { // Writes the message to the current file. It will spill over to the next // file if needed. void OnLogMessage(const std::string& message) override; + void OnLogMessage(const std::string& message, + LoggingSeverity sev, + const char* tag) override; // Deletes any existing files in the directory and creates a new log file. virtual bool Init();