| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 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 | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 package org.webrtc; | |
| 12 | |
| 13 import java.io.PrintWriter; | |
| 14 import java.io.StringWriter; | |
| 15 import java.util.EnumSet; | |
| 16 import java.util.logging.Level; | |
| 17 import java.util.logging.Logger; | |
| 18 | |
| 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 */ | |
| 29 public class Logging { | |
| 30 private static final Logger fallbackLogger = createFallbackLogger(); | |
| 31 private static volatile boolean tracingEnabled; | |
| 32 private static volatile boolean loggingEnabled; | |
| 33 private static enum NativeLibStatus { UNINITIALIZED, LOADED, FAILED } | |
| 34 private static volatile NativeLibStatus nativeLibStatus = NativeLibStatus.UNIN
ITIALIZED; | |
| 35 | |
| 36 private static Logger createFallbackLogger() { | |
| 37 final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging"); | |
| 38 fallbackLogger.setLevel(Level.ALL); | |
| 39 return fallbackLogger; | |
| 40 } | |
| 41 | |
| 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 } | |
| 51 } | |
| 52 return nativeLibStatus == NativeLibStatus.LOADED; | |
| 53 } | |
| 54 | |
| 55 // Keep in sync with webrtc/common_types.h:TraceLevel. | |
| 56 public enum TraceLevel { | |
| 57 TRACE_NONE(0x0000), | |
| 58 TRACE_STATEINFO(0x0001), | |
| 59 TRACE_WARNING(0x0002), | |
| 60 TRACE_ERROR(0x0004), | |
| 61 TRACE_CRITICAL(0x0008), | |
| 62 TRACE_APICALL(0x0010), | |
| 63 TRACE_DEFAULT(0x00ff), | |
| 64 TRACE_MODULECALL(0x0020), | |
| 65 TRACE_MEMORY(0x0100), | |
| 66 TRACE_TIMER(0x0200), | |
| 67 TRACE_STREAM(0x0400), | |
| 68 TRACE_DEBUG(0x0800), | |
| 69 TRACE_INFO(0x1000), | |
| 70 TRACE_TERSEINFO(0x2000), | |
| 71 TRACE_ALL(0xffff); | |
| 72 | |
| 73 public final int level; | |
| 74 TraceLevel(int level) { | |
| 75 this.level = level; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // Keep in sync with webrtc/base/logging.h:LoggingSeverity. | |
| 80 public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR
, LS_NONE } | |
| 81 | |
| 82 public static void enableLogThreads() { | |
| 83 if (!loadNativeLibrary()) { | |
| 84 fallbackLogger.log(Level.WARNING, "Cannot enable log thread because native
lib not loaded."); | |
| 85 return; | |
| 86 } | |
| 87 nativeEnableLogThreads(); | |
| 88 } | |
| 89 | |
| 90 public static void enableLogTimeStamps() { | |
| 91 if (!loadNativeLibrary()) { | |
| 92 fallbackLogger.log( | |
| 93 Level.WARNING, "Cannot enable log timestamps because native lib not lo
aded."); | |
| 94 return; | |
| 95 } | |
| 96 nativeEnableLogTimeStamps(); | |
| 97 } | |
| 98 | |
| 99 // Enable tracing to |path| of messages of |levels|. | |
| 100 // On Android, use "logcat:" for |path| to send output there. | |
| 101 // Note: this function controls the output of the WEBRTC_TRACE() macros. | |
| 102 public static synchronized void enableTracing(String path, EnumSet<TraceLevel>
levels) { | |
| 103 if (!loadNativeLibrary()) { | |
| 104 fallbackLogger.log(Level.WARNING, "Cannot enable tracing because native li
b not loaded."); | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 if (tracingEnabled) { | |
| 109 return; | |
| 110 } | |
| 111 int nativeLevel = 0; | |
| 112 for (TraceLevel level : levels) { | |
| 113 nativeLevel |= level.level; | |
| 114 } | |
| 115 nativeEnableTracing(path, nativeLevel); | |
| 116 tracingEnabled = true; | |
| 117 } | |
| 118 | |
| 119 // Enable diagnostic logging for messages of |severity| to the platform debug | |
| 120 // output. On Android, the output will be directed to Logcat. | |
| 121 // Note: this function starts collecting the output of the LOG() macros. | |
| 122 public static synchronized void enableLogToDebugOutput(Severity severity) { | |
| 123 if (!loadNativeLibrary()) { | |
| 124 fallbackLogger.log(Level.WARNING, "Cannot enable logging because native li
b not loaded."); | |
| 125 return; | |
| 126 } | |
| 127 nativeEnableLogToDebugOutput(severity.ordinal()); | |
| 128 loggingEnabled = true; | |
| 129 } | |
| 130 | |
| 131 public static void log(Severity severity, String tag, String message) { | |
| 132 if (loggingEnabled) { | |
| 133 nativeLog(severity.ordinal(), tag, message); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 // Fallback to system log. | |
| 138 Level level; | |
| 139 switch (severity) { | |
| 140 case LS_ERROR: | |
| 141 level = Level.SEVERE; | |
| 142 break; | |
| 143 case LS_WARNING: | |
| 144 level = Level.WARNING; | |
| 145 break; | |
| 146 case LS_INFO: | |
| 147 level = Level.INFO; | |
| 148 break; | |
| 149 default: | |
| 150 level = Level.FINE; | |
| 151 break; | |
| 152 } | |
| 153 fallbackLogger.log(level, tag + ": " + message); | |
| 154 } | |
| 155 | |
| 156 public static void d(String tag, String message) { | |
| 157 log(Severity.LS_INFO, tag, message); | |
| 158 } | |
| 159 | |
| 160 public static void e(String tag, String message) { | |
| 161 log(Severity.LS_ERROR, tag, message); | |
| 162 } | |
| 163 | |
| 164 public static void w(String tag, String message) { | |
| 165 log(Severity.LS_WARNING, tag, message); | |
| 166 } | |
| 167 | |
| 168 public static void e(String tag, String message, Throwable e) { | |
| 169 log(Severity.LS_ERROR, tag, message); | |
| 170 log(Severity.LS_ERROR, tag, e.toString()); | |
| 171 log(Severity.LS_ERROR, tag, getStackTraceString(e)); | |
| 172 } | |
| 173 | |
| 174 public static void w(String tag, String message, Throwable e) { | |
| 175 log(Severity.LS_WARNING, tag, message); | |
| 176 log(Severity.LS_WARNING, tag, e.toString()); | |
| 177 log(Severity.LS_WARNING, tag, getStackTraceString(e)); | |
| 178 } | |
| 179 | |
| 180 public static void v(String tag, String message) { | |
| 181 log(Severity.LS_VERBOSE, tag, message); | |
| 182 } | |
| 183 | |
| 184 private static String getStackTraceString(Throwable e) { | |
| 185 if (e == null) { | |
| 186 return ""; | |
| 187 } | |
| 188 | |
| 189 StringWriter sw = new StringWriter(); | |
| 190 PrintWriter pw = new PrintWriter(sw); | |
| 191 e.printStackTrace(pw); | |
| 192 return sw.toString(); | |
| 193 } | |
| 194 | |
| 195 private static native void nativeEnableTracing(String path, int nativeLevels); | |
| 196 private static native void nativeEnableLogToDebugOutput(int nativeSeverity); | |
| 197 private static native void nativeEnableLogThreads(); | |
| 198 private static native void nativeEnableLogTimeStamps(); | |
| 199 private static native void nativeLog(int severity, String tag, String message)
; | |
| 200 } | |
| OLD | NEW |