Loading core/java/android/util/Log.java +29 −25 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.util; import android.annotation.NonNull; import android.annotation.Nullable; import android.os.DeadSystemException; import com.android.internal.os.RuntimeInit; Loading Loading @@ -120,7 +122,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int v(String tag, String msg) { public static int v(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, VERBOSE, tag, msg); } Loading @@ -131,7 +133,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int v(String tag, String msg, Throwable tr) { public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, VERBOSE, tag, msg, tr); } Loading @@ -141,7 +143,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int d(String tag, String msg) { public static int d(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, DEBUG, tag, msg); } Loading @@ -152,7 +154,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int d(String tag, String msg, Throwable tr) { public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, DEBUG, tag, msg, tr); } Loading @@ -162,7 +164,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int i(String tag, String msg) { public static int i(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, INFO, tag, msg); } Loading @@ -173,7 +175,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int i(String tag, String msg, Throwable tr) { public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, INFO, tag, msg, tr); } Loading @@ -183,7 +185,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int w(String tag, String msg) { public static int w(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, WARN, tag, msg); } Loading @@ -194,7 +196,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int w(String tag, String msg, Throwable tr) { public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, WARN, tag, msg, tr); } Loading @@ -218,15 +220,15 @@ public final class Log { * for Nougat (7.0) releases (API <= 23) and prior, there is no * tag limit of concern after this API level. */ public static native boolean isLoggable(String tag, int level); public static native boolean isLoggable(@Nullable String tag, int level); /* /** * Send a {@link #WARN} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param tr An exception to log */ public static int w(String tag, Throwable tr) { public static int w(@Nullable String tag, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, WARN, tag, "", tr); } Loading @@ -236,7 +238,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int e(String tag, String msg) { public static int e(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, ERROR, tag, msg); } Loading @@ -247,7 +249,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int e(String tag, String msg, Throwable tr) { public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr); } Loading @@ -260,7 +262,7 @@ public final class Log { * @param tag Used to identify the source of a log message. * @param msg The message you would like logged. */ public static int wtf(String tag, String msg) { public static int wtf(@Nullable String tag, @Nullable String msg) { return wtf(LOG_ID_MAIN, tag, msg, null, false, false); } Loading @@ -269,7 +271,7 @@ public final class Log { * call stack. * @hide */ public static int wtfStack(String tag, String msg) { public static int wtfStack(@Nullable String tag, @Nullable String msg) { return wtf(LOG_ID_MAIN, tag, msg, null, true, false); } Loading @@ -279,7 +281,7 @@ public final class Log { * @param tag Used to identify the source of a log message. * @param tr An exception to log. */ public static int wtf(String tag, Throwable tr) { public static int wtf(@Nullable String tag, @NonNull Throwable tr) { return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false); } Loading @@ -290,12 +292,12 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log. May be null. */ public static int wtf(String tag, String msg, Throwable tr) { public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return wtf(LOG_ID_MAIN, tag, msg, tr, false, false); } static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack, boolean system) { static int wtf(int logId, @Nullable String tag, @Nullable String msg, @Nullable Throwable tr, boolean localStack, boolean system) { TerribleFailure what = new TerribleFailure(msg, tr); // Only mark this as ERROR, do not use ASSERT since that should be // reserved for cases where the system is guaranteed to abort. Loading @@ -305,7 +307,7 @@ public final class Log { return bytes; } static void wtfQuiet(int logId, String tag, String msg, boolean system) { static void wtfQuiet(int logId, @Nullable String tag, @Nullable String msg, boolean system) { TerribleFailure what = new TerribleFailure(msg, null); sWtfHandler.onTerribleFailure(tag, what, system); } Loading @@ -317,7 +319,8 @@ public final class Log { * * @hide */ public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) { @NonNull public static TerribleFailureHandler setWtfHandler(@NonNull TerribleFailureHandler handler) { if (handler == null) { throw new NullPointerException("handler == null"); } Loading @@ -330,7 +333,8 @@ public final class Log { * Handy function to get a loggable stack trace from a Throwable * @param tr An exception to log */ public static String getStackTraceString(Throwable tr) { @NonNull public static String getStackTraceString(@Nullable Throwable tr) { if (tr == null) { return ""; } Loading Loading @@ -360,7 +364,7 @@ public final class Log { * @param msg The message you would like logged. * @return The number of bytes written. */ public static int println(int priority, String tag, String msg) { public static int println(int priority, @Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, priority, tag, msg); } Loading @@ -385,8 +389,8 @@ public final class Log { * chunks. This is to avoid truncation. * @hide */ public static int printlns(int bufID, int priority, String tag, String msg, Throwable tr) { public static int printlns(int bufID, int priority, @Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { ImmediateLogWriter logWriter = new ImmediateLogWriter(bufID, priority, tag); // Acceptable buffer size. Get the native buffer size, subtract two zero terminators, // and the length of the tag. Loading core/tests/coretests/src/android/util/LogNullabilityTest.java 0 → 100644 +170 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.util; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; import org.junit.Test; import org.junit.runner.RunWith; @SmallTest @RunWith(AndroidJUnit4.class) public final class LogNullabilityTest { @Test public void nullTag() { Log.v(null, ""); Log.d(null, ""); Log.i(null, ""); Log.w(null, ""); Log.e(null, ""); Log.wtf(null, ""); Log.wtfStack(null, ""); Log.println(Log.INFO, null, ""); // Implicit assertions of not crashing. } @Test public void nullTagWithThrowable() { Log.v(null, "", new Throwable()); Log.d(null, "", new Throwable()); Log.i(null, "", new Throwable()); Log.w(null, "", new Throwable()); Log.e(null, "", new Throwable()); Log.wtf(null, "", new Throwable()); Log.printlns(Log.LOG_ID_MAIN, Log.INFO, null, "", new Throwable()); // Implicit assertions of not crashing. } @Test public void nullMessage() { try { Log.v("", null); fail(); } catch (NullPointerException expected) { } try { Log.d("", null); fail(); } catch (NullPointerException expected) { } try { Log.i("", null); fail(); } catch (NullPointerException expected) { } try { // Explicit cast needed because there's a weird (String, Throwable) overload. Log.w("", (String) null); fail(); } catch (NullPointerException expected) { } try { Log.e("", null); fail(); } catch (NullPointerException expected) { } Log.wtf("", (String) null); Log.wtfStack("", (String) null); // Implicit assertion of not crashing. try { Log.println(Log.INFO, "", null); fail(); } catch (NullPointerException expected) { } } @Test public void nullMessageWithThrowable() { Log.v("", null, new Throwable()); Log.d("", null, new Throwable()); Log.i("", null, new Throwable()); Log.w("", null, new Throwable()); Log.e("", null, new Throwable()); Log.wtf("", null, new Throwable()); // Implicit assertions of not crashing. try { Log.printlns(Log.LOG_ID_MAIN, Log.INFO, "", null, new Throwable()); fail(); } catch (NullPointerException expected) { } } @Test public void nullThrowable() { Log.v("", "", null); Log.d("", "", null); Log.i("", "", null); Log.w("", "", null); Log.e("", "", null); Log.wtf("", "", null); // Warning has its own (String, Throwable) overload. Log.w("", (Throwable) null); Log.printlns(Log.LOG_ID_MAIN, Log.INFO, "", "", null); // Implicit assertions of not crashing. // WTF has its own (String, Throwable) overload with different behavior. try { Log.wtf("", (Throwable) null); fail(); } catch (NullPointerException expected) { } } @Test public void nullMessageWithNullThrowable() { Log.v("", null, null); Log.d("", null, null); Log.i("", null, null); Log.w("", null, null); Log.e("", null, null); Log.wtf("", null, null); // Implicit assertions of not crashing. try { Log.printlns(Log.LOG_ID_MAIN, Log.INFO, "", null, null); fail(); } catch (NullPointerException expected) { } } @Test public void nullTagIsLoggable() { Log.isLoggable(null, Log.INFO); // Implicit assertion of not crashing. } @Test public void nullGetStackTraceString() { assertNotNull(Log.getStackTraceString(null)); } } Loading
core/java/android/util/Log.java +29 −25 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.util; import android.annotation.NonNull; import android.annotation.Nullable; import android.os.DeadSystemException; import com.android.internal.os.RuntimeInit; Loading Loading @@ -120,7 +122,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int v(String tag, String msg) { public static int v(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, VERBOSE, tag, msg); } Loading @@ -131,7 +133,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int v(String tag, String msg, Throwable tr) { public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, VERBOSE, tag, msg, tr); } Loading @@ -141,7 +143,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int d(String tag, String msg) { public static int d(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, DEBUG, tag, msg); } Loading @@ -152,7 +154,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int d(String tag, String msg, Throwable tr) { public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, DEBUG, tag, msg, tr); } Loading @@ -162,7 +164,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int i(String tag, String msg) { public static int i(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, INFO, tag, msg); } Loading @@ -173,7 +175,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int i(String tag, String msg, Throwable tr) { public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, INFO, tag, msg, tr); } Loading @@ -183,7 +185,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int w(String tag, String msg) { public static int w(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, WARN, tag, msg); } Loading @@ -194,7 +196,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int w(String tag, String msg, Throwable tr) { public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, WARN, tag, msg, tr); } Loading @@ -218,15 +220,15 @@ public final class Log { * for Nougat (7.0) releases (API <= 23) and prior, there is no * tag limit of concern after this API level. */ public static native boolean isLoggable(String tag, int level); public static native boolean isLoggable(@Nullable String tag, int level); /* /** * Send a {@link #WARN} log message and log the exception. * @param tag Used to identify the source of a log message. It usually identifies * the class or activity where the log call occurs. * @param tr An exception to log */ public static int w(String tag, Throwable tr) { public static int w(@Nullable String tag, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, WARN, tag, "", tr); } Loading @@ -236,7 +238,7 @@ public final class Log { * the class or activity where the log call occurs. * @param msg The message you would like logged. */ public static int e(String tag, String msg) { public static int e(@Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, ERROR, tag, msg); } Loading @@ -247,7 +249,7 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log */ public static int e(String tag, String msg, Throwable tr) { public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr); } Loading @@ -260,7 +262,7 @@ public final class Log { * @param tag Used to identify the source of a log message. * @param msg The message you would like logged. */ public static int wtf(String tag, String msg) { public static int wtf(@Nullable String tag, @Nullable String msg) { return wtf(LOG_ID_MAIN, tag, msg, null, false, false); } Loading @@ -269,7 +271,7 @@ public final class Log { * call stack. * @hide */ public static int wtfStack(String tag, String msg) { public static int wtfStack(@Nullable String tag, @Nullable String msg) { return wtf(LOG_ID_MAIN, tag, msg, null, true, false); } Loading @@ -279,7 +281,7 @@ public final class Log { * @param tag Used to identify the source of a log message. * @param tr An exception to log. */ public static int wtf(String tag, Throwable tr) { public static int wtf(@Nullable String tag, @NonNull Throwable tr) { return wtf(LOG_ID_MAIN, tag, tr.getMessage(), tr, false, false); } Loading @@ -290,12 +292,12 @@ public final class Log { * @param msg The message you would like logged. * @param tr An exception to log. May be null. */ public static int wtf(String tag, String msg, Throwable tr) { public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) { return wtf(LOG_ID_MAIN, tag, msg, tr, false, false); } static int wtf(int logId, String tag, String msg, Throwable tr, boolean localStack, boolean system) { static int wtf(int logId, @Nullable String tag, @Nullable String msg, @Nullable Throwable tr, boolean localStack, boolean system) { TerribleFailure what = new TerribleFailure(msg, tr); // Only mark this as ERROR, do not use ASSERT since that should be // reserved for cases where the system is guaranteed to abort. Loading @@ -305,7 +307,7 @@ public final class Log { return bytes; } static void wtfQuiet(int logId, String tag, String msg, boolean system) { static void wtfQuiet(int logId, @Nullable String tag, @Nullable String msg, boolean system) { TerribleFailure what = new TerribleFailure(msg, null); sWtfHandler.onTerribleFailure(tag, what, system); } Loading @@ -317,7 +319,8 @@ public final class Log { * * @hide */ public static TerribleFailureHandler setWtfHandler(TerribleFailureHandler handler) { @NonNull public static TerribleFailureHandler setWtfHandler(@NonNull TerribleFailureHandler handler) { if (handler == null) { throw new NullPointerException("handler == null"); } Loading @@ -330,7 +333,8 @@ public final class Log { * Handy function to get a loggable stack trace from a Throwable * @param tr An exception to log */ public static String getStackTraceString(Throwable tr) { @NonNull public static String getStackTraceString(@Nullable Throwable tr) { if (tr == null) { return ""; } Loading Loading @@ -360,7 +364,7 @@ public final class Log { * @param msg The message you would like logged. * @return The number of bytes written. */ public static int println(int priority, String tag, String msg) { public static int println(int priority, @Nullable String tag, @NonNull String msg) { return println_native(LOG_ID_MAIN, priority, tag, msg); } Loading @@ -385,8 +389,8 @@ public final class Log { * chunks. This is to avoid truncation. * @hide */ public static int printlns(int bufID, int priority, String tag, String msg, Throwable tr) { public static int printlns(int bufID, int priority, @Nullable String tag, @NonNull String msg, @Nullable Throwable tr) { ImmediateLogWriter logWriter = new ImmediateLogWriter(bufID, priority, tag); // Acceptable buffer size. Get the native buffer size, subtract two zero terminators, // and the length of the tag. Loading
core/tests/coretests/src/android/util/LogNullabilityTest.java 0 → 100644 +170 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.util; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; import org.junit.Test; import org.junit.runner.RunWith; @SmallTest @RunWith(AndroidJUnit4.class) public final class LogNullabilityTest { @Test public void nullTag() { Log.v(null, ""); Log.d(null, ""); Log.i(null, ""); Log.w(null, ""); Log.e(null, ""); Log.wtf(null, ""); Log.wtfStack(null, ""); Log.println(Log.INFO, null, ""); // Implicit assertions of not crashing. } @Test public void nullTagWithThrowable() { Log.v(null, "", new Throwable()); Log.d(null, "", new Throwable()); Log.i(null, "", new Throwable()); Log.w(null, "", new Throwable()); Log.e(null, "", new Throwable()); Log.wtf(null, "", new Throwable()); Log.printlns(Log.LOG_ID_MAIN, Log.INFO, null, "", new Throwable()); // Implicit assertions of not crashing. } @Test public void nullMessage() { try { Log.v("", null); fail(); } catch (NullPointerException expected) { } try { Log.d("", null); fail(); } catch (NullPointerException expected) { } try { Log.i("", null); fail(); } catch (NullPointerException expected) { } try { // Explicit cast needed because there's a weird (String, Throwable) overload. Log.w("", (String) null); fail(); } catch (NullPointerException expected) { } try { Log.e("", null); fail(); } catch (NullPointerException expected) { } Log.wtf("", (String) null); Log.wtfStack("", (String) null); // Implicit assertion of not crashing. try { Log.println(Log.INFO, "", null); fail(); } catch (NullPointerException expected) { } } @Test public void nullMessageWithThrowable() { Log.v("", null, new Throwable()); Log.d("", null, new Throwable()); Log.i("", null, new Throwable()); Log.w("", null, new Throwable()); Log.e("", null, new Throwable()); Log.wtf("", null, new Throwable()); // Implicit assertions of not crashing. try { Log.printlns(Log.LOG_ID_MAIN, Log.INFO, "", null, new Throwable()); fail(); } catch (NullPointerException expected) { } } @Test public void nullThrowable() { Log.v("", "", null); Log.d("", "", null); Log.i("", "", null); Log.w("", "", null); Log.e("", "", null); Log.wtf("", "", null); // Warning has its own (String, Throwable) overload. Log.w("", (Throwable) null); Log.printlns(Log.LOG_ID_MAIN, Log.INFO, "", "", null); // Implicit assertions of not crashing. // WTF has its own (String, Throwable) overload with different behavior. try { Log.wtf("", (Throwable) null); fail(); } catch (NullPointerException expected) { } } @Test public void nullMessageWithNullThrowable() { Log.v("", null, null); Log.d("", null, null); Log.i("", null, null); Log.w("", null, null); Log.e("", null, null); Log.wtf("", null, null); // Implicit assertions of not crashing. try { Log.printlns(Log.LOG_ID_MAIN, Log.INFO, "", null, null); fail(); } catch (NullPointerException expected) { } } @Test public void nullTagIsLoggable() { Log.isLoggable(null, Log.INFO); // Implicit assertion of not crashing. } @Test public void nullGetStackTraceString() { assertNotNull(Log.getStackTraceString(null)); } }