Revert "Injectable logging"

This reverts commit 59216ec4a4151b1ba5478c8f2b5c9f01f4683d7f.

Reason for revert:  forces all logs to have identical tag

Original change's description:
> Injectable logging
> 
> Allows passing a Loggable to PCFactory.initializationOptions, which
> is then injected to Logging.java and logging.h. Future log messages
> in both Java and native will then be passed to this Loggable.
> 
> Bug: webrtc:9225
> Change-Id: I2ff693380639448301a78a93dc11d3a0106f0967
> Reviewed-on: https://webrtc-review.googlesource.com/73243
> Commit-Queue: Paulina Hensman <phensman@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#23241}

TBR=magjed@webrtc.org,sakal@webrtc.org,kwiberg@webrtc.org,phensman@webrtc.org

Change-Id: I27c9587238325b69b26166434740869021b7db8a
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: webrtc:9225
Reviewed-on: https://webrtc-review.googlesource.com/76885
Reviewed-by: Alex Glaznev <glaznev@webrtc.org>
Commit-Queue: Alex Glaznev <glaznev@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23253}
This commit is contained in:
Alex Glaznev
2018-05-15 22:30:06 +00:00
committed by Commit Bot
parent d7624f2d90
commit 4f81038a52
16 changed files with 70 additions and 390 deletions

View File

@ -1342,10 +1342,7 @@ if (is_android) {
rtc_android_library("base_java") {
java_files = [
"java/src/org/webrtc/ContextUtils.java",
"java/src/org/webrtc/FallbackLogger.java",
"java/src/org/webrtc/Loggable.java",
"java/src/org/webrtc/Logging.java",
"java/src/org/webrtc/NativeLogger.java",
"java/src/org/webrtc/Size.java",
"java/src/org/webrtc/ThreadUtils.java",
]

View File

@ -1,45 +0,0 @@
/*
* Copyright 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
package org.webrtc;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.webrtc.Loggable;
import org.webrtc.Logging.Severity;
class FallbackLogger implements Loggable {
final Logger fallbackLogger;
public FallbackLogger() {
fallbackLogger = Logger.getLogger("org.webrtc.FallbackLogger");
fallbackLogger.setLevel(Level.ALL);
}
@Override
public void onLogMessage(String message, Severity severity, String tag) {
Level level;
switch (severity) {
case LS_ERROR:
level = Level.SEVERE;
break;
case LS_WARNING:
level = Level.WARNING;
break;
case LS_INFO:
level = Level.INFO;
break;
default:
level = Level.FINE;
break;
}
fallbackLogger.log(level, tag + ": " + message);
}
}

View File

@ -1,22 +0,0 @@
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
package org.webrtc;
import org.webrtc.Logging.Severity;
/**
* Java interface for WebRTC logging. The default implementation uses webrtc.Logging.
*
* When injected, the Loggable will receive logging from both Java and native.
*/
public interface Loggable {
public void onLogMessage(String message, Severity severity, String tag);
}

View File

@ -13,31 +13,28 @@ package org.webrtc;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.EnumSet;
import org.webrtc.Loggable;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Java wrapper for WebRTC logging. Logging defaults to java.util.logging.Logger, but a custom
* logger implementing the Loggable interface can be injected along with a Severity. All subsequent
* log messages will then be redirected to the injected Loggable, except those with a severity lower
* than the specified severity, which will be discarded.
* Java wrapper for WebRTC logging. Logging defaults to java.util.logging.Logger, but will switch to
* native logging (rtc::LogMessage) if one of the following static functions are called from the
* app:
* - Logging.enableLogThreads
* - Logging.enableLogTimeStamps
* - Logging.enableLogToDebugOutput
*
* Injecting a Loggable or using any of the enable... methods requires that the native library is
* loaded, using PeerConnectionFactory.initialize.
* Using these APIs requires that the native library is loaded, using
* PeerConnectionFactory.initialize.
*/
public class Logging {
private static Loggable loggable = new FallbackLogger();
private static Severity loggableSeverity = Severity.LS_SENSITIVE;
private static final Logger fallbackLogger = createFallbackLogger();
private static volatile boolean loggingEnabled;
static void injectLoggable(Loggable injectedLoggable, Severity severity) {
if (injectedLoggable != null) {
loggable = injectedLoggable;
loggableSeverity = severity;
}
}
static void deleteInjectedLoggable() {
loggable = new FallbackLogger();
loggableSeverity = Severity.LS_SENSITIVE;
private static Logger createFallbackLogger() {
final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
fallbackLogger.setLevel(Level.ALL);
return fallbackLogger;
}
// TODO(solenberg): Remove once dependent projects updated.
@ -86,16 +83,33 @@ public class Logging {
// TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression.
@SuppressWarnings("NoSynchronizedMethodCheck")
public static synchronized void enableLogToDebugOutput(Severity severity) {
loggable = new NativeLogger(severity);
loggableSeverity = severity;
nativeEnableLogToDebugOutput(severity.ordinal());
loggingEnabled = true;
}
public static void log(Severity severity, String tag, String message) {
// Filter log messages below loggableSeverity.
if (severity.ordinal() < loggableSeverity.ordinal()) {
if (loggingEnabled) {
nativeLog(severity.ordinal(), tag, message);
return;
}
loggable.onLogMessage(message, severity, tag);
// Fallback to system log.
Level level;
switch (severity) {
case LS_ERROR:
level = Level.SEVERE;
break;
case LS_WARNING:
level = Level.WARNING;
break;
case LS_INFO:
level = Level.INFO;
break;
default:
level = Level.FINE;
break;
}
fallbackLogger.log(level, tag + ": " + message);
}
public static void d(String tag, String message) {
@ -137,6 +151,8 @@ public class Logging {
return sw.toString();
}
private static native void nativeEnableLogToDebugOutput(int nativeSeverity);
private static native void nativeEnableLogThreads();
private static native void nativeEnableLogTimeStamps();
private static native void nativeLog(int severity, String tag, String message);
}

View File

@ -1,28 +0,0 @@
/*
* Copyright 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
package org.webrtc;
import org.webrtc.Loggable;
import org.webrtc.Logging.Severity;
class NativeLogger implements Loggable {
public NativeLogger(Severity severity) {
nativeEnableLogToDebugOutput(severity.ordinal());
}
@Override
public void onLogMessage(String message, Severity severity, String tag) {
nativeLog(severity.ordinal(), tag, message);
}
private static native void nativeEnableLogToDebugOutput(int nativeSeverity);
private static native void nativeLog(int severity, String tag, String message);
}

View File

@ -80,11 +80,6 @@ std::ostream& GetNoopStream() {
CriticalSection g_log_crit;
} // namespace
void LogSink::OnLogMessage(const std::string& msg,
LoggingSeverity severity,
const char* tag) {
OnLogMessage(msg);
}
/////////////////////////////////////////////////////////////////////////////
// LogMessage
@ -131,14 +126,8 @@ LogMessage::LogMessage(const char* file,
print_stream_ << "[" << std::dec << id << "] ";
}
if (file != nullptr) {
#if defined(WEBRTC_ANDROID)
tag_ = FilenameFromPath(file);
print_stream_ << "(line " << line << "): ";
#else
if (file != nullptr)
print_stream_ << "(" << FilenameFromPath(file) << ":" << line << "): ";
#endif
}
if (err_ctx != ERRCTX_NONE) {
char tmp_buf[1024];
@ -191,6 +180,7 @@ LogMessage::LogMessage(const char* file,
0 /* err */) {
if (!is_noop_) {
tag_ = tag;
print_stream_ << tag << ": ";
}
}
#endif
@ -228,11 +218,7 @@ 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
}
}
}

View File

@ -117,9 +117,6 @@ 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;
};
@ -246,7 +243,7 @@ class LogMessage {
LoggingSeverity severity_;
#if defined(WEBRTC_ANDROID)
// The default Android debug output tag.
// The Android debug output tag.
const char* tag_ = "libjingle";
#endif

View File

@ -47,9 +47,6 @@ 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
@ -189,13 +186,7 @@ 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
}
TEST(LogTest, CheckNoopLogEntry) {