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

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

Compatibalize ProtoLog with Ravenwood host side tests

Test: atest FrameworksCoreTestsRavenwood:com.android.internal.logging.ProtoLogTest
Flag: TEST_ONLY
Bug: 362981229
Bug: 396630823
Change-Id: I2ddb9f54c4f8f6fa7d58db3f921bc8afd8228609
parent 0709b098
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.internal.protolog;
import static com.android.internal.protolog.ProtoLog.REQUIRE_PROTOLOGTOOL;

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

@@ -31,14 +32,15 @@ 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
 * Class only created and used to serve 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
 * boolean is false. In which case we simply want to log protolog message to logcat. Note, that this
 * means that in such cases there is no real advantage of using protolog over logcat.
 *
 * @deprecated Should not be used. This is just a temporary class to support a legacy behavior.
 * NOTE: Should not be used in real products as this mostly removes the benefits of protolog. This
 * is just a temporary class to support a legacy behavior and tests running on the host-side.
 */
@Deprecated
@RavenwoodKeepWholeClass
public class LogcatOnlyProtoLogImpl implements IProtoLog {
    private static final String LOG_TAG = LogcatOnlyProtoLogImpl.class.getName();

+18 −3
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.internal.protolog;

import android.os.ServiceManager;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodReplace;
import android.tracing.perfetto.DataSourceParams;
import android.tracing.perfetto.InitArguments;
import android.tracing.perfetto.Producer;
@@ -47,6 +49,7 @@ import java.util.HashSet;
 * Methods in this class are stubs, that are replaced by optimised versions by the ProtoLogTool
 * during build.
 */
@RavenwoodKeepWholeClass
// LINT.IfChange
public class ProtoLog {
// LINT.ThenChange(frameworks/base/tools/protologtool/src/com/android/protolog/tool/ProtoLogTool.kt)
@@ -73,13 +76,25 @@ public class ProtoLog {
        // These tracing instances are only used when we cannot or do not preprocess the source
        // files to extract out the log strings. Otherwise, the trace calls are replaced with calls
        // directly to the generated tracing implementations.
        if (android.tracing.Flags.perfettoProtologTracing()) {
            initializePerfettoProtoLog(groups);
        } else {
        if (logOnlyToLogcat()) {
            sProtoLogInstance = new LogcatOnlyProtoLogImpl();
        } else {
            initializePerfettoProtoLog(groups);
        }
    }

    @RavenwoodReplace(reason = "Always use the Log backend on ravenwood, not Perfetto")
    private static boolean logOnlyToLogcat() {
        return !android.tracing.Flags.perfettoProtologTracing();
    }

    private static boolean logOnlyToLogcat$ravenwood() {
        // We don't want to initialize Perfetto data sources and have to deal with Perfetto
        // when running tests on the host side, instead just log everything to logcat which has
        // already been made compatible with ravenwood.
        return true;
    }

    private static void initializePerfettoProtoLog(IProtoLogGroup... groups) {
        var datasource = getSharedSingleInstanceDataSource();

+3 −0
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.internal.protolog.common;

import android.ravenwood.annotation.RavenwoodKeepWholeClass;

@RavenwoodKeepWholeClass
public enum LogLevel {
    DEBUG("d", 1),
    VERBOSE("v", 2),
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.logging;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.internal.protolog.ProtoLog;
import com.android.internal.protolog.common.IProtoLogGroup;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class ProtoLogTest {
    @Test
    public void canTrace() {
        ProtoLog.init(TEST_GROUP_1, TEST_GROUP_2);

        ProtoLog.v(TEST_GROUP_1, "Verbose message");
        ProtoLog.d(TEST_GROUP_1, "Debug message");
        ProtoLog.i(TEST_GROUP_1, "Info message");
        ProtoLog.w(TEST_GROUP_1, "Warning message");
        ProtoLog.e(TEST_GROUP_1, "Error message");
        ProtoLog.wtf(TEST_GROUP_1, "Wtf message");

        ProtoLog.v(TEST_GROUP_2, "Verbose message");
        ProtoLog.d(TEST_GROUP_2, "Debug message");
        ProtoLog.i(TEST_GROUP_2, "Info message");
        ProtoLog.w(TEST_GROUP_2, "Warning message");
        ProtoLog.e(TEST_GROUP_2, "Error message");
        ProtoLog.wtf(TEST_GROUP_2, "Wtf message");
    }

    private static final IProtoLogGroup TEST_GROUP_1 = new ProtoLogTestGroup("TEST_TAG_1", 1);
    private static final IProtoLogGroup TEST_GROUP_2 = new ProtoLogTestGroup("TEST_TAG_2", 2);

}
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.logging;

import com.android.internal.protolog.common.IProtoLogGroup;

class ProtoLogTestGroup implements IProtoLogGroup {
    private final boolean mEnabled;
    private volatile boolean mLogToProto;
    private volatile boolean mLogToLogcat;
    private final String mTag;
    private final int mId;

    ProtoLogTestGroup(String tag, int id) {
        this(true, true, false, tag, id);
    }

    ProtoLogTestGroup(
            boolean enabled, boolean logToProto, boolean logToLogcat, String tag, int id) {
        this.mEnabled = enabled;
        this.mLogToProto = logToProto;
        this.mLogToLogcat = logToLogcat;
        this.mTag = tag;
        this.mId = id;
    }

    @Override
    public String name() {
        return mTag;
    }

    @Override
    public boolean isEnabled() {
        return mEnabled;
    }

    @Override
    public boolean isLogToProto() {
        return mLogToProto;
    }

    @Override
    public boolean isLogToLogcat() {
        return mLogToLogcat;
    }

    @Override
    public boolean isLogToAny() {
        return mLogToLogcat || mLogToProto;
    }

    @Override
    public String getTag() {
        return mTag;
    }

    @Override
    public void setLogToProto(boolean logToProto) {
        this.mLogToProto = logToProto;
    }

    @Override
    public void setLogToLogcat(boolean logToLogcat) {
        this.mLogToLogcat = logToLogcat;
    }

    @Override
    public int getId() {
        return mId;
    }
}
Loading