| 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 org.chromium.base.annotations.RemovableInRelease; | 7 import org.chromium.base.annotations.RemovableInRelease; |
| 8 | 8 |
| 9 import java.util.Locale; | 9 import java.util.Locale; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Utility class for Logging. | 12 * Utility class for Logging. |
| 13 * | 13 * |
| 14 * <p> | 14 * <p> |
| 15 * Defines logging access points for each feature. They format and forward the l
ogs to | 15 * Defines logging access points for each feature. They format and forward the l
ogs to |
| 16 * {@link android.util.Log}, allowing to standardize the output, to make it easy
to identify | 16 * {@link android.util.Log}, allowing to standardize the output, to make it easy
to identify |
| 17 * the origin of logs, and enable or disable logging in different parts of the c
ode. | 17 * the origin of logs, and enable or disable logging in different parts of the c
ode. |
| 18 * </p> | 18 * </p> |
| 19 * <p> | 19 * <p> |
| 20 * @see usage documentation: <a href="README_logging.md">README_logging.md</a>. | 20 * Usage documentation: {@code //docs/logging.md}. |
| 21 * </p> | 21 * </p> |
| 22 */ | 22 */ |
| 23 public class Log { | 23 public class Log { |
| 24 /** Convenience property, same as {@link android.util.Log#ASSERT}. */ | 24 /** Convenience property, same as {@link android.util.Log#ASSERT}. */ |
| 25 public static final int ASSERT = android.util.Log.ASSERT; | 25 public static final int ASSERT = android.util.Log.ASSERT; |
| 26 | 26 |
| 27 /** Convenience property, same as {@link android.util.Log#DEBUG}. */ | 27 /** Convenience property, same as {@link android.util.Log#DEBUG}. */ |
| 28 public static final int DEBUG = android.util.Log.DEBUG; | 28 public static final int DEBUG = android.util.Log.DEBUG; |
| 29 | 29 |
| 30 /** Convenience property, same as {@link android.util.Log#ERROR}. */ | 30 /** Convenience property, same as {@link android.util.Log#ERROR}. */ |
| (...skipping 22 matching lines...) Expand all Loading... |
| 53 } | 53 } |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Returns a formatted log message, using the supplied format and arguments. | 56 * Returns a formatted log message, using the supplied format and arguments. |
| 57 * The message will be prepended with the filename and line number of the ca
ll. | 57 * The message will be prepended with the filename and line number of the ca
ll. |
| 58 */ | 58 */ |
| 59 private static String formatLogWithStack(String messageTemplate, Object... p
arams) { | 59 private static String formatLogWithStack(String messageTemplate, Object... p
arams) { |
| 60 return "[" + getCallOrigin() + "] " + formatLog(messageTemplate, params)
; | 60 return "[" + getCallOrigin() + "] " + formatLog(messageTemplate, params)
; |
| 61 } | 61 } |
| 62 | 62 |
| 63 /** Convenience function, forwards to {@link android.util.Log#isLoggable(Str
ing, int)}. */ | 63 /** |
| 64 * Convenience function, forwards to {@link android.util.Log#isLoggable(Stri
ng, int)}. |
| 65 * |
| 66 * Note: Has no effect on whether logs are sent or not. Use a method with |
| 67 * {@link RemovableInRelease} to log something in Debug builds only. |
| 68 */ |
| 64 public static boolean isLoggable(String tag, int level) { | 69 public static boolean isLoggable(String tag, int level) { |
| 65 return android.util.Log.isLoggable(tag, level); | 70 return android.util.Log.isLoggable(tag, level); |
| 66 } | 71 } |
| 67 | 72 |
| 68 /** | 73 /** |
| 69 * Sends a {@link android.util.Log#VERBOSE} log message. | 74 * Sends a {@link android.util.Log#VERBOSE} log message. |
| 70 * | 75 * |
| 71 * For optimization purposes, only the fixed parameters versions are visible
. If you need more | 76 * For optimization purposes, only the fixed parameters versions are visible
. If you need more |
| 72 * than 7 parameters, consider building your log message using a function an
notated with | 77 * than 7 parameters, consider building your log message using a function an
notated with |
| 73 * {@link RemovableInRelease}. | 78 * {@link RemovableInRelease}. |
| 74 * | 79 * |
| 75 * @param tag Used to identify the source of a log message. | 80 * @param tag Used to identify the source of a log message. |
| 76 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 81 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
| 77 * string. | 82 * string. |
| 78 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 83 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
| 79 * one is a {@link Throwable}, its trace will be printed. | 84 * one is a {@link Throwable}, its trace will be printed. |
| 80 */ | 85 */ |
| 81 private static void verbose(String tag, String messageTemplate, Object... ar
gs) { | 86 private static void verbose(String tag, String messageTemplate, Object... ar
gs) { |
| 82 if (Log.isLoggable(tag, Log.VERBOSE)) { | 87 String message = formatLogWithStack(messageTemplate, args); |
| 83 String message = formatLogWithStack(messageTemplate, args); | 88 Throwable tr = getThrowableToLog(args); |
| 84 Throwable tr = getThrowableToLog(args); | 89 if (tr != null) { |
| 85 if (tr != null) { | 90 android.util.Log.v(tag, message, tr); |
| 86 android.util.Log.v(tag, message, tr); | 91 } else { |
| 87 } else { | 92 android.util.Log.v(tag, message); |
| 88 android.util.Log.v(tag, message); | |
| 89 } | |
| 90 } | 93 } |
| 91 } | 94 } |
| 92 | 95 |
| 93 /** Sends a {@link android.util.Log#VERBOSE} log message. 0 args version. */ | 96 /** Sends a {@link android.util.Log#VERBOSE} log message. 0 args version. */ |
| 94 @RemovableInRelease | 97 @RemovableInRelease |
| 95 @VisibleForTesting | 98 @VisibleForTesting |
| 96 public static void v(String tag, String message) { | 99 public static void v(String tag, String message) { |
| 97 verbose(tag, message); | 100 verbose(tag, message); |
| 98 } | 101 } |
| 99 | 102 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 * than 7 parameters, consider building your log message using a function an
notated with | 161 * than 7 parameters, consider building your log message using a function an
notated with |
| 159 * {@link RemovableInRelease}. | 162 * {@link RemovableInRelease}. |
| 160 * | 163 * |
| 161 * @param tag Used to identify the source of a log message. | 164 * @param tag Used to identify the source of a log message. |
| 162 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 165 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
| 163 * string. | 166 * string. |
| 164 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 167 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
| 165 * one is a {@link Throwable}, its trace will be printed. | 168 * one is a {@link Throwable}, its trace will be printed. |
| 166 */ | 169 */ |
| 167 private static void debug(String tag, String messageTemplate, Object... args
) { | 170 private static void debug(String tag, String messageTemplate, Object... args
) { |
| 168 if (isLoggable(tag, Log.DEBUG)) { | 171 String message = formatLogWithStack(messageTemplate, args); |
| 169 String message = formatLogWithStack(messageTemplate, args); | 172 Throwable tr = getThrowableToLog(args); |
| 170 Throwable tr = getThrowableToLog(args); | 173 if (tr != null) { |
| 171 if (tr != null) { | 174 android.util.Log.d(tag, message, tr); |
| 172 android.util.Log.d(tag, message, tr); | 175 } else { |
| 173 } else { | 176 android.util.Log.d(tag, message); |
| 174 android.util.Log.d(tag, message); | |
| 175 } | |
| 176 } | 177 } |
| 177 } | 178 } |
| 178 | 179 |
| 179 /** Sends a {@link android.util.Log#DEBUG} log message. 0 args version. */ | 180 /** Sends a {@link android.util.Log#DEBUG} log message. 0 args version. */ |
| 180 @RemovableInRelease | 181 @RemovableInRelease |
| 181 @VisibleForTesting | 182 @VisibleForTesting |
| 182 public static void d(String tag, String message) { | 183 public static void d(String tag, String message) { |
| 183 debug(tag, message); | 184 debug(tag, message); |
| 184 } | 185 } |
| 185 | 186 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 * Sends an {@link android.util.Log#INFO} log message. | 240 * Sends an {@link android.util.Log#INFO} log message. |
| 240 * | 241 * |
| 241 * @param tag Used to identify the source of a log message. | 242 * @param tag Used to identify the source of a log message. |
| 242 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 243 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
| 243 * string. | 244 * string. |
| 244 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 245 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
| 245 * one is a {@link Throwable}, its trace will be printed. | 246 * one is a {@link Throwable}, its trace will be printed. |
| 246 */ | 247 */ |
| 247 @VisibleForTesting | 248 @VisibleForTesting |
| 248 public static void i(String tag, String messageTemplate, Object... args) { | 249 public static void i(String tag, String messageTemplate, Object... args) { |
| 249 if (Log.isLoggable(tag, Log.INFO)) { | 250 String message = formatLog(messageTemplate, args); |
| 250 String message = formatLog(messageTemplate, args); | 251 Throwable tr = getThrowableToLog(args); |
| 251 Throwable tr = getThrowableToLog(args); | 252 if (tr != null) { |
| 252 if (tr != null) { | 253 android.util.Log.i(tag, message, tr); |
| 253 android.util.Log.i(tag, message, tr); | 254 } else { |
| 254 } else { | 255 android.util.Log.i(tag, message); |
| 255 android.util.Log.i(tag, message); | |
| 256 } | |
| 257 } | 256 } |
| 258 } | 257 } |
| 259 | 258 |
| 260 /** | 259 /** |
| 261 * Sends a {@link android.util.Log#WARN} log message. | 260 * Sends a {@link android.util.Log#WARN} log message. |
| 262 * | 261 * |
| 263 * @param tag Used to identify the source of a log message. | 262 * @param tag Used to identify the source of a log message. |
| 264 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 263 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
| 265 * string. | 264 * string. |
| 266 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 265 * @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. | 266 * one is a {@link Throwable}, its trace will be printed. |
| 268 */ | 267 */ |
| 269 @VisibleForTesting | 268 @VisibleForTesting |
| 270 public static void w(String tag, String messageTemplate, Object... args) { | 269 public static void w(String tag, String messageTemplate, Object... args) { |
| 271 if (Log.isLoggable(tag, Log.WARN)) { | 270 String message = formatLog(messageTemplate, args); |
| 272 String message = formatLog(messageTemplate, args); | 271 Throwable tr = getThrowableToLog(args); |
| 273 Throwable tr = getThrowableToLog(args); | 272 if (tr != null) { |
| 274 if (tr != null) { | 273 android.util.Log.w(tag, message, tr); |
| 275 android.util.Log.w(tag, message, tr); | 274 } else { |
| 276 } else { | 275 android.util.Log.w(tag, message); |
| 277 android.util.Log.w(tag, message); | |
| 278 } | |
| 279 } | 276 } |
| 280 } | 277 } |
| 281 | 278 |
| 282 /** | 279 /** |
| 283 * Sends an {@link android.util.Log#ERROR} log message. | 280 * Sends an {@link android.util.Log#ERROR} log message. |
| 284 * | 281 * |
| 285 * @param tag Used to identify the source of a log message. | 282 * @param tag Used to identify the source of a log message. |
| 286 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 283 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
| 287 * string. | 284 * string. |
| 288 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 285 * @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. | 286 * one is a {@link Throwable}, its trace will be printed. |
| 290 */ | 287 */ |
| 291 @VisibleForTesting | 288 @VisibleForTesting |
| 292 public static void e(String tag, String messageTemplate, Object... args) { | 289 public static void e(String tag, String messageTemplate, Object... args) { |
| 293 if (Log.isLoggable(tag, Log.ERROR)) { | 290 String message = formatLog(messageTemplate, args); |
| 294 String message = formatLog(messageTemplate, args); | 291 Throwable tr = getThrowableToLog(args); |
| 295 Throwable tr = getThrowableToLog(args); | 292 if (tr != null) { |
| 296 if (tr != null) { | 293 android.util.Log.e(tag, message, tr); |
| 297 android.util.Log.e(tag, message, tr); | 294 } else { |
| 298 } else { | 295 android.util.Log.e(tag, message); |
| 299 android.util.Log.e(tag, message); | |
| 300 } | |
| 301 } | 296 } |
| 302 } | 297 } |
| 303 | 298 |
| 304 /** | 299 /** |
| 305 * What a Terrible Failure: Used for conditions that should never happen, an
d logged at | 300 * What a Terrible Failure: Used for conditions that should never happen, an
d logged at |
| 306 * the {@link android.util.Log#ASSERT} level. Depending on the configuration
, it might | 301 * the {@link android.util.Log#ASSERT} level. Depending on the configuration
, it might |
| 307 * terminate the process. | 302 * terminate the process. |
| 308 * | 303 * |
| 309 * @see android.util.Log#wtf(String, String, Throwable) | 304 * @see android.util.Log#wtf(String, String, Throwable) |
| 310 * | 305 * |
| 311 * @param tag Used to identify the source of a log message. | 306 * @param tag Used to identify the source of a log message. |
| 312 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format | 307 * @param messageTemplate The message you would like logged. It is to be spe
cified as a format |
| 313 * string. | 308 * string. |
| 314 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last | 309 * @param args Arguments referenced by the format specifiers in the format s
tring. If the last |
| 315 * one is a {@link Throwable}, its trace will be printed. | 310 * one is a {@link Throwable}, its trace will be printed. |
| 316 */ | 311 */ |
| 317 @VisibleForTesting | 312 @VisibleForTesting |
| 318 public static void wtf(String tag, String messageTemplate, Object... args) { | 313 public static void wtf(String tag, String messageTemplate, Object... args) { |
| 319 if (Log.isLoggable(tag, Log.ASSERT)) { | 314 String message = formatLog(messageTemplate, args); |
| 320 String message = formatLog(messageTemplate, args); | 315 Throwable tr = getThrowableToLog(args); |
| 321 Throwable tr = getThrowableToLog(args); | 316 if (tr != null) { |
| 322 if (tr != null) { | 317 android.util.Log.wtf(tag, message, tr); |
| 323 android.util.Log.wtf(tag, message, tr); | 318 } else { |
| 324 } else { | 319 android.util.Log.wtf(tag, message); |
| 325 android.util.Log.wtf(tag, message); | |
| 326 } | |
| 327 } | 320 } |
| 328 } | 321 } |
| 329 | 322 |
| 330 private static Throwable getThrowableToLog(Object[] args) { | 323 private static Throwable getThrowableToLog(Object[] args) { |
| 331 if (args == null || args.length == 0) return null; | 324 if (args == null || args.length == 0) return null; |
| 332 | 325 |
| 333 Object lastArg = args[args.length - 1]; | 326 Object lastArg = args[args.length - 1]; |
| 334 | 327 |
| 335 if (!(lastArg instanceof Throwable)) return null; | 328 if (!(lastArg instanceof Throwable)) return null; |
| 336 return (Throwable) lastArg; | 329 return (Throwable) lastArg; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 353 for (callerStackIndex = 0; callerStackIndex < st.length; callerStackInde
x++) { | 346 for (callerStackIndex = 0; callerStackIndex < st.length; callerStackInde
x++) { |
| 354 if (st[callerStackIndex].getClassName().equals(logClassName)) { | 347 if (st[callerStackIndex].getClassName().equals(logClassName)) { |
| 355 callerStackIndex += 4; | 348 callerStackIndex += 4; |
| 356 break; | 349 break; |
| 357 } | 350 } |
| 358 } | 351 } |
| 359 | 352 |
| 360 return st[callerStackIndex].getFileName() + ":" + st[callerStackIndex].g
etLineNumber(); | 353 return st[callerStackIndex].getFileName() + ":" + st[callerStackIndex].g
etLineNumber(); |
| 361 } | 354 } |
| 362 } | 355 } |
| OLD | NEW |