Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d8f339d0 authored by Jake Wharton's avatar Jake Wharton Committed by Android (Google) Code Review
Browse files

Merge "Annotate Log reference parameters and return types."

parents 10cb58dc d4a23897
Loading
Loading
Loading
Loading
+29 −25
Original line number Diff line number Diff line
@@ -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;
@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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);
    }

@@ -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.
@@ -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);
    }
@@ -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");
        }
@@ -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 "";
        }
@@ -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);
    }

@@ -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.
+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));
    }
}