Reland "Reland "Injectable logging""
This is a reland of 21219a0e43446701810236fb9fdd59be072c12df
The default implementation of OnLogMessage(msg, sev, tag) discarded
the tag, resulting in FileRotatingLogSink not receiving tags.
Since the revert the default implementation of
OnLogMessage(msg, sev, tag) has been updated to add the tag to the log
message. A more efficient implementation of it has also been added for
FileRotatingLogSink.
Unit tests are added for the default implementation and for Loggable
injection.
Original change's description:
> Reland "Injectable logging"
>
> Any injected loggable or NativeLogger would be deleted if PCFactory
> was reinitialized without calling setInjectableLogger. Now native
> logging is not implemented as a Loggable, so it will remain active
> unless a Loggable is injected.
>
> This is a reland of 59216ec4a4151b1ba5478c8f2b5c9f01f4683d7f
>
> 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}
>
> Bug: webrtc:9225
> Change-Id: I2fe3fbc8c323814284bb62e43fe1870bdab581ee
> TBR: kwiberg
> Reviewed-on: https://webrtc-review.googlesource.com/77140
> Commit-Queue: Paulina Hensman <phensman@webrtc.org>
> Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#23310}
Bug: webrtc:9225
Change-Id: I67a5728fe772f0bedc9509713ed8b8ffdc31af81
TBR: kwiberg
Reviewed-on: https://webrtc-review.googlesource.com/80860
Commit-Queue: Paulina Hensman <phensman@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23711}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index e7dfb9e..ceb5b02 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -1325,6 +1325,7 @@
rtc_android_library("base_java") {
java_files = [
"java/src/org/webrtc/ContextUtils.java",
+ "java/src/org/webrtc/Loggable.java",
"java/src/org/webrtc/Logging.java",
"java/src/org/webrtc/Size.java",
"java/src/org/webrtc/ThreadUtils.java",
diff --git a/rtc_base/java/src/org/webrtc/Loggable.java b/rtc_base/java/src/org/webrtc/Loggable.java
new file mode 100644
index 0000000..cd66aa1
--- /dev/null
+++ b/rtc_base/java/src/org/webrtc/Loggable.java
@@ -0,0 +1,22 @@
+/*
+ * 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);
+}
diff --git a/rtc_base/java/src/org/webrtc/Logging.java b/rtc_base/java/src/org/webrtc/Logging.java
index 35ef021..aafdbe8 100644
--- a/rtc_base/java/src/org/webrtc/Logging.java
+++ b/rtc_base/java/src/org/webrtc/Logging.java
@@ -15,21 +15,35 @@
import java.util.EnumSet;
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.annotation.Nullable;
+import org.webrtc.Loggable;
/**
- * 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:
+ * 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.
+ *
+ * It is also possible to switch to native logging (rtc::LogMessage) if one of the following static
+ * functions are called from the app:
* - Logging.enableLogThreads
* - Logging.enableLogTimeStamps
* - Logging.enableLogToDebugOutput
*
- * Using these APIs requires that the native library is loaded, using
- * PeerConnectionFactory.initialize.
+ * The priority goes:
+ * 1. Injected loggable
+ * 2. Native logging
+ * 3. Fallback logging.
+ * Only one method will be used at a time.
+ *
+ * Injecting a Loggable or using any of the enable... methods requires that the native library is
+ * loaded, using PeerConnectionFactory.initialize.
*/
public class Logging {
private static final Logger fallbackLogger = createFallbackLogger();
private static volatile boolean loggingEnabled;
+ @Nullable private static Loggable loggable;
+ private static Severity loggableSeverity;
private static Logger createFallbackLogger() {
final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
@@ -37,6 +51,17 @@
return fallbackLogger;
}
+ static void injectLoggable(Loggable injectedLoggable, Severity severity) {
+ if (injectedLoggable != null) {
+ loggable = injectedLoggable;
+ loggableSeverity = severity;
+ }
+ }
+
+ static void deleteInjectedLoggable() {
+ loggable = null;
+ }
+
// TODO(solenberg): Remove once dependent projects updated.
@Deprecated
public enum TraceLevel {
@@ -83,6 +108,11 @@
// TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression.
@SuppressWarnings("NoSynchronizedMethodCheck")
public static synchronized void enableLogToDebugOutput(Severity severity) {
+ if (loggable != null) {
+ throw new IllegalStateException(
+ "Logging to native debug output not supported while Loggable is injected. "
+ + "Delete the Loggable before calling this method.");
+ }
nativeEnableLogToDebugOutput(severity.ordinal());
loggingEnabled = true;
}
@@ -91,6 +121,16 @@
if (tag == null || message == null) {
throw new IllegalArgumentException("Logging tag or message may not be null.");
}
+ if (loggable != null) {
+ // Filter log messages below loggableSeverity.
+ if (severity.ordinal() < loggableSeverity.ordinal()) {
+ return;
+ }
+ loggable.onLogMessage(message, severity, tag);
+ return;
+ }
+
+ // Try native logging if no loggable is injected.
if (loggingEnabled) {
nativeLog(severity.ordinal(), tag, message);
return;