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

Commit 7840c6d7 authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Allow ProtoLog.init to be called multiple times in the same process

This can happen in cases where different modules run in the same process and they each initialize ProtoLog with their own parameters when those modules are started.

Change-Id: I3024ee24d14bcb5148df21960a80fe8c1d24d68f
Flag: EXEMPT small bug fix
Bug: 352538294
Test: atest InternalTests:com.android.internal.protolog.ProtoLogTest
parent 7f292d96
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import static com.android.internal.protolog.ProtoLogMessage.MESSAGE_HASH;
import static com.android.internal.protolog.ProtoLogMessage.SINT64_PARAMS;
import static com.android.internal.protolog.ProtoLogMessage.STR_PARAMS;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.ShellCommand;
import android.os.SystemClock;
@@ -49,6 +50,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
@@ -419,6 +421,12 @@ public class LegacyProtoLogImpl implements IProtoLog {
        return group.isLogToLogcat() || (group.isLogToProto() && isProtoEnabled());
    }

    @Override
    @NonNull
    public List<IProtoLogGroup> getRegisteredGroups() {
        return mLogGroups.values().stream().toList();
    }

    public void registerGroups(IProtoLogGroup... protoLogGroups) {
        for (IProtoLogGroup group : protoLogGroups) {
            mLogGroups.put(group.name(), group);
+10 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.internal.protolog;

import static com.android.internal.protolog.ProtoLog.REQUIRE_PROTOLOGTOOL;

import android.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

@@ -26,6 +27,9 @@ import com.android.internal.protolog.common.IProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;
import com.android.internal.protolog.common.LogLevel;

import java.util.Collections;
import java.util.List;

/**
 * Class only create and used to server temporarily for when there is source code pre-processing by
 * the ProtoLog tool, when the tracing to Perfetto flag is off, and the static REQUIRE_PROTOLOGTOOL
@@ -79,4 +83,10 @@ public class LogcatOnlyProtoLogImpl implements IProtoLog {
    public boolean isEnabled(IProtoLogGroup group, LogLevel level) {
        return true;
    }

    @Override
    @NonNull
    public List<IProtoLogGroup> getRegisteredGroups() {
        return Collections.emptyList();
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -306,6 +306,12 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
                || group.isLogToLogcat();
    }

    @Override
    @NonNull
    public List<IProtoLogGroup> getRegisteredGroups() {
        return mLogGroups.values().stream().toList();
    }

    private void registerGroupsLocally(@NonNull IProtoLogGroup[] protoLogGroups) {
        final var groupsLoggingToLogcat = new ArrayList<String>();
        for (IProtoLogGroup protoLogGroup : protoLogGroups) {
+16 −1
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ import com.android.internal.protolog.common.IProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;
import com.android.internal.protolog.common.LogLevel;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * ProtoLog API - exposes static logging methods. Usage of this API is similar
 * to {@code android.utils.Log} class. Instead of plain text log messages each call consists of
@@ -49,6 +52,8 @@ public class ProtoLog {

    private static IProtoLog sProtoLogInstance;

    private static final Object sInitLock = new Object();

    /**
     * Initialize ProtoLog in this process.
     * <p>
@@ -59,7 +64,17 @@ public class ProtoLog {
     */
    public static void init(IProtoLogGroup... groups) {
        if (android.tracing.Flags.perfettoProtologTracing()) {
            synchronized (sInitLock) {
                if (sProtoLogInstance != null) {
                    // The ProtoLog instance has already been initialized in this process
                    final var alreadyRegisteredGroups = sProtoLogInstance.getRegisteredGroups();
                    final var allGroups = new ArrayList<>(alreadyRegisteredGroups);
                    allGroups.addAll(Arrays.stream(groups).toList());
                    groups = allGroups.toArray(new IProtoLogGroup[0]);
                }

                sProtoLogInstance = new PerfettoProtoLogImpl(groups);
            }
        } else {
            // The first call to ProtoLog is likely to flip REQUIRE_PROTOLOGTOOL, which is when this
            // static block will be executed before REQUIRE_PROTOLOGTOOL is actually set.
+7 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.internal.protolog.common;

import java.util.List;

/**
 * Interface for ProtoLog implementations.
 */
@@ -68,4 +70,9 @@ public interface IProtoLog {
     * @return If we need to log this group and level to either ProtoLog or Logcat.
     */
    boolean isEnabled(IProtoLogGroup group, LogLevel level);

    /**
     * @return an immutable list of the registered ProtoLog groups in this ProtoLog instance.
     */
    List<IProtoLogGroup> getRegisteredGroups();
}
Loading