Skip to content

Commit ef94f7f

Browse files
authored
[apple] [infra] Refactor unification of app and main logs for iOS devices and MacCatalyst (#1383)
- Move the logic of unifying the app and main logs to the TestOrchestrator. - Copy system and application logs to the main log after the app has finished.
1 parent 6a680e9 commit ef94f7f

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/Microsoft.DotNet.XHarness.Apple/AppOperations/AppRunnerBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,8 @@ private CancellationTokenSource CaptureLogStream(string appName, bool useSimctl,
327327
}
328328
else
329329
{
330-
// For MacCatalyst, the test output log does not propagate to the main log.
331-
// Hence, we duplicate the log to the main console log to simplify the UX of failure investigation.
332-
IFileBackedLog aggregatedAppOutputLog = Log.CreateReadableAggregatedLog(_mainLog, log);
333330
_processManager
334-
.ExecuteCommandAsync("log", logArgs, _mainLog, aggregatedAppOutputLog, aggregatedAppOutputLog, TimeSpan.FromDays(1), cancellationToken: streamCancellation)
331+
.ExecuteCommandAsync("log", logArgs, _mainLog, log, log, TimeSpan.FromDays(1), cancellationToken: streamCancellation)
335332
.DoNotAwait();
336333
}
337334

src/Microsoft.DotNet.XHarness.Apple/AppOperations/AppTester.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,12 @@ private async Task RunDeviceTests(
362362
// We need to check for MT1111 (which means that mlaunch won't wait for the app to exit)
363363
IFileBackedLog aggregatedLog = Log.CreateReadableAggregatedLog(_mainLog, testReporter.CallbackLog);
364364

365-
// The app output log is not directly accessible.
366-
// Hence, we duplicate the log to the main console log to simplify the UX of failure investigation.
367-
IFileBackedLog aggregatedAppOutputLog = Log.CreateReadableAggregatedLog(_mainLog, appOutputLog);
368365

369366
var result = await RunAndWatchForAppSignal(() => _processManager.ExecuteCommandAsync(
370367
mlaunchArguments,
371368
aggregatedLog,
372-
aggregatedAppOutputLog,
373-
aggregatedAppOutputLog,
369+
appOutputLog,
370+
appOutputLog,
374371
timeout,
375372
envVars,
376373
cancellationToken: cancellationToken));

src/Microsoft.DotNet.XHarness.Apple/Orchestration/TestOrchestrator.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,15 @@ private async Task<ExitCode> ExecuteApp(
248248
skippedTestClasses: classMethodFilters?.ToArray(),
249249
cancellationToken: cancellationToken);
250250

251-
return ParseResult(testResult, resultMessage, appTester.ListenerConnected);
251+
ExitCode exitCode = ParseResult(testResult, resultMessage, appTester.ListenerConnected);
252+
253+
if (!target.Platform.IsSimulator()) // Simulator app logs are already included in the main log
254+
{
255+
// Copy system and application logs to the main log for better failure investigation.
256+
CopyLogsToMainLog();
257+
}
258+
259+
return exitCode;
252260
}
253261

254262
private async Task<ExitCode> ExecuteMacCatalystApp(
@@ -278,7 +286,12 @@ private async Task<ExitCode> ExecuteMacCatalystApp(
278286
skippedTestClasses: classMethodFilters?.ToArray(),
279287
cancellationToken: cancellationToken);
280288

281-
return ParseResult(testResult, resultMessage, appTester.ListenerConnected);
289+
ExitCode exitCode = ParseResult(testResult, resultMessage, appTester.ListenerConnected);
290+
291+
// Copy system and application logs to the main log for better failure investigation.
292+
CopyLogsToMainLog();
293+
294+
return exitCode;
282295
}
283296

284297
private IAppTester GetAppTester(CommunicationChannel communicationChannel, bool isSimulator)
@@ -364,4 +377,38 @@ ExitCode LogProblem(string message, ExitCode defaultExitCode)
364377
return ExitCode.GENERAL_FAILURE;
365378
}
366379
}
380+
381+
/// <summary>
382+
/// Copy system and application logs to the main log for better failure investigation.
383+
/// </summary>
384+
private void CopyLogsToMainLog()
385+
{
386+
var logs = _logs.Where(log => log.Description == LogType.SystemLog.ToString() || log.Description == LogType.ApplicationLog.ToString()).ToList();
387+
388+
foreach (var log in logs)
389+
{
390+
_mainLog.WriteLine($"==================== {log.Description} ====================");
391+
_mainLog.WriteLine($"Log file: {log.FullPath}");
392+
393+
try
394+
{
395+
// Read and append log content to the main log
396+
using var reader = log.GetReader();
397+
while (!reader.EndOfStream)
398+
{
399+
var logContent = reader.ReadLine();
400+
if (logContent is null)
401+
continue;
402+
_mainLog.WriteLine(logContent);
403+
}
404+
}
405+
catch (Exception ex)
406+
{
407+
_mainLog.WriteLine($"Failed to read {log.Description}: {ex.Message}");
408+
}
409+
410+
_mainLog.WriteLine($"==================== End of {log.Description} ====================");
411+
_mainLog.WriteLine(string.Empty);
412+
}
413+
}
367414
}

0 commit comments

Comments
 (0)