Index: base/android/java/src/org/chromium/base/Log.java |
diff --git a/base/android/java/src/org/chromium/base/Log.java b/base/android/java/src/org/chromium/base/Log.java |
index 674273c8b5ecea998a26348ce341f689565595e0..99d755d2d078c37b2b61b35634dbb0ba71aa990d 100644 |
--- a/base/android/java/src/org/chromium/base/Log.java |
+++ b/base/android/java/src/org/chromium/base/Log.java |
@@ -4,6 +4,8 @@ |
package org.chromium.base; |
+import android.text.TextUtils; |
+ |
import org.chromium.base.annotations.NoSideEffects; |
import java.util.Locale; |
@@ -20,92 +22,64 @@ |
* Please make use of the formatting capability of the logging methods rather than doing |
* concatenations in the calling code. In the release builds of Chrome, debug and verbose log |
* calls will be stripped out of the binary. Concatenations and method calls however will still |
- * remain and be executed. If they can't be avoided, use {@link Log#isEnabled(int)} to guard |
- * such calls. Another possibility is to annotate methods to be called with {@link NoSideEffects}. |
+ * remain and be executed. If they can't be avoided, try to generate the logs in a method annotated |
+ * with {@link NoSideEffects}. Another possibility is to use |
+ * {@link android.util.Log#isLoggable(String, int)}, with {@link Log#makeTag(String)} to get the |
+ * correct tag. |
* </p> |
* |
* Usage: |
* <pre> |
- * Log.FEATURE.d("MyTag", "My %s message", awesome); |
+ * private void myMethod(String awesome) { |
+ * Log.i("Group", "My %s message.", awesome); |
+ * Log.d("Group", "My debug message"); |
+ * } |
* </pre> |
* |
* Logcat output: |
* <pre> |
- * D/chromium.Feature (999): [MyTag] My awesome message |
+ * I/chromium.Group (999): My awesome message |
+ * D/chromium.Group (999): [MyClass.java:42] My debug message |
* </pre> |
* |
- * Set the log level for a given feature: |
+ * Set the log level for a given group: |
* <pre> |
- * adb shell setprop log.tag.chromium:Feature VERBOSE |
+ * $ adb shell setprop log.tag.chromium.Group VERBOSE |
* </pre> |
- * |
- *<p> |
- *<b>Notes:</b> |
- * <ul> |
- * <li>For loggers configured to log the origin of debug calls (see {@link #Log(String, boolean)}, |
- * the tag provided for debug and verbose calls will be ignored and replaced in the log with |
- * the file name and the line number.</li> |
- * <li>New features or features not having a dedicated logger: please make a new one rather |
- * than using {@link #ROOT}.</li> |
- * </ul> |
- * </p> |
*/ |
public class Log { |
private static final String BASE_TAG = "chromium"; |
- /** |
- * Maximum length for the feature tag. |
- * |
- * A complete tag will look like <code>chromium:FooFeature</code>. Because of the 23 characters |
- * limit on log tags, feature tags have to be restricted to fit. |
- */ |
- private static final int MAX_FEATURE_TAG_LENGTH = 23 - 1 - BASE_TAG.length(); |
- |
- /** |
- * Logger for the "chromium" tag. |
- * Note: Disabling logging for that one will not disable the others. |
- */ |
- public static final Log ROOT = new Log(null, true); |
- |
- @VisibleForTesting |
- final String mTag; |
- private final boolean mDebugWithStack; |
- |
- /** |
- * Creates a new logging access point for the given tag. |
- * @param featureTag The complete log tag will be displayed as "chromium.featureTag". |
- * If <code>null</code>, it will only be "chromium". |
- * @param debugWithStack Whether to replace the secondary tag name with the file name and line |
- * number of the origin of the call for debug and verbose logs. |
- * @throws IllegalArgumentException If <code>featureTag</code> is too long. The complete |
- * tag has to fit within 23 characters. |
- */ |
- protected Log(String featureTag, boolean debugWithStack) { |
- mDebugWithStack = debugWithStack; |
- if (featureTag == null) { |
- mTag = BASE_TAG; |
- return; |
- } else if (featureTag.length() > MAX_FEATURE_TAG_LENGTH) { |
- throw new IllegalArgumentException( |
- "The feature tag can be at most " + MAX_FEATURE_TAG_LENGTH + " characters."); |
- } else { |
- mTag = BASE_TAG + "." + featureTag; |
- } |
- } |
- |
- /** Returns whether this logger is currently allowed to send logs.*/ |
- public boolean isEnabled(int level) { |
- return android.util.Log.isLoggable(mTag, level); |
+ private Log() { |
+ // Static only access |
} |
/** Returns a formatted log message, using the supplied format and arguments.*/ |
- @VisibleForTesting |
- protected String formatLog(String secondaryTag, String messageTemplate, Object... params) { |
+ private static String formatLog(String messageTemplate, Object... params) { |
if (params != null && params.length != 0) { |
messageTemplate = String.format(Locale.US, messageTemplate, params); |
} |
- return "[" + secondaryTag + "] " + messageTemplate; |
+ return messageTemplate; |
+ } |
+ |
+ /** |
+ * 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, Object... params) { |
+ return "[" + getCallOrigin() + "] " + formatLog(messageTemplate, params); |
+ } |
+ |
+ /** |
+ * Returns a full tag for the provided group tag. Full tags longer than 23 characters |
+ * will cause a runtime exception later. (see {@link android.util.Log#isLoggable(String, int)}) |
+ * |
+ * @param groupTag {@code null} and empty string are allowed. |
+ */ |
+ public static String makeTag(String groupTag) { |
+ if (TextUtils.isEmpty(groupTag)) return BASE_TAG; |
+ return BASE_TAG + "." + groupTag; |
} |
/** |
@@ -115,72 +89,70 @@ protected String formatLog(String secondaryTag, String messageTemplate, Object.. |
* than 7 parameters, consider building your log message using a function annotated with |
* {@link NoSideEffects}. |
* |
- * @param secondaryTag Used to identify the source of a log message. It usually identifies the |
- * class where the log call occurs. If the logger is configured to log the |
- * call's origin (see {@link #Log(String, boolean)}, this parameter is |
- * unused and will be replaced in the log message with the file name and |
- * the line number. |
+ * @param groupTag Used to identify the source of a log message. It usually refers to the |
+ * package or feature, to logically group related logs. |
+ * |
* @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. |
*/ |
- private void verbose(String secondaryTag, String messageTemplate, Object... args) { |
- if (isEnabled(android.util.Log.VERBOSE)) { |
- if (mDebugWithStack) secondaryTag = getCallOrigin(); |
- String message = formatLog(secondaryTag, messageTemplate, args); |
+ private static void verbose(String groupTag, String messageTemplate, Object... args) { |
+ String tag = makeTag(groupTag); |
+ if (android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) { |
+ String message = formatLogWithStack(messageTemplate, args); |
Throwable tr = getThrowableToLog(args); |
if (tr != null) { |
- android.util.Log.v(mTag, message, tr); |
+ android.util.Log.v(tag, message, tr); |
} else { |
- android.util.Log.v(mTag, message); |
+ android.util.Log.v(tag, message); |
} |
} |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 0 arg version. */ |
- public void v(String secondaryTag, String message) { |
- verbose(secondaryTag, message); |
+ public static void v(String groupTag, String message) { |
+ verbose(groupTag, message); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 1 arg version. */ |
- public void v(String secondaryTag, String messageTemplate, Object arg1) { |
- verbose(secondaryTag, messageTemplate, arg1); |
+ public static void v(String groupTag, String messageTemplate, Object arg1) { |
+ verbose(groupTag, messageTemplate, arg1); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 2 args version */ |
- public void v(String secondaryTag, String messageTemplate, Object arg1, Object arg2) { |
- verbose(secondaryTag, messageTemplate, arg1, arg2); |
+ public static void v(String groupTag, String messageTemplate, Object arg1, Object arg2) { |
+ verbose(groupTag, messageTemplate, arg1, arg2); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 3 args version */ |
- public void v( |
- String secondaryTag, String messageTemplate, Object arg1, Object arg2, Object arg3) { |
- verbose(secondaryTag, messageTemplate, arg1, arg2, arg3); |
+ public static void v( |
+ String groupTag, String messageTemplate, Object arg1, Object arg2, Object arg3) { |
+ verbose(groupTag, messageTemplate, arg1, arg2, arg3); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 4 args version */ |
- public void v(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void v(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4) { |
- verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4); |
+ verbose(groupTag, messageTemplate, arg1, arg2, arg3, arg4); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 5 args version */ |
- public void v(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void v(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4, Object arg5) { |
- verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5); |
+ verbose(groupTag, messageTemplate, arg1, arg2, arg3, arg4, arg5); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 6 args version */ |
- public void v(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void v(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4, Object arg5, Object arg6) { |
- verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6); |
+ verbose(groupTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6); |
} |
/** Sends a {@link android.util.Log#VERBOSE} log message. 7 args version */ |
- public void v(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void v(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) { |
- verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
+ verbose(groupTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
} |
/** |
@@ -190,90 +162,89 @@ public void v(String secondaryTag, String messageTemplate, Object arg1, Object a |
* than 7 parameters, consider building your log message using a function annotated with |
* {@link NoSideEffects}. |
* |
- * @param secondaryTag Used to identify the source of a log message. It usually identifies the |
- * class where the log call occurs. If the logger is configured to log the |
- * call's origin (see {@link #Log(String, boolean)}, this parameter is |
- * unused and will be replaced in the log message with the file name and |
- * the line number. |
+ * @param groupTag Used to identify the source of a log message. It usually refers to the |
+ * package or feature, to logically group related logs. |
+ * |
* @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. |
*/ |
- private void debug(String secondaryTag, String messageTemplate, Object... args) { |
- if (isEnabled(android.util.Log.DEBUG)) { |
- if (mDebugWithStack) secondaryTag = getCallOrigin(); |
- String message = formatLog(secondaryTag, messageTemplate, args); |
+ private static void debug(String groupTag, String messageTemplate, Object... args) { |
+ String tag = makeTag(groupTag); |
+ if (android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) { |
+ String message = formatLogWithStack(messageTemplate, args); |
Throwable tr = getThrowableToLog(args); |
if (tr != null) { |
- android.util.Log.d(mTag, message, tr); |
+ android.util.Log.d(tag, message, tr); |
} else { |
- android.util.Log.d(mTag, message); |
+ android.util.Log.d(tag, message); |
} |
} |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 0 arg version. */ |
- public void d(String secondaryTag, String message) { |
- debug(secondaryTag, message); |
+ public static void d(String groupTag, String message) { |
+ debug(groupTag, message); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 1 arg version. */ |
- public void d(String secondaryTag, String messageTemplate, Object arg1) { |
- debug(secondaryTag, messageTemplate, arg1); |
+ public static void d(String groupTag, String messageTemplate, Object arg1) { |
+ debug(groupTag, messageTemplate, arg1); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 2 args version */ |
- public void d(String secondaryTag, String messageTemplate, Object arg1, Object arg2) { |
- debug(secondaryTag, messageTemplate, arg1, arg2); |
+ public static void d(String groupTag, String messageTemplate, Object arg1, Object arg2) { |
+ debug(groupTag, messageTemplate, arg1, arg2); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 3 args version */ |
- public void d( |
- String secondaryTag, String messageTemplate, Object arg1, Object arg2, Object arg3) { |
- debug(secondaryTag, messageTemplate, arg1, arg2, arg3); |
+ public static void d( |
+ String groupTag, String messageTemplate, Object arg1, Object arg2, Object arg3) { |
+ debug(groupTag, messageTemplate, arg1, arg2, arg3); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 4 args version */ |
- public void d(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void d(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4) { |
- debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4); |
+ debug(groupTag, messageTemplate, arg1, arg2, arg3, arg4); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 5 args version */ |
- public void d(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void d(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4, Object arg5) { |
- debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5); |
+ debug(groupTag, messageTemplate, arg1, arg2, arg3, arg4, arg5); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 6 args version */ |
- public void d(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void d(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4, Object arg5, Object arg6) { |
- debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6); |
+ debug(groupTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6); |
} |
/** Sends a {@link android.util.Log#DEBUG} log message. 7 args version */ |
- public void d(String secondaryTag, String messageTemplate, Object arg1, Object arg2, |
+ public static void d(String groupTag, String messageTemplate, Object arg1, Object arg2, |
Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) { |
- debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
+ debug(groupTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7); |
} |
/** |
* Sends an {@link android.util.Log#INFO} log message. |
* |
- * @param secondaryTag Used to identify the source of a log message. It usually identifies the |
- * class where the log call occurs. |
+ * @param groupTag Used to identify the source of a log message. It usually refers to the |
+ * package or feature, to logically group related logs. |
* @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 void i(String secondaryTag, String messageTemplate, Object... args) { |
- if (isEnabled(android.util.Log.INFO)) { |
- String message = formatLog(secondaryTag, messageTemplate, args); |
+ public static void i(String groupTag, String messageTemplate, Object... args) { |
+ String tag = makeTag(groupTag); |
+ if (android.util.Log.isLoggable(tag, android.util.Log.INFO)) { |
+ String message = formatLog(messageTemplate, args); |
Throwable tr = getThrowableToLog(args); |
if (tr != null) { |
- android.util.Log.i(mTag, message, tr); |
+ android.util.Log.i(tag, message, tr); |
} else { |
- android.util.Log.i(mTag, message); |
+ android.util.Log.i(tag, message); |
} |
} |
} |
@@ -281,21 +252,22 @@ public void i(String secondaryTag, String messageTemplate, Object... args) { |
/** |
* Sends a {@link android.util.Log#WARN} log message. |
* |
- * @param secondaryTag Used to identify the source of a log message. It usually identifies the |
- * class where the log call occurs. |
+ * @param groupTag Used to identify the source of a log message. It usually refers to the |
+ * package or feature, to logically group related logs. |
* @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 void w(String secondaryTag, String messageTemplate, Object... args) { |
- if (isEnabled(android.util.Log.WARN)) { |
- String message = formatLog(secondaryTag, messageTemplate, args); |
+ public static void w(String groupTag, String messageTemplate, Object... args) { |
+ String tag = makeTag(groupTag); |
+ if (android.util.Log.isLoggable(tag, android.util.Log.WARN)) { |
+ String message = formatLog(messageTemplate, args); |
Throwable tr = getThrowableToLog(args); |
if (tr != null) { |
- android.util.Log.w(mTag, message, tr); |
+ android.util.Log.w(tag, message, tr); |
} else { |
- android.util.Log.w(mTag, message); |
+ android.util.Log.w(tag, message); |
} |
} |
} |
@@ -303,21 +275,22 @@ public void w(String secondaryTag, String messageTemplate, Object... args) { |
/** |
* Sends an {@link android.util.Log#ERROR} log message. |
* |
- * @param secondaryTag Used to identify the source of a log message. It usually identifies the |
- * class where the log call occurs. |
+ * @param groupTag Used to identify the source of a log message. It usually refers to the |
+ * package or feature, to logically group related logs. |
* @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 void e(String secondaryTag, String messageTemplate, Object... args) { |
- if (isEnabled(android.util.Log.ERROR)) { |
- String message = formatLog(secondaryTag, messageTemplate, args); |
+ public static void e(String groupTag, String messageTemplate, Object... args) { |
+ String tag = makeTag(groupTag); |
+ if (android.util.Log.isLoggable(tag, android.util.Log.ERROR)) { |
+ String message = formatLog(messageTemplate, args); |
Throwable tr = getThrowableToLog(args); |
if (tr != null) { |
- android.util.Log.e(mTag, message, tr); |
+ android.util.Log.e(tag, message, tr); |
} else { |
- android.util.Log.e(mTag, message); |
+ android.util.Log.e(tag, message); |
} |
} |
} |
@@ -329,26 +302,27 @@ public void e(String secondaryTag, String messageTemplate, Object... args) { |
* |
* @see android.util.Log#wtf(String, String, Throwable) |
* |
- * @param secondaryTag Used to identify the source of a log message. It usually identifies the |
- * class where the log call occurs. |
+ * @param groupTag Used to identify the source of a log message. It usually refers to the |
+ * package or feature, to logically group related logs. |
* @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 void wtf(String secondaryTag, String messageTemplate, Object... args) { |
- if (isEnabled(android.util.Log.ERROR)) { |
- String message = formatLog(secondaryTag, messageTemplate, args); |
+ public static void wtf(String groupTag, String messageTemplate, Object... args) { |
+ String tag = makeTag(groupTag); |
+ if (android.util.Log.isLoggable(tag, android.util.Log.ERROR)) { |
+ String message = formatLog(messageTemplate, args); |
Throwable tr = getThrowableToLog(args); |
if (tr != null) { |
- android.util.Log.wtf(mTag, message, tr); |
+ android.util.Log.wtf(tag, message, tr); |
} else { |
- android.util.Log.wtf(mTag, message); |
+ android.util.Log.wtf(tag, message); |
} |
} |
} |
- private Throwable getThrowableToLog(Object[] args) { |
+ private static Throwable getThrowableToLog(Object[] args) { |
if (args == null || args.length == 0) return null; |
Object lastArg = args[args.length - 1]; |
@@ -358,21 +332,22 @@ private Throwable getThrowableToLog(Object[] args) { |
} |
/** Returns a string form of the origin of the log call, to be used as secondary tag.*/ |
- private String getCallOrigin() { |
+ 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 privateLogFunction: verbose or debug |
- // +2 logFunction: v or d |
- // +3 caller |
+ // +2 formatLogWithStack() |
+ // +3 logFunction: v or d |
+ // +4 caller |
int callerStackIndex; |
String logClassName = Log.class.getName(); |
for (callerStackIndex = 0; callerStackIndex < st.length; callerStackIndex++) { |
if (st[callerStackIndex].getClassName().equals(logClassName)) { |
- callerStackIndex += 3; |
+ callerStackIndex += 4; |
break; |
} |
} |