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.enableLogToDebugOutput
26  *
27  * Using these APIs requires that the native library is loaded, using
28  * PeerConnectionFactory.initialize.
29  */
30 public class Logging {
31   private static final Logger fallbackLogger = createFallbackLogger();
32   private static volatile boolean loggingEnabled;
33 
createFallbackLogger()34   private static Logger createFallbackLogger() {
35     final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
36     fallbackLogger.setLevel(Level.ALL);
37     return fallbackLogger;
38   }
39 
40   // TODO(solenberg): Remove once dependent projects updated.
41   @Deprecated
42   public enum TraceLevel {
43     TRACE_NONE(0x0000),
44     TRACE_STATEINFO(0x0001),
45     TRACE_WARNING(0x0002),
46     TRACE_ERROR(0x0004),
47     TRACE_CRITICAL(0x0008),
48     TRACE_APICALL(0x0010),
49     TRACE_DEFAULT(0x00ff),
50     TRACE_MODULECALL(0x0020),
51     TRACE_MEMORY(0x0100),
52     TRACE_TIMER(0x0200),
53     TRACE_STREAM(0x0400),
54     TRACE_DEBUG(0x0800),
55     TRACE_INFO(0x1000),
56     TRACE_TERSEINFO(0x2000),
57     TRACE_ALL(0xffff);
58 
59     public final int level;
TraceLevel(int level)60     TraceLevel(int level) {
61       this.level = level;
62     }
63   }
64 
65   // Keep in sync with webrtc/rtc_base/logging.h:LoggingSeverity.
66   public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR, LS_NONE }
67 
enableLogThreads()68   public static void enableLogThreads() {
69     nativeEnableLogThreads();
70   }
71 
enableLogTimeStamps()72   public static void enableLogTimeStamps() {
73     nativeEnableLogTimeStamps();
74   }
75 
76   // TODO(solenberg): Remove once dependent projects updated.
77   @Deprecated
enableTracing(String path, EnumSet<TraceLevel> levels)78   public static void enableTracing(String path, EnumSet<TraceLevel> levels) {}
79 
80   // Enable diagnostic logging for messages of |severity| to the platform debug
81   // output. On Android, the output will be directed to Logcat.
82   // Note: this function starts collecting the output of the RTC_LOG() macros.
83   // TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression.
84   @SuppressWarnings("NoSynchronizedMethodCheck")
enableLogToDebugOutput(Severity severity)85   public static synchronized void enableLogToDebugOutput(Severity severity) {
86     nativeEnableLogToDebugOutput(severity.ordinal());
87     loggingEnabled = true;
88   }
89 
log(Severity severity, String tag, String message)90   public static void log(Severity severity, String tag, String message) {
91     if (loggingEnabled) {
92       nativeLog(severity.ordinal(), tag, message);
93       return;
94     }
95 
96     // Fallback to system log.
97     Level level;
98     switch (severity) {
99       case LS_ERROR:
100         level = Level.SEVERE;
101         break;
102       case LS_WARNING:
103         level = Level.WARNING;
104         break;
105       case LS_INFO:
106         level = Level.INFO;
107         break;
108       default:
109         level = Level.FINE;
110         break;
111     }
112     fallbackLogger.log(level, tag + ": " + message);
113   }
114 
d(String tag, String message)115   public static void d(String tag, String message) {
116     log(Severity.LS_INFO, tag, message);
117   }
118 
e(String tag, String message)119   public static void e(String tag, String message) {
120     log(Severity.LS_ERROR, tag, message);
121   }
122 
w(String tag, String message)123   public static void w(String tag, String message) {
124     log(Severity.LS_WARNING, tag, message);
125   }
126 
e(String tag, String message, Throwable e)127   public static void e(String tag, String message, Throwable e) {
128     log(Severity.LS_ERROR, tag, message);
129     log(Severity.LS_ERROR, tag, e.toString());
130     log(Severity.LS_ERROR, tag, getStackTraceString(e));
131   }
132 
w(String tag, String message, Throwable e)133   public static void w(String tag, String message, Throwable e) {
134     log(Severity.LS_WARNING, tag, message);
135     log(Severity.LS_WARNING, tag, e.toString());
136     log(Severity.LS_WARNING, tag, getStackTraceString(e));
137   }
138 
v(String tag, String message)139   public static void v(String tag, String message) {
140     log(Severity.LS_VERBOSE, tag, message);
141   }
142 
getStackTraceString(Throwable e)143   private static String getStackTraceString(Throwable e) {
144     if (e == null) {
145       return "";
146     }
147 
148     StringWriter sw = new StringWriter();
149     PrintWriter pw = new PrintWriter(sw);
150     e.printStackTrace(pw);
151     return sw.toString();
152   }
153 
nativeEnableLogToDebugOutput(int nativeSeverity)154   private static native void nativeEnableLogToDebugOutput(int nativeSeverity);
nativeEnableLogThreads()155   private static native void nativeEnableLogThreads();
nativeEnableLogTimeStamps()156   private static native void nativeEnableLogTimeStamps();
nativeLog(int severity, String tag, String message)157   private static native void nativeLog(int severity, String tag, String message);
158 }
159