1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.test.tracing.coroutines.util
18
19 import android.os.Trace
20 import android.util.Log
21 import org.robolectric.annotation.Implementation
22 import org.robolectric.annotation.Implements
23
24 @OptIn(ExperimentalStdlibApi::class)
25 @Suppress("unused_parameter")
26 @Implements(Trace::class)
27 object ShadowTrace {
28
29 @Implementation
30 @JvmStatic
isEnablednull31 fun isEnabled(): Boolean {
32 return FakeTraceState.isTracingEnabled
33 }
34
35 @Implementation
36 @JvmStatic
traceBeginnull37 fun traceBegin(traceTag: Long, methodName: String) {
38 debug { "traceBegin: name=$methodName" }
39 FakeTraceState.begin(methodName)
40 }
41
42 @Implementation
43 @JvmStatic
traceEndnull44 fun traceEnd(traceTag: Long) {
45 debug { "traceEnd" }
46 FakeTraceState.end()
47 }
48
49 @Implementation
50 @JvmStatic
asyncTraceBeginnull51 fun asyncTraceBegin(traceTag: Long, methodName: String, cookie: Int) {
52 debug { "asyncTraceBegin: name=$methodName cookie=${cookie.toHexString()}" }
53 }
54
55 @Implementation
56 @JvmStatic
asyncTraceEndnull57 fun asyncTraceEnd(traceTag: Long, methodName: String, cookie: Int) {
58 debug { "asyncTraceEnd: name=$methodName cookie=${cookie.toHexString()}" }
59 }
60
61 @Implementation
62 @JvmStatic
asyncTraceForTrackBeginnull63 fun asyncTraceForTrackBegin(
64 traceTag: Long,
65 trackName: String,
66 methodName: String,
67 cookie: Int,
68 ) {
69 debug {
70 "asyncTraceForTrackBegin: track=$trackName name=$methodName cookie=${cookie.toHexString()}"
71 }
72 }
73
74 @Implementation
75 @JvmStatic
asyncTraceForTrackEndnull76 fun asyncTraceForTrackEnd(traceTag: Long, trackName: String, methodName: String, cookie: Int) {
77 debug {
78 "asyncTraceForTrackEnd: track=$trackName name=$methodName cookie=${cookie.toHexString()}"
79 }
80 }
81
82 @Implementation
83 @JvmStatic
instantnull84 fun instant(traceTag: Long, eventName: String) {
85 debug { "instant: name=$eventName" }
86 }
87
88 @Implementation
89 @JvmStatic
instantForTracknull90 fun instantForTrack(traceTag: Long, trackName: String, eventName: String) {
91 debug { "instantForTrack: track=$trackName name=$eventName" }
92 }
93 }
94
95 private const val DEBUG = false
96
97 /** Log a message with a tag indicating the current thread ID */
debugnull98 private fun debug(message: () -> String) {
99 if (DEBUG) Log.d("ShadowTrace", "Thread #${currentThreadId()}: $message")
100 }
101