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

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

Modify command to toggle all ProtoLog groups

Now the `cmd protolog logcat (enable|disable)` can be used without any group, to enable/disable all registered protolog groups, or with a list of space separated groups to enable/disable only those groups.

Test: atest TracingTests
Flag: EXEMPT debug command handling code
Bug: 429119886
Change-Id: Ia4e81bc4f3e4169a51f1aef15b25aa645f912f57
parent e7722f3f
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -83,8 +83,10 @@ public class ProtoLogCommandHandler extends ShellCommand {
        pw.println("    list - lists all ProtoLog groups registered with ProtoLog service");
        pw.println("    status <group> - print the status of a ProtoLog group");
        pw.println();
        pw.println("  logcat (enable | disable) <group>");
        pw.println("    enable or disable ProtoLog to logcat");
        pw.println("  logcat (enable | disable) <group>?");
        pw.println("    Enable or disable ProtoLog to logcat. Passing no groups to the command "
                + "will enable/disable all groups. Passing a group or space separated list of "
                + "groups to the command will enable/disable those groups only.");
        pw.println();
    }

@@ -150,7 +152,7 @@ public class ProtoLogCommandHandler extends ShellCommand {
    private int handleLogcatCommands(@Nullable String cmd) {
        PrintWriter pw = getOutPrintWriter();

        if (cmd == null || peekNextArg() == null) {
        if (cmd == null) {
            pw.println("Incomplete command. Use 'cmd protolog help' for guidance.");
            return 0;
        }
+10 −3
Original line number Diff line number Diff line
@@ -375,10 +375,17 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
    ) {
        // For each client, if its groups intersect the given list, send the command to toggle.
        synchronized (mConfigLock) {
            final String[] groupsToToggle;
            if (groups.length == 0) {
                groupsToToggle = mRegisteredGroups.toArray(new String[0]);
            } else {
                groupsToToggle = groups;
            }

            for (var clientRecord : mClientRecords.values()) {
                final ArraySet<String> affectedGroups;
                affectedGroups = new ArraySet<>(clientRecord.groups);
                affectedGroups.retainAll(Arrays.asList(groups));
                affectedGroups.retainAll(Arrays.asList(groupsToToggle));

                if (!affectedGroups.isEmpty()) {
                    final var clientGroups = affectedGroups.toArray(new String[0]);
@@ -397,7 +404,7 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
            }

            // Groups that actually have no clients associated indicate some kind of a bug.
            Set<String> noOpGroups = new ArraySet<>(groups);
            Set<String> noOpGroups = new ArraySet<>(Arrays.asList(groupsToToggle));
            mClientRecords.forEach((k, r) -> noOpGroups.removeAll(r.groups));

            // Send out a warning in logcat and the PrintWriter for unrecognized groups.
@@ -409,7 +416,7 @@ public class ProtoLogConfigurationServiceImpl extends IProtoLogConfigurationServ
            }

            // Flip the status of the groups in our record-keeping.
            for (String group : groups) {
            for (String group : groupsToToggle) {
                mLogGroupToLogcatStatus.put(group, enabled);
            }
        }
+15 −7
Original line number Diff line number Diff line
@@ -159,13 +159,13 @@ public class ProtoLogCommandHandlerTest {
        cmdHandler.exec(mMockBinder, FileDescriptor.in, FileDescriptor.out,
                FileDescriptor.err, new String[] { "logcat", "enable", "MY_GROUP" });
        Mockito.verify(mProtoLogConfigurationService)
                .enableProtoLogToLogcat(Mockito.any(), eq("MY_GROUP"));
                .enableProtoLogToLogcat(Mockito.any(PrintWriter.class), eq("MY_GROUP"));

        cmdHandler.exec(mMockBinder, FileDescriptor.in, FileDescriptor.out,
                FileDescriptor.err,
                new String[] { "logcat", "enable", "MY_GROUP", "MY_OTHER_GROUP" });
        Mockito.verify(mProtoLogConfigurationService)
                .enableProtoLogToLogcat(Mockito.any(),
                .enableProtoLogToLogcat(Mockito.any(PrintWriter.class),
                        eq("MY_GROUP"), eq("MY_OTHER_GROUP"));
    }

@@ -177,34 +177,42 @@ public class ProtoLogCommandHandlerTest {
        cmdHandler.exec(mMockBinder, FileDescriptor.in, FileDescriptor.out,
                FileDescriptor.err, new String[] { "logcat", "disable", "MY_GROUP" });
        Mockito.verify(mProtoLogConfigurationService)
                .disableProtoLogToLogcat(Mockito.any(), eq("MY_GROUP"));
                .disableProtoLogToLogcat(Mockito.any(PrintWriter.class), eq("MY_GROUP"));

        cmdHandler.exec(mMockBinder, FileDescriptor.in, FileDescriptor.out,
                FileDescriptor.err,
                new String[] { "logcat", "disable", "MY_GROUP", "MY_OTHER_GROUP" });
        Mockito.verify(mProtoLogConfigurationService)
                .disableProtoLogToLogcat(Mockito.any(),
                .disableProtoLogToLogcat(Mockito.any(PrintWriter.class),
                        eq("MY_GROUP"), eq("MY_OTHER_GROUP"));
    }

    @Test
    public void handlesLogcatEnableCommandWithNoGroups() {
        Mockito.when(mProtoLogConfigurationService.getGroups())
                .thenReturn(new String[]{"MY_GROUP", "MY_OTHER_GROUP"});
        final ProtoLogCommandHandler cmdHandler =
                new ProtoLogCommandHandler(mProtoLogConfigurationService, mPrintWriter);

        cmdHandler.exec(mMockBinder, FileDescriptor.in, FileDescriptor.out,
                FileDescriptor.err, new String[] { "logcat", "enable" });
        Mockito.verify(mPrintWriter).println(contains("Incomplete command"));
        Mockito.verify(mProtoLogConfigurationService)
                .enableProtoLogToLogcat(Mockito.any(PrintWriter.class),
                        eq("MY_GROUP"), eq("MY_OTHER_GROUP"));
    }

    @Test
    public void handlesLogcatDisableCommandWithNoGroups() {
        Mockito.when(mProtoLogConfigurationService.getGroups())
                .thenReturn(new String[]{"MY_GROUP", "MY_OTHER_GROUP"});
        final ProtoLogCommandHandler cmdHandler =
                new ProtoLogCommandHandler(mProtoLogConfigurationService, mPrintWriter);

        cmdHandler.exec(mMockBinder, FileDescriptor.in, FileDescriptor.out,
                FileDescriptor.err, new String[] { "logcat", "disable" });
        Mockito.verify(mPrintWriter).println(contains("Incomplete command"));
        Mockito.verify(mProtoLogConfigurationService)
                .disableProtoLogToLogcat(Mockito.any(PrintWriter.class),
                        eq("MY_GROUP"), eq("MY_OTHER_GROUP"));
    }

    private void validateOnHelpPrinted() {
@@ -212,7 +220,7 @@ public class ProtoLogCommandHandlerTest {
        Mockito.verify(mPrintWriter, times(1))
                .println(endsWith("groups (list | status)"));
        Mockito.verify(mPrintWriter, times(1))
                .println(endsWith("logcat (enable | disable) <group>"));
                .println(endsWith("logcat (enable | disable) <group>?"));
        Mockito.verify(mPrintWriter, atLeast(0)).println(anyString());
    }
}
+58 −0
Original line number Diff line number Diff line
@@ -263,6 +263,64 @@ public class ProtoLogConfigurationServiceTest {
        Mockito.verify(mMockClient, never()).toggleLogcat(anyBoolean(), any());
    }

    @Test
    public void sendEnableLoggingToLogcatToAllClientsWhenNoGroupIsProvided()
            throws RemoteException {
        final var service = new ProtoLogConfigurationServiceImpl();

        final RegisterClientArgs args = new RegisterClientArgs();
        args.groups = new String[] { TEST_GROUP };
        args.groupsDefaultLogcatStatus = new boolean[] { false };
        service.registerClient(mMockClient, args);

        final RegisterClientArgs secondClientArgs = new RegisterClientArgs();
        secondClientArgs.groups = new String[] { OTHER_TEST_GROUP };
        secondClientArgs.groupsDefaultLogcatStatus = new boolean[] { false };
        service.registerClient(mSecondMockClient, secondClientArgs);

        Truth.assertThat(service.isLoggingToLogcat(TEST_GROUP)).isFalse();
        Truth.assertThat(service.isLoggingToLogcat(OTHER_TEST_GROUP)).isFalse();

        service.enableProtoLogToLogcat(Mockito.mock(PrintWriter.class));

        Truth.assertThat(service.isLoggingToLogcat(TEST_GROUP)).isTrue();
        Truth.assertThat(service.isLoggingToLogcat(OTHER_TEST_GROUP)).isTrue();

        Mockito.verify(mMockClient).toggleLogcat(eq(true),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(TEST_GROUP)));
        Mockito.verify(mSecondMockClient).toggleLogcat(eq(true),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(OTHER_TEST_GROUP)));
    }

    @Test
    public void sendDisableLoggingToLogcatToAllClientsWhenNoGroupIsProvided()
            throws RemoteException {
        final ProtoLogConfigurationService service = new ProtoLogConfigurationServiceImpl();

        final RegisterClientArgs args = new RegisterClientArgs();
        args.groups = new String[] { TEST_GROUP };
        args.groupsDefaultLogcatStatus = new boolean[] { true };
        service.registerClient(mMockClient, args);

        final RegisterClientArgs secondClientArgs = new RegisterClientArgs();
        secondClientArgs.groups = new String[] { OTHER_TEST_GROUP };
        secondClientArgs.groupsDefaultLogcatStatus = new boolean[] { true };
        service.registerClient(mSecondMockClient, secondClientArgs);

        Truth.assertThat(service.isLoggingToLogcat(TEST_GROUP)).isTrue();
        Truth.assertThat(service.isLoggingToLogcat(OTHER_TEST_GROUP)).isTrue();

        service.disableProtoLogToLogcat(Mockito.mock(PrintWriter.class));

        Truth.assertThat(service.isLoggingToLogcat(TEST_GROUP)).isFalse();
        Truth.assertThat(service.isLoggingToLogcat(OTHER_TEST_GROUP)).isFalse();

        Mockito.verify(mMockClient).toggleLogcat(eq(false),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(TEST_GROUP)));
        Mockito.verify(mSecondMockClient).toggleLogcat(eq(false),
                Mockito.argThat(it -> it.length == 1 && it[0].equals(OTHER_TEST_GROUP)));
    }

    @Test
    public void handlesToggleToLogcatBeforeClientIsRegistered() throws RemoteException {
        final ProtoLogConfigurationService service = new ProtoLogConfigurationServiceImpl();