Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(617)

Unified Diff: base/android/java/src/org/chromium/base/Log.java

Issue 1069373002: Add Exception stack trace support to base.Log (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add throwable support everywhere, add Log#wtf Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 d08cae3abf26a8f51e734f62e5c33141a0596c05..674273c8b5ecea998a26348ce341f689565595e0 100644
--- a/base/android/java/src/org/chromium/base/Log.java
+++ b/base/android/java/src/org/chromium/base/Log.java
@@ -122,12 +122,19 @@ protected String formatLog(String secondaryTag, String messageTemplate, Object..
* the line number.
* @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.
+ * @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();
- android.util.Log.v(mTag, formatLog(secondaryTag, messageTemplate, args));
+ String message = formatLog(secondaryTag, messageTemplate, args);
+ Throwable tr = getThrowableToLog(args);
+ if (tr != null) {
+ android.util.Log.v(mTag, message, tr);
+ } else {
+ android.util.Log.v(mTag, message);
+ }
}
}
@@ -190,12 +197,19 @@ public void v(String secondaryTag, String messageTemplate, Object arg1, Object a
* the line number.
* @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.
+ * @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();
- android.util.Log.d(mTag, formatLog(secondaryTag, messageTemplate, args));
+ String message = formatLog(secondaryTag, messageTemplate, args);
+ Throwable tr = getThrowableToLog(args);
+ if (tr != null) {
+ android.util.Log.d(mTag, message, tr);
+ } else {
+ android.util.Log.d(mTag, message);
+ }
}
}
@@ -249,11 +263,18 @@ public void d(String secondaryTag, String messageTemplate, Object arg1, Object a
* class where the log call occurs.
* @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.
+ * @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)) {
- android.util.Log.i(mTag, formatLog(secondaryTag, messageTemplate, args));
+ String message = formatLog(secondaryTag, messageTemplate, args);
+ Throwable tr = getThrowableToLog(args);
+ if (tr != null) {
+ android.util.Log.i(mTag, message, tr);
+ } else {
+ android.util.Log.i(mTag, message);
+ }
}
}
@@ -264,11 +285,18 @@ public void i(String secondaryTag, String messageTemplate, Object... args) {
* class where the log call occurs.
* @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.
+ * @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)) {
- android.util.Log.w(mTag, formatLog(secondaryTag, messageTemplate, args));
+ String message = formatLog(secondaryTag, messageTemplate, args);
+ Throwable tr = getThrowableToLog(args);
+ if (tr != null) {
+ android.util.Log.w(mTag, message, tr);
+ } else {
+ android.util.Log.w(mTag, message);
+ }
}
}
@@ -279,14 +307,56 @@ public void w(String secondaryTag, String messageTemplate, Object... args) {
* class where the log call occurs.
* @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.
+ * @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)) {
- android.util.Log.e(mTag, formatLog(secondaryTag, messageTemplate, args));
+ String message = formatLog(secondaryTag, messageTemplate, args);
+ Throwable tr = getThrowableToLog(args);
+ if (tr != null) {
+ android.util.Log.e(mTag, message, tr);
+ } else {
+ android.util.Log.e(mTag, message);
+ }
+ }
+ }
+
+ /**
+ * 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 secondaryTag Used to identify the source of a log message. It usually identifies the
+ * class where the log call occurs.
+ * @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);
+ Throwable tr = getThrowableToLog(args);
+ if (tr != null) {
+ android.util.Log.wtf(mTag, message, tr);
+ } else {
+ android.util.Log.wtf(mTag, message);
+ }
}
}
+ private 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.*/
private String getCallOrigin() {
StackTraceElement[] st = Thread.currentThread().getStackTrace();
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698