OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import android.text.TextUtils; | 7 import android.text.TextUtils; |
8 | 8 |
9 import org.chromium.base.annotations.NoSideEffects; | 9 import org.chromium.base.annotations.NoSideEffects; |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 * Please make use of the formatting capability of the logging methods rather th
an doing | 22 * Please make use of the formatting capability of the logging methods rather th
an doing |
23 * concatenations in the calling code. In the release builds of Chrome, debug an
d verbose log | 23 * concatenations in the calling code. In the release builds of Chrome, debug an
d verbose log |
24 * calls will be stripped out of the binary. Concatenations and method calls how
ever will still | 24 * calls will be stripped out of the binary. Concatenations and method calls how
ever will still |
25 * remain and be executed. If they can't be avoided, try to generate the logs in
a method annotated | 25 * remain and be executed. If they can't be avoided, try to generate the logs in
a method annotated |
26 * with {@link NoSideEffects}. Another possibility is to use | 26 * with {@link NoSideEffects}. Another possibility is to use |
27 * {@link android.util.Log#isLoggable(String, int)} to guard those calls. | 27 * {@link android.util.Log#isLoggable(String, int)} to guard those calls. |
28 * </p> | 28 * </p> |
29 * | 29 * |
30 * Usage: | 30 * Usage: |
31 * <pre> | 31 * <pre> |
32 * private static final String TAG = Log.makeTag("Group"); | 32 * private static final String TAG = "cr.Group"; |
33 * | 33 * |
34 * private void myMethod(String awesome) { | 34 * private void myMethod(String awesome) { |
35 * Log.i(TAG, "My %s message.", awesome); | 35 * Log.i(TAG, "My %s message.", awesome); |
36 * Log.d(TAG, "My debug message"); | 36 * Log.d(TAG, "My debug message"); |
37 * } | 37 * } |
38 * </pre> | 38 * </pre> |
39 * | 39 * |
40 * Logcat output: | 40 * Logcat output: |
41 * <pre> | 41 * <pre> |
42 * I/cr.Group (999): My awesome message | 42 * I/cr.Group (999): My awesome message |
43 * D/cr.Group (999): [MyClass.java:42] My debug message | 43 * D/cr.Group (999): [MyClass.java:42] My debug message |
44 * </pre> | 44 * </pre> |
45 * | 45 * |
46 * Set the log level for a given group: | 46 * Set the log level for a given group: |
47 * <pre> | 47 * <pre> |
48 * $ adb shell setprop log.tag.chromium.Group VERBOSE | 48 * $ adb shell setprop log.tag.cr.Group VERBOSE |
49 * </pre> | 49 * </pre> |
50 */ | 50 */ |
51 public class Log { | 51 public class Log { |
52 private static final String BASE_TAG = "cr"; | |
53 | |
54 /** Convenience property, same as {@link android.util.Log#ASSERT}. */ | 52 /** Convenience property, same as {@link android.util.Log#ASSERT}. */ |
55 public static final int ASSERT = android.util.Log.ASSERT; | 53 public static final int ASSERT = android.util.Log.ASSERT; |
56 | 54 |
57 /** Convenience property, same as {@link android.util.Log#DEBUG}. */ | 55 /** Convenience property, same as {@link android.util.Log#DEBUG}. */ |
58 public static final int DEBUG = android.util.Log.DEBUG; | 56 public static final int DEBUG = android.util.Log.DEBUG; |
59 | 57 |
60 /** Convenience property, same as {@link android.util.Log#ERROR}. */ | 58 /** Convenience property, same as {@link android.util.Log#ERROR}. */ |
61 public static final int ERROR = android.util.Log.ERROR; | 59 public static final int ERROR = android.util.Log.ERROR; |
62 | 60 |
63 /** Convenience property, same as {@link android.util.Log#INFO}. */ | 61 /** Convenience property, same as {@link android.util.Log#INFO}. */ |
(...skipping 27 matching lines...) Expand all Loading... |
91 } | 89 } |
92 | 90 |
93 /** | 91 /** |
94 * Returns a full tag for the provided group tag. Full tags longer than 23 c
haracters | 92 * Returns a full tag for the provided group tag. Full tags longer than 23 c
haracters |
95 * will cause a runtime exception. | 93 * will cause a runtime exception. |
96 * | 94 * |
97 * @param groupTag {@code null} and empty string are allowed. | 95 * @param groupTag {@code null} and empty string are allowed. |
98 * | 96 * |
99 * @see android.util.Log#isLoggable(String, int) | 97 * @see android.util.Log#isLoggable(String, int) |
100 * @throws IllegalArgumentException if the tag is too long. | 98 * @throws IllegalArgumentException if the tag is too long. |
| 99 * @deprecated Directly use a string (e.g. "cr.Tag") in your class. See http
://crbug.com/485772 |
101 */ | 100 */ |
| 101 @Deprecated |
102 public static String makeTag(String groupTag) { | 102 public static String makeTag(String groupTag) { |
103 if (TextUtils.isEmpty(groupTag)) return BASE_TAG; | 103 if (TextUtils.isEmpty(groupTag)) return "cr"; |
104 String tag = BASE_TAG + "." + groupTag; | 104 String tag = "cr." + groupTag; |
105 if (tag.length() > 23) { | 105 if (tag.length() > 23) { |
106 throw new IllegalArgumentException( | 106 throw new IllegalArgumentException( |
107 "The full tag (" + tag + ") is longer than 23 characters."); | 107 "The full tag (" + tag + ") is longer than 23 characters."); |
108 } | 108 } |
109 return tag; | 109 return tag; |
110 } | 110 } |
111 | 111 |
112 /** Convenience function, forwards to {@link android.util.Log#isLoggable(Str
ing, int)}. */ | 112 /** Convenience function, forwards to {@link android.util.Log#isLoggable(Str
ing, int)}. */ |
113 public static boolean isLoggable(String tag, int level) { | 113 public static boolean isLoggable(String tag, int level) { |
114 return android.util.Log.isLoggable(tag, level); | 114 return android.util.Log.isLoggable(tag, level); |
115 } | 115 } |
116 | 116 |
117 /** | 117 /** |
118 * Sends a {@link android.util.Log#VERBOSE} log message. | 118 * Sends a {@link android.util.Log#VERBOSE} log message. |
119 * | 119 * |
120 * For optimization purposes, only the fixed parameters versions are visible
. If you need more | 120 * For optimization purposes, only the fixed parameters versions are visible
. If you need more |
121 * than 7 parameters, consider building your log message using a function an
notated with | 121 * than 7 parameters, consider building your log message using a function an
notated with |
122 * {@link NoSideEffects}. | 122 * {@link NoSideEffects}. |
123 * | 123 * |
124 * @param tag Used to identify the source of a log message. Should be create
d using | 124 * @param tag Used to identify the source of a log message. |
125 * {@link #makeTag(String)}. | |
126 * | |
127 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 125 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
128 * string. | 126 * string. |
129 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 127 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
130 * one is a {@link Throwable}, its trace will be printed. | 128 * one is a {@link Throwable}, its trace will be printed. |
131 */ | 129 */ |
132 private static void verbose(String tag, String messageTemplate, Object... ar
gs) { | 130 private static void verbose(String tag, String messageTemplate, Object... ar
gs) { |
133 if (android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) { | 131 if (android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) { |
134 String message = formatLogWithStack(messageTemplate, args); | 132 String message = formatLogWithStack(messageTemplate, args); |
135 Throwable tr = getThrowableToLog(args); | 133 Throwable tr = getThrowableToLog(args); |
136 if (tr != null) { | 134 if (tr != null) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 verbose(tag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); | 184 verbose(tag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
187 } | 185 } |
188 | 186 |
189 /** | 187 /** |
190 * Sends a {@link android.util.Log#DEBUG} log message. | 188 * Sends a {@link android.util.Log#DEBUG} log message. |
191 * | 189 * |
192 * For optimization purposes, only the fixed parameters versions are visible
. If you need more | 190 * For optimization purposes, only the fixed parameters versions are visible
. If you need more |
193 * than 7 parameters, consider building your log message using a function an
notated with | 191 * than 7 parameters, consider building your log message using a function an
notated with |
194 * {@link NoSideEffects}. | 192 * {@link NoSideEffects}. |
195 * | 193 * |
196 * @param tag Used to identify the source of a log message. Should be create
d using | 194 * @param tag Used to identify the source of a log message. |
197 * {@link #makeTag(String)}. | |
198 * | |
199 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 195 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
200 * string. | 196 * string. |
201 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 197 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
202 * one is a {@link Throwable}, its trace will be printed. | 198 * one is a {@link Throwable}, its trace will be printed. |
203 */ | 199 */ |
204 private static void debug(String tag, String messageTemplate, Object... args
) { | 200 private static void debug(String tag, String messageTemplate, Object... args
) { |
205 if (android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) { | 201 if (android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) { |
206 String message = formatLogWithStack(messageTemplate, args); | 202 String message = formatLogWithStack(messageTemplate, args); |
207 Throwable tr = getThrowableToLog(args); | 203 Throwable tr = getThrowableToLog(args); |
208 if (tr != null) { | 204 if (tr != null) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 248 |
253 /** Sends a {@link android.util.Log#DEBUG} log message. 7 args version */ | 249 /** Sends a {@link android.util.Log#DEBUG} log message. 7 args version */ |
254 public static void d(String tag, String messageTemplate, Object arg1, Object
arg2, Object arg3, | 250 public static void d(String tag, String messageTemplate, Object arg1, Object
arg2, Object arg3, |
255 Object arg4, Object arg5, Object arg6, Object arg7) { | 251 Object arg4, Object arg5, Object arg6, Object arg7) { |
256 debug(tag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); | 252 debug(tag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
257 } | 253 } |
258 | 254 |
259 /** | 255 /** |
260 * Sends an {@link android.util.Log#INFO} log message. | 256 * Sends an {@link android.util.Log#INFO} log message. |
261 * | 257 * |
262 * @param tag Used to identify the source of a log message. Should be create
d using | 258 * @param tag Used to identify the source of a log message. |
263 * {@link #makeTag(String)}. | |
264 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 259 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
265 * string. | 260 * string. |
266 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 261 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
267 * one is a {@link Throwable}, its trace will be printed. | 262 * one is a {@link Throwable}, its trace will be printed. |
268 */ | 263 */ |
269 public static void i(String tag, String messageTemplate, Object... args) { | 264 public static void i(String tag, String messageTemplate, Object... args) { |
270 if (android.util.Log.isLoggable(tag, android.util.Log.INFO)) { | 265 if (android.util.Log.isLoggable(tag, android.util.Log.INFO)) { |
271 String message = formatLog(messageTemplate, args); | 266 String message = formatLog(messageTemplate, args); |
272 Throwable tr = getThrowableToLog(args); | 267 Throwable tr = getThrowableToLog(args); |
273 if (tr != null) { | 268 if (tr != null) { |
274 android.util.Log.i(tag, message, tr); | 269 android.util.Log.i(tag, message, tr); |
275 } else { | 270 } else { |
276 android.util.Log.i(tag, message); | 271 android.util.Log.i(tag, message); |
277 } | 272 } |
278 } | 273 } |
279 } | 274 } |
280 | 275 |
281 /** | 276 /** |
282 * Sends a {@link android.util.Log#WARN} log message. | 277 * Sends a {@link android.util.Log#WARN} log message. |
283 * | 278 * |
284 * @param tag Used to identify the source of a log message. Should be create
d using | 279 * @param tag Used to identify the source of a log message. |
285 * {@link #makeTag(String)}. | |
286 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 280 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
287 * string. | 281 * string. |
288 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 282 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
289 * one is a {@link Throwable}, its trace will be printed. | 283 * one is a {@link Throwable}, its trace will be printed. |
290 */ | 284 */ |
291 public static void w(String tag, String messageTemplate, Object... args) { | 285 public static void w(String tag, String messageTemplate, Object... args) { |
292 if (android.util.Log.isLoggable(tag, android.util.Log.WARN)) { | 286 if (android.util.Log.isLoggable(tag, android.util.Log.WARN)) { |
293 String message = formatLog(messageTemplate, args); | 287 String message = formatLog(messageTemplate, args); |
294 Throwable tr = getThrowableToLog(args); | 288 Throwable tr = getThrowableToLog(args); |
295 if (tr != null) { | 289 if (tr != null) { |
296 android.util.Log.w(tag, message, tr); | 290 android.util.Log.w(tag, message, tr); |
297 } else { | 291 } else { |
298 android.util.Log.w(tag, message); | 292 android.util.Log.w(tag, message); |
299 } | 293 } |
300 } | 294 } |
301 } | 295 } |
302 | 296 |
303 /** | 297 /** |
304 * Sends an {@link android.util.Log#ERROR} log message. | 298 * Sends an {@link android.util.Log#ERROR} log message. |
305 * | 299 * |
306 * @param tag Used to identify the source of a log message. Should be create
d using | 300 * @param tag Used to identify the source of a log message. |
307 * {@link #makeTag(String)}. | |
308 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 301 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
309 * string. | 302 * string. |
310 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 303 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
311 * one is a {@link Throwable}, its trace will be printed. | 304 * one is a {@link Throwable}, its trace will be printed. |
312 */ | 305 */ |
313 public static void e(String tag, String messageTemplate, Object... args) { | 306 public static void e(String tag, String messageTemplate, Object... args) { |
314 if (android.util.Log.isLoggable(tag, android.util.Log.ERROR)) { | 307 if (android.util.Log.isLoggable(tag, android.util.Log.ERROR)) { |
315 String message = formatLog(messageTemplate, args); | 308 String message = formatLog(messageTemplate, args); |
316 Throwable tr = getThrowableToLog(args); | 309 Throwable tr = getThrowableToLog(args); |
317 if (tr != null) { | 310 if (tr != null) { |
318 android.util.Log.e(tag, message, tr); | 311 android.util.Log.e(tag, message, tr); |
319 } else { | 312 } else { |
320 android.util.Log.e(tag, message); | 313 android.util.Log.e(tag, message); |
321 } | 314 } |
322 } | 315 } |
323 } | 316 } |
324 | 317 |
325 /** | 318 /** |
326 * What a Terrible Failure: Used for conditions that should never happen, an
d logged at | 319 * What a Terrible Failure: Used for conditions that should never happen, an
d logged at |
327 * the {@link android.util.Log#ASSERT} level. Depending on the configuration
, it might | 320 * the {@link android.util.Log#ASSERT} level. Depending on the configuration
, it might |
328 * terminate the process. | 321 * terminate the process. |
329 * | 322 * |
330 * @see android.util.Log#wtf(String, String, Throwable) | 323 * @see android.util.Log#wtf(String, String, Throwable) |
331 * | 324 * |
332 * @param tag Used to identify the source of a log message. Should be create
d using | 325 * @param tag Used to identify the source of a log message. |
333 * {@link #makeTag(String)}. | |
334 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 326 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
335 * string. | 327 * string. |
336 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 328 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
337 * one is a {@link Throwable}, its trace will be printed. | 329 * one is a {@link Throwable}, its trace will be printed. |
338 */ | 330 */ |
339 public static void wtf(String tag, String messageTemplate, Object... args) { | 331 public static void wtf(String tag, String messageTemplate, Object... args) { |
340 if (android.util.Log.isLoggable(tag, android.util.Log.ERROR)) { | 332 if (android.util.Log.isLoggable(tag, android.util.Log.ERROR)) { |
341 String message = formatLog(messageTemplate, args); | 333 String message = formatLog(messageTemplate, args); |
342 Throwable tr = getThrowableToLog(args); | 334 Throwable tr = getThrowableToLog(args); |
343 if (tr != null) { | 335 if (tr != null) { |
(...skipping 30 matching lines...) Expand all Loading... |
374 for (callerStackIndex = 0; callerStackIndex < st.length; callerStackInde
x++) { | 366 for (callerStackIndex = 0; callerStackIndex < st.length; callerStackInde
x++) { |
375 if (st[callerStackIndex].getClassName().equals(logClassName)) { | 367 if (st[callerStackIndex].getClassName().equals(logClassName)) { |
376 callerStackIndex += 4; | 368 callerStackIndex += 4; |
377 break; | 369 break; |
378 } | 370 } |
379 } | 371 } |
380 | 372 |
381 return st[callerStackIndex].getFileName() + ":" + st[callerStackIndex].g
etLineNumber(); | 373 return st[callerStackIndex].getFileName() + ":" + st[callerStackIndex].g
etLineNumber(); |
382 } | 374 } |
383 } | 375 } |
OLD | NEW |