OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 package org.webrtc; | 11 package org.webrtc; |
12 | 12 |
13 import java.io.PrintWriter; | 13 import java.io.PrintWriter; |
14 import java.io.StringWriter; | 14 import java.io.StringWriter; |
15 import java.util.EnumSet; | 15 import java.util.EnumSet; |
16 import java.util.logging.Level; | 16 import java.util.logging.Level; |
17 import java.util.logging.Logger; | 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 li
brary. |
| 28 */ |
20 public class Logging { | 29 public class Logging { |
21 private static final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logg
ing"); | 30 private static final Logger fallbackLogger = createFallbackLogger(); |
22 private static volatile boolean tracingEnabled; | 31 private static volatile boolean tracingEnabled; |
23 private static volatile boolean loggingEnabled; | 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 = NativeLibStatus.UNIN
ITIALIZED; |
25 | 35 |
26 static { | 36 private static Logger createFallbackLogger() { |
27 try { | 37 final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging"); |
28 System.loadLibrary("jingle_peerconnection_so"); | 38 fallbackLogger.setLevel(Level.ALL); |
29 nativeLibLoaded = true; | 39 return fallbackLogger; |
30 } catch (UnsatisfiedLinkError t) { | 40 } |
31 // If native logging is unavailable, log to system log. | |
32 fallbackLogger.setLevel(Level.ALL); | |
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 // Keep in sync with webrtc/common_types.h:TraceLevel. | 55 // Keep in sync with webrtc/common_types.h:TraceLevel. |
39 public enum TraceLevel { | 56 public enum TraceLevel { |
40 TRACE_NONE(0x0000), | 57 TRACE_NONE(0x0000), |
41 TRACE_STATEINFO(0x0001), | 58 TRACE_STATEINFO(0x0001), |
42 TRACE_WARNING(0x0002), | 59 TRACE_WARNING(0x0002), |
43 TRACE_ERROR(0x0004), | 60 TRACE_ERROR(0x0004), |
44 TRACE_CRITICAL(0x0008), | 61 TRACE_CRITICAL(0x0008), |
45 TRACE_APICALL(0x0010), | 62 TRACE_APICALL(0x0010), |
(...skipping 10 matching lines...) Expand all Loading... |
56 public final int level; | 73 public final int level; |
57 TraceLevel(int level) { | 74 TraceLevel(int level) { |
58 this.level = level; | 75 this.level = level; |
59 } | 76 } |
60 } | 77 } |
61 | 78 |
62 // Keep in sync with webrtc/base/logging.h:LoggingSeverity. | 79 // Keep in sync with webrtc/base/logging.h:LoggingSeverity. |
63 public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR
, LS_NONE } | 80 public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR
, LS_NONE } |
64 | 81 |
65 public static void enableLogThreads() { | 82 public static void enableLogThreads() { |
66 if (!nativeLibLoaded) { | 83 if (!loadNativeLibrary()) { |
67 fallbackLogger.log(Level.WARNING, "Cannot enable log thread because native
lib not loaded."); | 84 fallbackLogger.log(Level.WARNING, "Cannot enable log thread because native
lib not loaded."); |
68 return; | 85 return; |
69 } | 86 } |
70 nativeEnableLogThreads(); | 87 nativeEnableLogThreads(); |
71 } | 88 } |
72 | 89 |
73 public static void enableLogTimeStamps() { | 90 public static void enableLogTimeStamps() { |
74 if (!nativeLibLoaded) { | 91 if (!loadNativeLibrary()) { |
75 fallbackLogger.log( | 92 fallbackLogger.log( |
76 Level.WARNING, "Cannot enable log timestamps because native lib not lo
aded."); | 93 Level.WARNING, "Cannot enable log timestamps because native lib not lo
aded."); |
77 return; | 94 return; |
78 } | 95 } |
79 nativeEnableLogTimeStamps(); | 96 nativeEnableLogTimeStamps(); |
80 } | 97 } |
81 | 98 |
82 // Enable tracing to |path| of messages of |levels|. | 99 // Enable tracing to |path| of messages of |levels|. |
83 // On Android, use "logcat:" for |path| to send output there. | 100 // On Android, use "logcat:" for |path| to send output there. |
84 // Note: this function controls the output of the WEBRTC_TRACE() macros. | 101 // Note: this function controls the output of the WEBRTC_TRACE() macros. |
85 public static synchronized void enableTracing(String path, EnumSet<TraceLevel>
levels) { | 102 public static synchronized void enableTracing(String path, EnumSet<TraceLevel>
levels) { |
86 if (!nativeLibLoaded) { | 103 if (!loadNativeLibrary()) { |
87 fallbackLogger.log(Level.WARNING, "Cannot enable tracing because native li
b not loaded."); | 104 fallbackLogger.log(Level.WARNING, "Cannot enable tracing because native li
b not loaded."); |
88 return; | 105 return; |
89 } | 106 } |
90 | 107 |
91 if (tracingEnabled) { | 108 if (tracingEnabled) { |
92 return; | 109 return; |
93 } | 110 } |
94 int nativeLevel = 0; | 111 int nativeLevel = 0; |
95 for (TraceLevel level : levels) { | 112 for (TraceLevel level : levels) { |
96 nativeLevel |= level.level; | 113 nativeLevel |= level.level; |
97 } | 114 } |
98 nativeEnableTracing(path, nativeLevel); | 115 nativeEnableTracing(path, nativeLevel); |
99 tracingEnabled = true; | 116 tracingEnabled = true; |
100 } | 117 } |
101 | 118 |
102 // Enable diagnostic logging for messages of |severity| to the platform debug | 119 // Enable diagnostic logging for messages of |severity| to the platform debug |
103 // output. On Android, the output will be directed to Logcat. | 120 // output. On Android, the output will be directed to Logcat. |
104 // Note: this function starts collecting the output of the LOG() macros. | 121 // Note: this function starts collecting the output of the LOG() macros. |
105 public static synchronized void enableLogToDebugOutput(Severity severity) { | 122 public static synchronized void enableLogToDebugOutput(Severity severity) { |
106 if (!nativeLibLoaded) { | 123 if (!loadNativeLibrary()) { |
107 fallbackLogger.log(Level.WARNING, "Cannot enable logging because native li
b not loaded."); | 124 fallbackLogger.log(Level.WARNING, "Cannot enable logging because native li
b not loaded."); |
108 return; | 125 return; |
109 } | 126 } |
110 nativeEnableLogToDebugOutput(severity.ordinal()); | 127 nativeEnableLogToDebugOutput(severity.ordinal()); |
111 loggingEnabled = true; | 128 loggingEnabled = true; |
112 } | 129 } |
113 | 130 |
114 public static void log(Severity severity, String tag, String message) { | 131 public static void log(Severity severity, String tag, String message) { |
115 if (loggingEnabled) { | 132 if (loggingEnabled) { |
116 nativeLog(severity.ordinal(), tag, message); | 133 nativeLog(severity.ordinal(), tag, message); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 e.printStackTrace(pw); | 191 e.printStackTrace(pw); |
175 return sw.toString(); | 192 return sw.toString(); |
176 } | 193 } |
177 | 194 |
178 private static native void nativeEnableTracing(String path, int nativeLevels); | 195 private static native void nativeEnableTracing(String path, int nativeLevels); |
179 private static native void nativeEnableLogToDebugOutput(int nativeSeverity); | 196 private static native void nativeEnableLogToDebugOutput(int nativeSeverity); |
180 private static native void nativeEnableLogThreads(); | 197 private static native void nativeEnableLogThreads(); |
181 private static native void nativeEnableLogTimeStamps(); | 198 private static native void nativeEnableLogTimeStamps(); |
182 private static native void nativeLog(int severity, String tag, String message)
; | 199 private static native void nativeLog(int severity, String tag, String message)
; |
183 } | 200 } |
OLD | NEW |