OLD | NEW |
| (Empty) |
1 /* | |
2 * libjingle | |
3 * Copyright 2013 Google Inc. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright notice, | |
9 * this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
11 * this list of conditions and the following disclaimer in the documentation | |
12 * and/or other materials provided with the distribution. | |
13 * 3. The name of the author may not be used to endorse or promote products | |
14 * derived from this software without specific prior written permission. | |
15 * | |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 */ | |
27 | |
28 package org.webrtc; | |
29 | |
30 import java.io.PrintWriter; | |
31 import java.io.StringWriter; | |
32 import java.nio.charset.Charset; | |
33 import java.util.EnumSet; | |
34 | |
35 /** Java wrapper for WebRTC & libjingle logging. */ | |
36 public class Logging { | |
37 static { | |
38 System.loadLibrary("jingle_peerconnection_so"); | |
39 } | |
40 | |
41 // Keep in sync with webrtc/common_types.h:TraceLevel. | |
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; | |
60 TraceLevel(int level) { | |
61 this.level = level; | |
62 } | |
63 }; | |
64 | |
65 // Keep in sync with webrtc/base/logging.h:LoggingSeverity. | |
66 public enum Severity { | |
67 LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR, | |
68 }; | |
69 | |
70 public static void enableLogThreads() { | |
71 nativeEnableLogThreads(); | |
72 } | |
73 | |
74 public static void enableLogTimeStamps() { | |
75 nativeEnableLogTimeStamps(); | |
76 } | |
77 | |
78 // Enable tracing to |path| of messages of |levels| and |severity|. | |
79 // On Android, use "logcat:" for |path| to send output there. | |
80 public static void enableTracing( | |
81 String path, EnumSet<TraceLevel> levels, Severity severity) { | |
82 int nativeLevel = 0; | |
83 for (TraceLevel level : levels) { | |
84 nativeLevel |= level.level; | |
85 } | |
86 nativeEnableTracing(path, nativeLevel, severity.ordinal()); | |
87 } | |
88 | |
89 public static void log(Severity severity, String tag, String message) { | |
90 nativeLog(severity.ordinal(), tag, message); | |
91 } | |
92 | |
93 public static void d(String tag, String message) { | |
94 log(Severity.LS_INFO, tag, message); | |
95 } | |
96 | |
97 public static void e(String tag, String message) { | |
98 log(Severity.LS_ERROR, tag, message); | |
99 } | |
100 | |
101 public static void w(String tag, String message) { | |
102 log(Severity.LS_WARNING, tag, message); | |
103 } | |
104 | |
105 public static void e(String tag, String message, Throwable e) { | |
106 log(Severity.LS_ERROR, tag, message); | |
107 log(Severity.LS_ERROR, tag, e.toString()); | |
108 log(Severity.LS_ERROR, tag, getStackTraceString(e)); | |
109 } | |
110 | |
111 public static void w(String tag, String message, Throwable e) { | |
112 log(Severity.LS_WARNING, tag, message); | |
113 log(Severity.LS_WARNING, tag, e.toString()); | |
114 log(Severity.LS_WARNING, tag, getStackTraceString(e)); | |
115 } | |
116 | |
117 public static void v(String tag, String message) { | |
118 log(Severity.LS_VERBOSE, tag, message); | |
119 } | |
120 | |
121 private static String getStackTraceString(Throwable e) { | |
122 if (e == null) { | |
123 return ""; | |
124 } | |
125 | |
126 StringWriter sw = new StringWriter(); | |
127 PrintWriter pw = new PrintWriter(sw); | |
128 e.printStackTrace(pw); | |
129 return sw.toString(); | |
130 } | |
131 | |
132 private static native void nativeEnableTracing( | |
133 String path, int nativeLevels, int nativeSeverity); | |
134 private static native void nativeEnableLogThreads(); | |
135 private static native void nativeEnableLogTimeStamps(); | |
136 private static native void nativeLog(int severity, String tag, String message)
; | |
137 } | |
OLD | NEW |