Add OnLogMessage(msg, sev, tag) to logsinks
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 <kwiberg@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Commit-Queue: Paulina Hensman <phensman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23695}
diff --git a/rtc_base/logging.cc b/rtc_base/logging.cc
index 37d23ce..c87698a 100644
--- a/rtc_base/logging.cc
+++ b/rtc_base/logging.cc
@@ -81,6 +81,13 @@
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 @@
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 @@
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 f07ed8d..b5af959 100644
--- a/rtc_base/logging.h
+++ b/rtc_base/logging.h
@@ -116,6 +116,9 @@
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 @@
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 8f96095..44d5805 100644
--- a/rtc_base/logging_unittest.cc
+++ b/rtc_base/logging_unittest.cc
@@ -45,6 +45,9 @@
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 @@
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<StringStream> 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 d5febee..662b1f2 100644
--- a/rtc_base/logsinks.cc
+++ b/rtc_base/logsinks.cc
@@ -41,6 +41,18 @@
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 315ef96..caf4a5f 100644
--- a/rtc_base/logsinks.h
+++ b/rtc_base/logsinks.h
@@ -35,6 +35,9 @@
// 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();