Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit dee5eb1

Browse files
Hnoo112233Commit bot
authored andcommitted
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}
1 parent 382f2b2 commit dee5eb1

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

webrtc/base/java/src/org/webrtc/Logging.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,40 @@
1616
import java.util.logging.Level;
1717
import java.util.logging.Logger;
1818

19-
/** Java wrapper for WebRTC logging. */
19+
/**
20+
* Java wrapper for WebRTC logging. Logging defaults to java.util.logging.Logger, but will switch to
21+
* native logging (rtc::LogMessage) if one of the following static functions are called from the
22+
* app:
23+
* - Logging.enableLogThreads
24+
* - Logging.enableLogTimeStamps
25+
* - Logging.enableTracing
26+
* - Logging.enableLogToDebugOutput
27+
* Using native logging requires the presence of the jingle_peerconnection_so library.
28+
*/
2029
public class Logging {
21-
private static final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
30+
private static final Logger fallbackLogger = createFallbackLogger();
2231
private static volatile boolean tracingEnabled;
2332
private static volatile boolean loggingEnabled;
24-
private static volatile boolean nativeLibLoaded;
33+
private static enum NativeLibStatus { UNINITIALIZED, LOADED, FAILED }
34+
private static volatile NativeLibStatus nativeLibStatus;
2535

26-
static {
27-
try {
28-
System.loadLibrary("jingle_peerconnection_so");
29-
nativeLibLoaded = true;
30-
} catch (UnsatisfiedLinkError t) {
31-
// If native logging is unavailable, log to system log.
32-
fallbackLogger.setLevel(Level.ALL);
36+
private static Logger createFallbackLogger() {
37+
final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
38+
fallbackLogger.setLevel(Level.ALL);
39+
return fallbackLogger;
40+
}
3341

34-
fallbackLogger.log(Level.WARNING, "Failed to load jingle_peerconnection_so: ", t);
42+
private static boolean loadNativeLibrary() {
43+
if (nativeLibStatus == NativeLibStatus.UNINITIALIZED) {
44+
try {
45+
System.loadLibrary("jingle_peerconnection_so");
46+
nativeLibStatus = NativeLibStatus.LOADED;
47+
} catch (UnsatisfiedLinkError t) {
48+
nativeLibStatus = NativeLibStatus.FAILED;
49+
fallbackLogger.log(Level.WARNING, "Failed to load jingle_peerconnection_so: ", t);
50+
}
3551
}
52+
return nativeLibStatus == NativeLibStatus.LOADED;
3653
}
3754

3855
// Keep in sync with webrtc/common_types.h:TraceLevel.
@@ -63,15 +80,15 @@ public enum TraceLevel {
6380
public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR, LS_NONE }
6481

6582
public static void enableLogThreads() {
66-
if (!nativeLibLoaded) {
83+
if (!loadNativeLibrary()) {
6784
fallbackLogger.log(Level.WARNING, "Cannot enable log thread because native lib not loaded.");
6885
return;
6986
}
7087
nativeEnableLogThreads();
7188
}
7289

7390
public static void enableLogTimeStamps() {
74-
if (!nativeLibLoaded) {
91+
if (!loadNativeLibrary()) {
7592
fallbackLogger.log(
7693
Level.WARNING, "Cannot enable log timestamps because native lib not loaded.");
7794
return;
@@ -83,7 +100,7 @@ public static void enableLogTimeStamps() {
83100
// On Android, use "logcat:" for |path| to send output there.
84101
// Note: this function controls the output of the WEBRTC_TRACE() macros.
85102
public static synchronized void enableTracing(String path, EnumSet<TraceLevel> levels) {
86-
if (!nativeLibLoaded) {
103+
if (!loadNativeLibrary()) {
87104
fallbackLogger.log(Level.WARNING, "Cannot enable tracing because native lib not loaded.");
88105
return;
89106
}
@@ -103,7 +120,7 @@ public static synchronized void enableTracing(String path, EnumSet<TraceLevel> l
103120
// output. On Android, the output will be directed to Logcat.
104121
// Note: this function starts collecting the output of the LOG() macros.
105122
public static synchronized void enableLogToDebugOutput(Severity severity) {
106-
if (!nativeLibLoaded) {
123+
if (!loadNativeLibrary()) {
107124
fallbackLogger.log(Level.WARNING, "Cannot enable logging because native lib not loaded.");
108125
return;
109126
}

webrtc/sdk/android/api/org/webrtc/FileVideoCapturer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import java.io.IOException;
2121

2222
public class FileVideoCapturer implements VideoCapturer {
23+
static {
24+
System.loadLibrary("jingle_peerconnection_so");
25+
}
26+
2327
private interface VideoReader {
2428
int getFrameWidth();
2529
int getFrameHeight();

webrtc/sdk/android/api/org/webrtc/VideoFileRenderer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* Can be used to save the video frames to file.
2323
*/
2424
public class VideoFileRenderer implements VideoRenderer.Callbacks {
25+
static {
26+
System.loadLibrary("jingle_peerconnection_so");
27+
}
28+
2529
private static final String TAG = "VideoFileRenderer";
2630

2731
private final HandlerThread renderThread;

0 commit comments

Comments
 (0)