16
16
import java .util .logging .Level ;
17
17
import java .util .logging .Logger ;
18
18
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
+ */
20
29
public class Logging {
21
- private static final Logger fallbackLogger = Logger . getLogger ( "org.webrtc.Logging" );
30
+ private static final Logger fallbackLogger = createFallbackLogger ( );
22
31
private static volatile boolean tracingEnabled ;
23
32
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 ;
25
35
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
+ }
33
41
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
+ }
35
51
}
52
+ return nativeLibStatus == NativeLibStatus .LOADED ;
36
53
}
37
54
38
55
// Keep in sync with webrtc/common_types.h:TraceLevel.
@@ -63,15 +80,15 @@ public enum TraceLevel {
63
80
public enum Severity { LS_SENSITIVE , LS_VERBOSE , LS_INFO , LS_WARNING , LS_ERROR , LS_NONE }
64
81
65
82
public static void enableLogThreads () {
66
- if (!nativeLibLoaded ) {
83
+ if (!loadNativeLibrary () ) {
67
84
fallbackLogger .log (Level .WARNING , "Cannot enable log thread because native lib not loaded." );
68
85
return ;
69
86
}
70
87
nativeEnableLogThreads ();
71
88
}
72
89
73
90
public static void enableLogTimeStamps () {
74
- if (!nativeLibLoaded ) {
91
+ if (!loadNativeLibrary () ) {
75
92
fallbackLogger .log (
76
93
Level .WARNING , "Cannot enable log timestamps because native lib not loaded." );
77
94
return ;
@@ -83,7 +100,7 @@ public static void enableLogTimeStamps() {
83
100
// On Android, use "logcat:" for |path| to send output there.
84
101
// Note: this function controls the output of the WEBRTC_TRACE() macros.
85
102
public static synchronized void enableTracing (String path , EnumSet <TraceLevel > levels ) {
86
- if (!nativeLibLoaded ) {
103
+ if (!loadNativeLibrary () ) {
87
104
fallbackLogger .log (Level .WARNING , "Cannot enable tracing because native lib not loaded." );
88
105
return ;
89
106
}
@@ -103,7 +120,7 @@ public static synchronized void enableTracing(String path, EnumSet<TraceLevel> l
103
120
// output. On Android, the output will be directed to Logcat.
104
121
// Note: this function starts collecting the output of the LOG() macros.
105
122
public static synchronized void enableLogToDebugOutput (Severity severity ) {
106
- if (!nativeLibLoaded ) {
123
+ if (!loadNativeLibrary () ) {
107
124
fallbackLogger .log (Level .WARNING , "Cannot enable logging because native lib not loaded." );
108
125
return ;
109
126
}
0 commit comments