Merge "Don't throw a NPE in SharedLog#e"

This commit is contained in:
Remi NGUYEN VAN
2018-09-28 01:08:59 +00:00
committed by Gerrit Code Review
2 changed files with 16 additions and 3 deletions

View File

@@ -17,6 +17,7 @@
package android.net.util;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.text.TextUtils;
import android.util.LocalLog;
import android.util.Log;
@@ -92,10 +93,17 @@ public class SharedLog {
}
/**
* Log an error due to an exception, with the exception stacktrace.
* Log an error due to an exception, with the exception stacktrace if provided.
*
* <p>The error and exception message appear in the shared log, but the stacktrace is only
* logged in general log output (logcat).
*/
public void e(@NonNull String msg, @NonNull Throwable e) {
Log.e(mTag, record(Category.ERROR, msg + ": " + e.getMessage()), e);
public void e(@NonNull String msg, @Nullable Throwable exception) {
if (exception == null) {
e(msg);
return;
}
Log.e(mTag, record(Category.ERROR, msg + ": " + exception.getMessage()), exception);
}
public void i(String msg) {

View File

@@ -44,6 +44,8 @@ public class SharedLogTest {
final SharedLog logLevel2a = logTop.forSubComponent("twoA");
final SharedLog logLevel2b = logTop.forSubComponent("twoB");
logLevel2b.e("2b or not 2b");
logLevel2b.e("No exception", null);
logLevel2b.e("Wait, here's one", new Exception("Test"));
logLevel2a.w("second post?");
final SharedLog logLevel3 = logLevel2a.forSubComponent("three");
@@ -54,6 +56,9 @@ public class SharedLogTest {
final String[] expected = {
" - MARK first post!",
" - [twoB] ERROR 2b or not 2b",
" - [twoB] ERROR No exception",
// No stacktrace in shared log, only in logcat
" - [twoB] ERROR Wait, here's one: Test",
" - [twoA] WARN second post?",
" - still logging",
" - [twoA.three] 3 >> 2",