diff options
author | 2025-03-03 20:34:23 +0000 | |
---|---|---|
committer | 2025-03-19 09:46:29 -0700 | |
commit | b8247e60208545a008dfd13aa81dbd5a39690085 (patch) | |
tree | 563f074fe88b3bf380bff86d64478f15c056a897 | |
parent | 0709b098951b60cfe1235b87be8eb60ba1f226fd (diff) |
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
6 files changed, 168 insertions, 6 deletions
diff --git a/core/java/com/android/internal/protolog/LogcatOnlyProtoLogImpl.java b/core/java/com/android/internal/protolog/LogcatOnlyProtoLogImpl.java index 34e04181388d..256a3ffd6456 100644 --- a/core/java/com/android/internal/protolog/LogcatOnlyProtoLogImpl.java +++ b/core/java/com/android/internal/protolog/LogcatOnlyProtoLogImpl.java @@ -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(); diff --git a/core/java/com/android/internal/protolog/ProtoLog.java b/core/java/com/android/internal/protolog/ProtoLog.java index c81af959f36c..915b981a7e7a 100644 --- a/core/java/com/android/internal/protolog/ProtoLog.java +++ b/core/java/com/android/internal/protolog/ProtoLog.java @@ -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(); diff --git a/core/java/com/android/internal/protolog/common/LogLevel.java b/core/java/com/android/internal/protolog/common/LogLevel.java index b5541ae81c2d..ea62a665c980 100644 --- a/core/java/com/android/internal/protolog/common/LogLevel.java +++ b/core/java/com/android/internal/protolog/common/LogLevel.java @@ -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), diff --git a/core/tests/coretests/src/com/android/internal/logging/ProtoLogTest.java b/core/tests/coretests/src/com/android/internal/logging/ProtoLogTest.java new file mode 100644 index 000000000000..90b6902629e6 --- /dev/null +++ b/core/tests/coretests/src/com/android/internal/logging/ProtoLogTest.java @@ -0,0 +1,53 @@ +/* + * 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); + +} diff --git a/core/tests/coretests/src/com/android/internal/logging/ProtoLogTestGroup.java b/core/tests/coretests/src/com/android/internal/logging/ProtoLogTestGroup.java new file mode 100644 index 000000000000..ecfaae5ea0c5 --- /dev/null +++ b/core/tests/coretests/src/com/android/internal/logging/ProtoLogTestGroup.java @@ -0,0 +1,85 @@ +/* + * 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; + } +} diff --git a/ravenwood/texts/ravenwood-annotation-allowed-classes.txt b/ravenwood/texts/ravenwood-annotation-allowed-classes.txt index 7462cc2f384a..c035688a8c84 100644 --- a/ravenwood/texts/ravenwood-annotation-allowed-classes.txt +++ b/ravenwood/texts/ravenwood-annotation-allowed-classes.txt @@ -323,6 +323,10 @@ com.android.internal.graphics.cam.Frame com.android.internal.graphics.cam.HctSolver com.android.internal.graphics.ColorUtils +com.android.internal.protolog.common.LogLevel +com.android.internal.protolog.LogcatOnlyProtoLogImpl +com.android.internal.protolog.ProtoLog + com.android.internal.util.BitUtils com.android.internal.util.BitwiseInputStream com.android.internal.util.BitwiseOutputStream |