Android Logging.java: Load native library only when needed

Logging.java currently always tries to load jingle_peerconnection_so in
the static section, but some clients don't want to use it. This CL loads
jingle_peerconnection_so only when a client requests it by calling one
of:
 * Logging.enableLogThreads
 * Logging.enableLogTimeStamps
 * Logging.enableTracing
 * Logging.enableLogToDebugOutput

BUG=b/36410678

Review-Url: https://codereview.webrtc.org/2803203002
Cr-Commit-Position: refs/heads/master@{#17647}
This commit is contained in:
magjed 2017-04-11 04:21:50 -07:00 committed by Commit bot
parent 382f2b2c45
commit dee5eb14e1
3 changed files with 40 additions and 15 deletions

View File

@ -16,23 +16,40 @@ import java.util.EnumSet;
import java.util.logging.Level;
import java.util.logging.Logger;
/** Java wrapper for WebRTC logging. */
/**
* 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.enableTracing
* - Logging.enableLogToDebugOutput
* Using native logging requires the presence of the jingle_peerconnection_so library.
*/
public class Logging {
private static final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
private static final Logger fallbackLogger = createFallbackLogger();
private static volatile boolean tracingEnabled;
private static volatile boolean loggingEnabled;
private static volatile boolean nativeLibLoaded;
private static enum NativeLibStatus { UNINITIALIZED, LOADED, FAILED }
private static volatile NativeLibStatus nativeLibStatus;
static {
try {
System.loadLibrary("jingle_peerconnection_so");
nativeLibLoaded = true;
} catch (UnsatisfiedLinkError t) {
// If native logging is unavailable, log to system log.
fallbackLogger.setLevel(Level.ALL);
private static Logger createFallbackLogger() {
final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
fallbackLogger.setLevel(Level.ALL);
return fallbackLogger;
}
fallbackLogger.log(Level.WARNING, "Failed to load jingle_peerconnection_so: ", t);
private static boolean loadNativeLibrary() {
if (nativeLibStatus == NativeLibStatus.UNINITIALIZED) {
try {
System.loadLibrary("jingle_peerconnection_so");
nativeLibStatus = NativeLibStatus.LOADED;
} catch (UnsatisfiedLinkError t) {
nativeLibStatus = NativeLibStatus.FAILED;
fallbackLogger.log(Level.WARNING, "Failed to load jingle_peerconnection_so: ", t);
}
}
return nativeLibStatus == NativeLibStatus.LOADED;
}
// Keep in sync with webrtc/common_types.h:TraceLevel.
@ -63,7 +80,7 @@ public class Logging {
public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR, LS_NONE }
public static void enableLogThreads() {
if (!nativeLibLoaded) {
if (!loadNativeLibrary()) {
fallbackLogger.log(Level.WARNING, "Cannot enable log thread because native lib not loaded.");
return;
}
@ -71,7 +88,7 @@ public class Logging {
}
public static void enableLogTimeStamps() {
if (!nativeLibLoaded) {
if (!loadNativeLibrary()) {
fallbackLogger.log(
Level.WARNING, "Cannot enable log timestamps because native lib not loaded.");
return;
@ -83,7 +100,7 @@ public class Logging {
// On Android, use "logcat:" for |path| to send output there.
// Note: this function controls the output of the WEBRTC_TRACE() macros.
public static synchronized void enableTracing(String path, EnumSet<TraceLevel> levels) {
if (!nativeLibLoaded) {
if (!loadNativeLibrary()) {
fallbackLogger.log(Level.WARNING, "Cannot enable tracing because native lib not loaded.");
return;
}
@ -103,7 +120,7 @@ public class Logging {
// output. On Android, the output will be directed to Logcat.
// Note: this function starts collecting the output of the LOG() macros.
public static synchronized void enableLogToDebugOutput(Severity severity) {
if (!nativeLibLoaded) {
if (!loadNativeLibrary()) {
fallbackLogger.log(Level.WARNING, "Cannot enable logging because native lib not loaded.");
return;
}

View File

@ -20,6 +20,10 @@ import java.io.RandomAccessFile;
import java.io.IOException;
public class FileVideoCapturer implements VideoCapturer {
static {
System.loadLibrary("jingle_peerconnection_so");
}
private interface VideoReader {
int getFrameWidth();
int getFrameHeight();

View File

@ -22,6 +22,10 @@ import java.util.ArrayList;
* Can be used to save the video frames to file.
*/
public class VideoFileRenderer implements VideoRenderer.Callbacks {
static {
System.loadLibrary("jingle_peerconnection_so");
}
private static final String TAG = "VideoFileRenderer";
private final HandlerThread renderThread;