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

committed by
Commit Bot

parent
8694f29b30
commit
59216ec4a4
45
rtc_base/java/src/org/webrtc/FallbackLogger.java
Normal file
45
rtc_base/java/src/org/webrtc/FallbackLogger.java
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
22
rtc_base/java/src/org/webrtc/Loggable.java
Normal file
22
rtc_base/java/src/org/webrtc/Loggable.java
Normal file
@ -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);
|
||||
}
|
@ -13,28 +13,31 @@ package org.webrtc;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.EnumSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
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:
|
||||
* - Logging.enableLogThreads
|
||||
* - Logging.enableLogTimeStamps
|
||||
* - Logging.enableLogToDebugOutput
|
||||
* 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.
|
||||
*
|
||||
* Using these APIs requires that the native library is loaded, using
|
||||
* PeerConnectionFactory.initialize.
|
||||
* 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;
|
||||
private static Loggable loggable = new FallbackLogger();
|
||||
private static Severity loggableSeverity = Severity.LS_SENSITIVE;
|
||||
|
||||
private static Logger createFallbackLogger() {
|
||||
final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
|
||||
fallbackLogger.setLevel(Level.ALL);
|
||||
return fallbackLogger;
|
||||
static void injectLoggable(Loggable injectedLoggable, Severity severity) {
|
||||
if (injectedLoggable != null) {
|
||||
loggable = injectedLoggable;
|
||||
loggableSeverity = severity;
|
||||
}
|
||||
}
|
||||
|
||||
static void deleteInjectedLoggable() {
|
||||
loggable = new FallbackLogger();
|
||||
loggableSeverity = Severity.LS_SENSITIVE;
|
||||
}
|
||||
|
||||
// TODO(solenberg): Remove once dependent projects updated.
|
||||
@ -83,33 +86,16 @@ public class Logging {
|
||||
// TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression.
|
||||
@SuppressWarnings("NoSynchronizedMethodCheck")
|
||||
public static synchronized void enableLogToDebugOutput(Severity severity) {
|
||||
nativeEnableLogToDebugOutput(severity.ordinal());
|
||||
loggingEnabled = true;
|
||||
loggable = new NativeLogger(severity);
|
||||
loggableSeverity = severity;
|
||||
}
|
||||
|
||||
public static void log(Severity severity, String tag, String message) {
|
||||
if (loggingEnabled) {
|
||||
nativeLog(severity.ordinal(), tag, message);
|
||||
// Filter log messages below loggableSeverity.
|
||||
if (severity.ordinal() < loggableSeverity.ordinal()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
loggable.onLogMessage(message, severity, tag);
|
||||
}
|
||||
|
||||
public static void d(String tag, String message) {
|
||||
@ -151,8 +137,6 @@ 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);
|
||||
}
|
||||
|
28
rtc_base/java/src/org/webrtc/NativeLogger.java
Normal file
28
rtc_base/java/src/org/webrtc/NativeLogger.java
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
Reference in New Issue
Block a user