diff --git a/core/java/android/util/Slog.java b/core/java/android/util/Slog.java index 3880131324fcf..f61ab29851635 100644 --- a/core/java/android/util/Slog.java +++ b/core/java/android/util/Slog.java @@ -16,14 +16,26 @@ package android.util; +import android.annotation.Nullable; import android.compat.annotation.UnsupportedAppUsage; import android.os.Build; +import com.android.internal.annotations.GuardedBy; + +import java.util.Formatter; +import java.util.Locale; + /** * @hide */ public final class Slog { + @GuardedBy("sMessageBuilder") + private static final StringBuilder sMessageBuilder = new StringBuilder(); + + @GuardedBy("sMessageBuilder") + private static final Formatter sFormatter = new Formatter(sMessageBuilder, Locale.ENGLISH); + private Slog() { } @@ -37,6 +49,15 @@ public final class Slog { msg + '\n' + Log.getStackTraceString(tr)); } + /** + * Logs a {@link Log.VERBOSE} message. + */ + public static void v(String tag, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.VERBOSE)) return; + + v(tag, getMessage(format, args)); + } + @UnsupportedAppUsage public static int d(String tag, String msg) { return Log.println_native(Log.LOG_ID_SYSTEM, Log.DEBUG, tag, msg); @@ -48,6 +69,15 @@ public final class Slog { msg + '\n' + Log.getStackTraceString(tr)); } + /** + * Logs a {@link Log.DEBUG} message. + */ + public static void d(String tag, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.DEBUG)) return; + + d(tag, getMessage(format, args)); + } + @UnsupportedAppUsage public static int i(String tag, String msg) { return Log.println_native(Log.LOG_ID_SYSTEM, Log.INFO, tag, msg); @@ -58,6 +88,15 @@ public final class Slog { msg + '\n' + Log.getStackTraceString(tr)); } + /** + * Logs a {@link Log.INFO} message. + */ + public static void i(String tag, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.INFO)) return; + + i(tag, getMessage(format, args)); + } + @UnsupportedAppUsage public static int w(String tag, String msg) { return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, msg); @@ -73,6 +112,24 @@ public final class Slog { return Log.println_native(Log.LOG_ID_SYSTEM, Log.WARN, tag, Log.getStackTraceString(tr)); } + /** + * Logs a {@link Log.WARN} message. + */ + public static void w(String tag, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.WARN)) return; + + w(tag, getMessage(format, args)); + } + + /** + * Logs a {@link Log.WARN} message with an exception + */ + public static void w(String tag, Exception exception, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.WARN)) return; + + w(tag, getMessage(format, args), exception); + } + @UnsupportedAppUsage public static int e(String tag, String msg) { return Log.println_native(Log.LOG_ID_SYSTEM, Log.ERROR, tag, msg); @@ -84,6 +141,24 @@ public final class Slog { msg + '\n' + Log.getStackTraceString(tr)); } + /** + * Logs a {@link Log.ERROR} message. + */ + public static void e(String tag, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.ERROR)) return; + + e(tag, getMessage(format, args)); + } + + /** + * Logs a {@link Log.ERROR} message with an exception + */ + public static void e(String tag, Exception exception, String format, @Nullable Object... args) { + if (!Log.isLoggable(tag, Log.ERROR)) return; + + e(tag, getMessage(format, args), exception); + } + /** * Like {@link Log#wtf(String, String)}, but will never cause the caller to crash, and * will always be handled asynchronously. Primarily for use by coding running within @@ -94,6 +169,21 @@ public final class Slog { return Log.wtf(Log.LOG_ID_SYSTEM, tag, msg, null, false, true); } + /** + * Logs a {@code wtf} message. + */ + public static void wtf(String tag, String format, @Nullable Object... args) { + wtf(tag, getMessage(format, args)); + } + + /** + * Logs a {@code wtf} message with an exception. + */ + public static void wtf(String tag, Exception exception, String format, + @Nullable Object... args) { + wtf(tag, getMessage(format, args), exception); + } + /** * Like {@link #wtf(String, String)}, but does not output anything to the log. */ @@ -134,5 +224,13 @@ public final class Slog { public static int println(int priority, String tag, String msg) { return Log.println_native(Log.LOG_ID_SYSTEM, priority, tag, msg); } -} + private static String getMessage(String format, @Nullable Object... args) { + synchronized (sMessageBuilder) { + sFormatter.format(format, args); + String message = sMessageBuilder.toString(); + sMessageBuilder.setLength(0); + return message; + } + } +}