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}
This commit is contained in:

committed by
Commit Bot

parent
db38972eda
commit
f1e3cb418e
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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<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.");
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user