Lazy load the allocation of Slog message formatting objects.

Test: manual verification looking at logcat output
Fixes: 182963900

Change-Id: If62ccfe085b20cda8755fefc3ec757ad59986685
This commit is contained in:
Felipe Leme
2021-03-19 12:37:39 -07:00
parent d3d61ffefe
commit 5395b2af57

View File

@@ -30,11 +30,11 @@ import java.util.Locale;
*/
public final class Slog {
@GuardedBy("sMessageBuilder")
private static final StringBuilder sMessageBuilder = new StringBuilder();
@GuardedBy("Slog.class")
private static StringBuilder sMessageBuilder;
@GuardedBy("sMessageBuilder")
private static final Formatter sFormatter = new Formatter(sMessageBuilder, Locale.ENGLISH);
@GuardedBy("Slog.class")
private static Formatter sFormatter;
private Slog() {
}
@@ -226,7 +226,12 @@ public final class Slog {
}
private static String getMessage(String format, @Nullable Object... args) {
synchronized (sMessageBuilder) {
synchronized (Slog.class) {
if (sMessageBuilder == null) {
// Lazy load so they're not created if not used by the process
sMessageBuilder = new StringBuilder();
sFormatter = new Formatter(sMessageBuilder, Locale.ENGLISH);
}
sFormatter.format(format, args);
String message = sMessageBuilder.toString();
sMessageBuilder.setLength(0);