Skip to content

Commit 2065642

Browse files
[dsrouter] cache Android SDK path (#5533)
This improves upon d7d8500 by caching the Android SDK path in a `static` field. I was testing `dsrouter` and noticed it logging duplicate messages: > dotnet-trace collect --dsrouter android For finer control over the dotnet-dsrouter options, run it separately and connect to it using -p WARNING: dotnet-dsrouter is a development tool not intended for production environments. How to connect current dotnet-dsrouter pid=45036 with android device and diagnostics tooling. Start an application on android device with ONE of the following environment variables set: [Default Tracing] DOTNET_DiagnosticPorts=127.0.0.1:9000,nosuspend,connect [Startup Tracing] DOTNET_DiagnosticPorts=127.0.0.1:9000,suspend,connect Run diagnotic tool connecting application on android device through dotnet-dsrouter pid=45036: dotnet-trace collect -p 45036 See https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dsrouter for additional details and examples. info: dotnet-dsrouter-45036[0] Starting dotnet-dsrouter using pid=45036 info: dotnet-dsrouter-45036[0] Looking for Android NDK... info: dotnet-dsrouter-45036[0] Looking for Android SDK... info: dotnet-dsrouter-45036[0] Looking for Android NDK... info: dotnet-dsrouter-45036[0] Looking for Android SDK... info: dotnet-dsrouter-45036[0] With the changes in d7d8500, it is running the code to "find" the Android SDK multiple times. The operation itself isn't very fast if called repeatedly, and it also clutters the logs with duplicate messages. With these changes, I only see the log message once now: info: dotnet-dsrouter-48616[0] Starting dotnet-dsrouter using pid=48616 info: dotnet-dsrouter-48616[0] Looking for Android NDK... info: dotnet-dsrouter-48616[0] Looking for Android SDK...
1 parent aa810aa commit 2065642

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/Tools/dotnet-dsrouter/ADBTcpRouterFactory.cs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,7 @@ public static void AdbRemovePortReverse(int localPort, int remotePort, bool owns
6666

6767
public static bool RunAdbCommandInternal(string command, string expectedOutput, int expectedExitCode, bool rethrow, ILogger logger)
6868
{
69-
void sdklogger(TraceLevel level, string message)
70-
{
71-
switch (level)
72-
{
73-
case TraceLevel.Error:
74-
logger?.LogError(message);
75-
break;
76-
case TraceLevel.Warning:
77-
logger?.LogWarning(message);
78-
break;
79-
case TraceLevel.Info:
80-
logger?.LogInformation(message);
81-
break;
82-
case TraceLevel.Verbose:
83-
logger?.LogDebug(message);
84-
break;
85-
}
86-
};
87-
88-
// AndroidSdkInfo checks $ANDROID_SDK_ROOT, $ANDROID_HOME, and default locations.
89-
string sdkRoot = new AndroidSdkInfo(logger: sdklogger).AndroidSdkPath;
69+
string sdkRoot = GetAndroidSdkPath(logger);
9070
string adbTool = "adb";
9171

9272
if (!string.IsNullOrEmpty(sdkRoot))
@@ -148,6 +128,38 @@ void sdklogger(TraceLevel level, string message)
148128

149129
return processStartedResult && expectedOutputResult && expectedExitCodeResult;
150130
}
131+
132+
private static string AndroidSdkPath;
133+
134+
private static string GetAndroidSdkPath(ILogger logger)
135+
{
136+
if (AndroidSdkPath is not null)
137+
{
138+
return AndroidSdkPath;
139+
}
140+
141+
void sdklogger(TraceLevel level, string message)
142+
{
143+
switch (level)
144+
{
145+
case TraceLevel.Error:
146+
logger?.LogError(message);
147+
break;
148+
case TraceLevel.Warning:
149+
logger?.LogWarning(message);
150+
break;
151+
case TraceLevel.Info:
152+
logger?.LogInformation(message);
153+
break;
154+
case TraceLevel.Verbose:
155+
logger?.LogDebug(message);
156+
break;
157+
}
158+
}
159+
160+
// AndroidSdkInfo checks $ANDROID_SDK_ROOT, $ANDROID_HOME, and default locations.
161+
return AndroidSdkPath = new AndroidSdkInfo(logger: sdklogger).AndroidSdkPath;
162+
}
151163
}
152164

153165
internal sealed class ADBTcpServerRouterFactory : TcpServerRouterFactory

0 commit comments

Comments
 (0)