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

Commit d5fb3067 authored by Pablo Gamito's avatar Pablo Gamito
Browse files

Remove registerGroups interface

Instead we will have a static init call that is used to register all protolog groups on initialization of ProtoLog in a new process.

Test: atest com.android.internal.protolog.ProtoLogServiceTest
Bug: 352538294
Flag: EXEMPT refactor
Change-Id: I733481640e89aa6394ea33e01436db01aa5824f9
parent ca85395e
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -419,7 +419,6 @@ public class LegacyProtoLogImpl implements IProtoLog {
        return group.isLogToLogcat() || (group.isLogToProto() && isProtoEnabled());
    }

    @Override
    public void registerGroups(IProtoLogGroup... protoLogGroups) {
        for (IProtoLogGroup group : protoLogGroups) {
            mLogGroups.put(group.name(), group);
+0 −5
Original line number Diff line number Diff line
@@ -79,9 +79,4 @@ public class LogcatOnlyProtoLogImpl implements IProtoLog {
    public boolean isEnabled(IProtoLogGroup group, LogLevel level) {
        return true;
    }

    @Override
    public void registerGroups(IProtoLogGroup... protoLogGroups) {
        // Does nothing
    }
}
+28 −17
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

/**
 * A service for the ProtoLog logging system.
@@ -125,17 +126,18 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
    private final Lock mBackgroundServiceLock = new ReentrantLock();
    private ExecutorService mBackgroundLoggingService = Executors.newSingleThreadExecutor();

    public PerfettoProtoLogImpl() {
        this(null, null, null, () -> {});
    public PerfettoProtoLogImpl(@NonNull IProtoLogGroup[] groups) {
        this(null, null, null, () -> {}, groups);
    }

    public PerfettoProtoLogImpl(@NonNull Runnable cacheUpdater) {
        this(null, null, null, cacheUpdater);
    public PerfettoProtoLogImpl(@NonNull Runnable cacheUpdater, @NonNull IProtoLogGroup[] groups) {
        this(null, null, null, cacheUpdater, groups);
    }

    public PerfettoProtoLogImpl(
            @NonNull String viewerConfigFilePath,
            @NonNull Runnable cacheUpdater) {
            @NonNull Runnable cacheUpdater,
            @NonNull IProtoLogGroup[] groups) {
        this(viewerConfigFilePath,
                null,
                new ProtoLogViewerConfigReader(() -> {
@@ -146,22 +148,24 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
                                "Failed to load viewer config file " + viewerConfigFilePath, e);
                    }
                }),
                cacheUpdater);
                cacheUpdater, groups);
    }

    @VisibleForTesting
    public PerfettoProtoLogImpl(
            @Nullable ViewerConfigInputStreamProvider viewerConfigInputStreamProvider,
            @Nullable ProtoLogViewerConfigReader viewerConfigReader,
            @NonNull Runnable cacheUpdater) {
        this(null, viewerConfigInputStreamProvider, viewerConfigReader, cacheUpdater);
            @NonNull Runnable cacheUpdater,
            @NonNull IProtoLogGroup[] groups) {
        this(null, viewerConfigInputStreamProvider, viewerConfigReader, cacheUpdater, groups);
    }

    private PerfettoProtoLogImpl(
            @Nullable String viewerConfigFilePath,
            @Nullable ViewerConfigInputStreamProvider viewerConfigInputStreamProvider,
            @Nullable ProtoLogViewerConfigReader viewerConfigReader,
            @NonNull Runnable cacheUpdater) {
            @NonNull Runnable cacheUpdater,
            @NonNull IProtoLogGroup[] groups) {
        if (viewerConfigFilePath != null && viewerConfigInputStreamProvider != null) {
            throw new RuntimeException("Only one of viewerConfigFilePath and "
                    + "viewerConfigInputStreamProvider can be set");
@@ -179,6 +183,8 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
        this.mViewerConfigReader = viewerConfigReader;
        this.mCacheUpdater = cacheUpdater;

        registerGroupsLocally(groups);

        if (android.tracing.Flags.clientSideProtoLogging()) {
            mProtoLogService =
                    IProtoLogService.Stub.asInterface(ServiceManager.getService(PROTOLOG_SERVICE));
@@ -192,6 +198,12 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
                    args.setViewerConfigFile(viewerConfigFilePath);
                }

                final var groupArgs = Stream.of(groups)
                        .map(group -> new ProtoLogService.RegisterClientArgs.GroupConfig(
                                group.name(), group.isLogToLogcat()))
                        .toArray(ProtoLogService.RegisterClientArgs.GroupConfig[]::new);
                args.setGroups(groupArgs);

                mProtoLogService.registerClient(this, args);
            } catch (RemoteException e) {
                throw new RuntimeException("Failed to register ProtoLog client");
@@ -294,19 +306,18 @@ public class PerfettoProtoLogImpl extends IProtoLogClient.Stub implements IProto
                || group.isLogToLogcat();
    }

    @Override
    public void registerGroups(IProtoLogGroup... protoLogGroups) {
    private void registerGroupsLocally(@NonNull IProtoLogGroup[] protoLogGroups) {
        final var groupsLoggingToLogcat = new ArrayList<String>();
        for (IProtoLogGroup protoLogGroup : protoLogGroups) {
            mLogGroups.put(protoLogGroup.name(), protoLogGroup);
        }

        final String[] groupsLoggingToLogcat = Arrays.stream(protoLogGroups)
                .filter(IProtoLogGroup::isLogToLogcat)
                .map(IProtoLogGroup::name)
                .toArray(String[]::new);
            if (protoLogGroup.isLogToLogcat()) {
                groupsLoggingToLogcat.add(protoLogGroup.name());
            }
        }

        if (mViewerConfigReader != null) {
            mViewerConfigReader.loadViewerConfig(groupsLoggingToLogcat);
            mViewerConfigReader.loadViewerConfig(groupsLoggingToLogcat.toArray(new String[0]));
        }
    }

+18 −18
Original line number Diff line number Diff line
@@ -49,6 +49,24 @@ public class ProtoLog {

    private static IProtoLog sProtoLogInstance;

    /**
     * Initialize ProtoLog in this process.
     * <p>
     * This method MUST be called before any protologging is performed in this process.
     * Ensure that all groups that will be used for protologging are registered.
     *
     * @param groups The ProtoLog groups that will be used in the process.
     */
    public static void init(IProtoLogGroup... groups) {
        if (android.tracing.Flags.perfettoProtologTracing()) {
            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.
            sProtoLogInstance = new LogcatOnlyProtoLogImpl();
        }
    }

    /**
     * DEBUG level log.
     *
@@ -150,14 +168,6 @@ public class ProtoLog {
        return sProtoLogInstance;
    }

    /**
     * Registers available protolog groups. A group must be registered before it can be used.
     * @param protoLogGroups The groups to register for use in protolog.
     */
    public static void registerGroups(IProtoLogGroup... protoLogGroups) {
        sProtoLogInstance.registerGroups(protoLogGroups);
    }

    private static void logStringMessage(LogLevel logLevel, IProtoLogGroup group,
            String stringMessage, Object... args) {
        if (sProtoLogInstance == null) {
@@ -169,14 +179,4 @@ public class ProtoLog {
            sProtoLogInstance.log(logLevel, group, stringMessage, args);
        }
    }

    static {
        if (android.tracing.Flags.perfettoProtologTracing()) {
            sProtoLogInstance = new PerfettoProtoLogImpl();
        } 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.
            sProtoLogInstance = new LogcatOnlyProtoLogImpl();
        }
    }
}
+9 −14
Original line number Diff line number Diff line
@@ -92,36 +92,31 @@ public class ProtoLogImpl {
        return getSingleInstance().isEnabled(group, level);
    }

    /**
     * Registers available protolog groups. A group must be registered before it can be used.
     * @param protoLogGroups The groups to register for use in protolog.
     */
    public static void registerGroups(IProtoLogGroup... protoLogGroups) {
        getSingleInstance().registerGroups(protoLogGroups);
    }

    /**
     * Returns the single instance of the ProtoLogImpl singleton class.
     */
    public static synchronized IProtoLog getSingleInstance() {
        if (sServiceInstance == null) {
            final var groups = sLogGroups.values().toArray(new IProtoLogGroup[0]);

            if (android.tracing.Flags.perfettoProtologTracing()) {
                File f = new File(sViewerConfigPath);
                if (!ProtoLog.REQUIRE_PROTOLOGTOOL && !f.exists()) {
                    // TODO(b/353530422): Remove - temporary fix to unblock b/352290057
                    // In so tests the viewer config file might not exist in which we don't
                    // In some tests the viewer config file might not exist in which we don't
                    // want to provide config path to the user
                    sServiceInstance = new PerfettoProtoLogImpl(sCacheUpdater);
                    sServiceInstance = new PerfettoProtoLogImpl(sCacheUpdater, groups);
                } else {
                    sServiceInstance = new PerfettoProtoLogImpl(sViewerConfigPath, sCacheUpdater);
                    sServiceInstance =
                            new PerfettoProtoLogImpl(sViewerConfigPath, sCacheUpdater, groups);
                }
            } else {
                sServiceInstance = new LegacyProtoLogImpl(
                var protologImpl = new LegacyProtoLogImpl(
                        sLegacyOutputFilePath, sLegacyViewerConfigPath, sCacheUpdater);
                protologImpl.registerGroups(groups);
                sServiceInstance = protologImpl;
            }

            IProtoLogGroup[] groups = sLogGroups.values().toArray(new IProtoLogGroup[0]);
            sServiceInstance.registerGroups(groups);
            sCacheUpdater.run();
        }
        return sServiceInstance;
Loading