forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.java
318 lines (284 loc) · 12.5 KB
/
Log.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base;
import org.chromium.build.BuildConfig;
import org.chromium.build.annotations.AlwaysInline;
import org.chromium.build.annotations.CheckDiscard;
import org.chromium.build.annotations.DoNotInline;
import java.util.Locale;
/**
* Utility class for Logging.
*
* <p>
* Defines logging access points for each feature. They format and forward the logs to
* {@link android.util.Log}, allowing to standardize the output, to make it easy to identify
* the origin of logs, and enable or disable logging in different parts of the code.
* </p>
* <p>
* Usage documentation: {@code //docs/android_logging.md}.
* </p>
*/
public class Log {
/** Convenience property, same as {@link android.util.Log#ASSERT}. */
public static final int ASSERT = android.util.Log.ASSERT;
/** Convenience property, same as {@link android.util.Log#DEBUG}. */
public static final int DEBUG = android.util.Log.DEBUG;
/** Convenience property, same as {@link android.util.Log#ERROR}. */
public static final int ERROR = android.util.Log.ERROR;
/** Convenience property, same as {@link android.util.Log#INFO}. */
public static final int INFO = android.util.Log.INFO;
/** Convenience property, same as {@link android.util.Log#VERBOSE}. */
public static final int VERBOSE = android.util.Log.VERBOSE;
/** Convenience property, same as {@link android.util.Log#WARN}. */
public static final int WARN = android.util.Log.WARN;
private Log() {
// Static only access
}
/** Returns a formatted log message, using the supplied format and arguments.*/
private static String formatLog(String messageTemplate, Throwable tr, Object... params) {
if ((params != null) && ((tr == null && params.length > 0) || params.length > 1)) {
messageTemplate = String.format(Locale.US, messageTemplate, params);
}
return messageTemplate;
}
/**
* Returns a normalized tag that will be in the form: "cr_foo". This function is called by the
* various Log overrides. If using {@link #isLoggable(String, int)}, you might want to call it
* to get the tag that will actually be used.
*/
@AlwaysInline
public static String normalizeTag(String tag) {
// @AlwaysInline makes sense because this method is almost always called with a string
// literal as a parameter, so inlining causes the .concat() to happen at build-time.
return "cr_" + tag;
}
/**
* Returns a formatted log message, using the supplied format and arguments.
* The message will be prepended with the filename and line number of the call.
*/
private static String formatLogWithStack(
String messageTemplate, Throwable tr, Object... params) {
return "[" + getCallOrigin() + "] " + formatLog(messageTemplate, tr, params);
}
/**
* When BuildConfig.ENABLE_DEBUG_LOGS=true, returns true. Otherwise, forwards to {@link
* android.util.Log#isLoggable(String, int)} (which returns false for levels < INFO, unless
* configured otherwise by R8's -maximumremovedandroidloglevel).
*
* <p>https://stackoverflow.com/questions/7948204/does-log-isloggable-returns-wrong-values
*/
@AlwaysInline
public static boolean isLoggable(String tag, int level) {
return BuildConfig.ENABLE_DEBUG_LOGS;
}
/**
* Sends a {@link android.util.Log#VERBOSE} log message.
*
* @param tag Used to identify the source of a log message. Might be modified in the output
* (see {@link #normalizeTag(String)})
* @param messageTemplate The message you would like logged. It is to be specified as a format
* string.
* @param args Arguments referenced by the format specifiers in the format string. If the last
* one is a {@link Throwable}, its trace will be printed.
*/
@CheckDiscard("crbug.com/1231625")
public static void v(String tag, String messageTemplate, Object... args) {
if (!isLoggable(tag, VERBOSE)) return;
Throwable tr = getThrowableToLog(args);
String message = formatLogWithStack(messageTemplate, tr, args);
tag = normalizeTag(tag);
if (tr != null) {
android.util.Log.v(tag, message, tr);
} else {
android.util.Log.v(tag, message);
}
}
/**
* Sends a {@link android.util.Log#DEBUG} log message.
*
* @param tag Used to identify the source of a log message. Might be modified in the output
* (see {@link #normalizeTag(String)})
* @param messageTemplate The message you would like logged. It is to be specified as a format
* string.
* @param args Arguments referenced by the format specifiers in the format string. If the last
* one is a {@link Throwable}, its trace will be printed.
*/
@CheckDiscard("crbug.com/1231625")
public static void d(String tag, String messageTemplate, Object... args) {
if (!isLoggable(tag, DEBUG)) return;
Throwable tr = getThrowableToLog(args);
String message = formatLogWithStack(messageTemplate, tr, args);
tag = normalizeTag(tag);
if (tr != null) {
android.util.Log.d(tag, message, tr);
} else {
android.util.Log.d(tag, message);
}
}
/**
* Sends an {@link android.util.Log#INFO} log message.
*
* @param tag Used to identify the source of a log message. Might be modified in the output
* (see {@link #normalizeTag(String)})
* @param messageTemplate The message you would like logged. It is to be specified as a format
* string.
* @param args Arguments referenced by the format specifiers in the format string. If the last
* one is a {@link Throwable}, its trace will be printed.
*/
public static void i(String tag, String messageTemplate, Object... args) {
Throwable tr = getThrowableToLog(args);
String message = formatLog(messageTemplate, tr, args);
tag = normalizeTag(tag);
if (tr != null) {
android.util.Log.i(tag, message, tr);
} else {
android.util.Log.i(tag, message);
}
}
// Overloads to avoid varargs overhead.
@AlwaysInline
public static void i(String tag, String message) {
android.util.Log.i(normalizeTag(tag), message);
}
@AlwaysInline
public static void i(String tag, String message, Throwable t) {
android.util.Log.i(normalizeTag(tag), message, t);
}
@DoNotInline
public static void i(String tag, String messageTemplate, Object o) {
i(tag, messageTemplate, new Object[] {o});
}
@DoNotInline
public static void i(String tag, String messageTemplate, Object o1, Object o2) {
i(tag, messageTemplate, new Object[] {o1, o2});
}
/**
* Sends a {@link android.util.Log#WARN} log message.
*
* @param tag Used to identify the source of a log message. Might be modified in the output
* (see {@link #normalizeTag(String)})
* @param messageTemplate The message you would like logged. It is to be specified as a format
* string.
* @param args Arguments referenced by the format specifiers in the format string. If the last
* one is a {@link Throwable}, its trace will be printed.
*/
public static void w(String tag, String messageTemplate, Object... args) {
Throwable tr = getThrowableToLog(args);
String message = formatLog(messageTemplate, tr, args);
tag = normalizeTag(tag);
if (tr != null) {
android.util.Log.w(tag, message, tr);
} else {
android.util.Log.w(tag, message);
}
}
// Overloads to avoid varargs overhead.
@AlwaysInline
public static void w(String tag, String message) {
android.util.Log.w(normalizeTag(tag), message);
}
@AlwaysInline
public static void w(String tag, String message, Throwable t) {
android.util.Log.w(normalizeTag(tag), message, t);
}
@DoNotInline
public static void w(String tag, String messageTemplate, Object o) {
w(tag, messageTemplate, new Object[] {o});
}
@DoNotInline
public static void w(String tag, String messageTemplate, Object o1, Object o2) {
w(tag, messageTemplate, new Object[] {o1, o2});
}
/**
* Sends an {@link android.util.Log#ERROR} log message.
*
* @param tag Used to identify the source of a log message. Might be modified in the output
* (see {@link #normalizeTag(String)})
* @param messageTemplate The message you would like logged. It is to be specified as a format
* string.
* @param args Arguments referenced by the format specifiers in the format string. If the last
* one is a {@link Throwable}, its trace will be printed.
*/
public static void e(String tag, String messageTemplate, Object... args) {
Throwable tr = getThrowableToLog(args);
String message = formatLog(messageTemplate, tr, args);
tag = normalizeTag(tag);
if (tr != null) {
android.util.Log.e(tag, message, tr);
} else {
android.util.Log.e(tag, message);
}
}
// Overloads to avoid varargs overhead.
@AlwaysInline
public static void e(String tag, String message) {
android.util.Log.e(normalizeTag(tag), message);
}
@AlwaysInline
public static void e(String tag, String message, Throwable t) {
android.util.Log.e(normalizeTag(tag), message, t);
}
@DoNotInline
public static void e(String tag, String messageTemplate, Object o) {
e(tag, messageTemplate, new Object[] {o});
}
@DoNotInline
public static void e(String tag, String messageTemplate, Object o1, Object o2) {
e(tag, messageTemplate, new Object[] {o1, o2});
}
/**
* What a Terrible Failure: Used for conditions that should never happen, and logged at
* the {@link android.util.Log#ASSERT} level. Depending on the configuration, it might
* terminate the process.
*
* @see android.util.Log#wtf(String, String, Throwable)
*
* @param tag Used to identify the source of a log message. Might be modified in the output
* (see {@link #normalizeTag(String)})
* @param messageTemplate The message you would like logged. It is to be specified as a format
* string.
* @param args Arguments referenced by the format specifiers in the format string. If the last
* one is a {@link Throwable}, its trace will be printed.
*/
public static void wtf(String tag, String messageTemplate, Object... args) {
Throwable tr = getThrowableToLog(args);
String message = formatLog(messageTemplate, tr, args);
tag = normalizeTag(tag);
if (tr != null) {
android.util.Log.wtf(tag, message, tr);
} else {
android.util.Log.wtf(tag, message);
}
}
/** Handy function to get a loggable stack trace from a Throwable. */
public static String getStackTraceString(Throwable tr) {
return android.util.Log.getStackTraceString(tr);
}
private static Throwable getThrowableToLog(Object[] args) {
if (args == null || args.length == 0) return null;
Object lastArg = args[args.length - 1];
if (!(lastArg instanceof Throwable)) return null;
return (Throwable) lastArg;
}
/** Returns a string form of the origin of the log call, to be used as secondary tag.*/
@CheckDiscard("crbug.com/1231625")
private static String getCallOrigin() {
StackTraceElement[] st = Thread.currentThread().getStackTrace();
// The call stack should look like:
// n [a variable number of calls depending on the vm used]
// +0 getCallOrigin()
// +1 formatLogWithStack()
// +2 privateLogFunction: verbose or debug
// +3 caller
int callerStackIndex;
String logClassName = Log.class.getName();
for (callerStackIndex = 0; callerStackIndex < st.length; callerStackIndex++) {
if (st[callerStackIndex].getClassName().equals(logClassName)) {
callerStackIndex += 3;
break;
}
}
return st[callerStackIndex].getFileName() + ":" + st[callerStackIndex].getLineNumber();
}
}