diff --git a/src/tasks/AndroidAppBuilder/ApkBuilder.cs b/src/tasks/AndroidAppBuilder/ApkBuilder.cs index 9ed098ac55a61e..40a5c85b159f98 100644 --- a/src/tasks/AndroidAppBuilder/ApkBuilder.cs +++ b/src/tasks/AndroidAppBuilder/ApkBuilder.cs @@ -504,7 +504,7 @@ private void SignApk(string apkPath, string apksigner) { Utils.RunProcess(logger, "keytool", "-genkey -v -keystore debug.keystore -storepass android -alias " + "androiddebugkey -keypass android -keyalg RSA -keysize 2048 -noprompt " + - "-dname \"CN=Android Debug,O=Android,C=US\"", workingDir: OutputDir, silent: true); + "-dname \"CN=Android Debug,O=Android,C=US\"", workingDir: OutputDir, messageImportance: MessageImportance.Low); } else if (Path.GetFullPath(signingKey) != Path.GetFullPath(defaultKey)) { diff --git a/src/tasks/AotCompilerTask/MonoAOTCompiler.cs b/src/tasks/AotCompilerTask/MonoAOTCompiler.cs index b211eaedc0a649..913c6eac157c3f 100644 --- a/src/tasks/AotCompilerTask/MonoAOTCompiler.cs +++ b/src/tasks/AotCompilerTask/MonoAOTCompiler.cs @@ -926,8 +926,7 @@ private bool PrecompileLibrary(PrecompileArguments args) $"--response=\"{args.ResponseFilePath}\"", args.EnvironmentVariables, args.WorkingDir, - silent: true, - debugMessageImportance: MessageImportance.Low, + messageImportance: MessageImportance.Low, label: Path.GetFileName(assembly)); var importance = exitCode == 0 ? MessageImportance.Low : MessageImportance.High; diff --git a/src/tasks/Common/Utils.cs b/src/tasks/Common/Utils.cs index 2c624214937d66..cb36bd6cef7c98 100644 --- a/src/tasks/Common/Utils.cs +++ b/src/tasks/Common/Utils.cs @@ -28,9 +28,8 @@ public static (int exitCode, string output) RunShellCommand( string command, IDictionary envVars, string workingDir, - bool silent=false, - bool logStdErrAsMessage=false, - MessageImportance debugMessageImportance=MessageImportance.Low, + bool logErrorsAndWarningsFromOutput=false, + MessageImportance messageImportance=MessageImportance.Low, string? label=null) { string scriptFileName = CreateTemporaryBatchFile(command); @@ -39,18 +38,17 @@ public static (int exitCode, string output) RunShellCommand( : ("/bin/sh", $"\"{scriptFileName}\""); string msgPrefix = label == null ? string.Empty : $"[{label}] "; - logger.LogMessage(debugMessageImportance, $"{msgPrefix}Running {command} via script {scriptFileName}:", msgPrefix); - logger.LogMessage(debugMessageImportance, File.ReadAllText(scriptFileName), msgPrefix); + logger.LogMessage(messageImportance, $"{msgPrefix}Running {command} via script {scriptFileName}:", msgPrefix); + logger.LogMessage(messageImportance, File.ReadAllText(scriptFileName), msgPrefix); return TryRunProcess(logger, shell, args, envVars, workingDir, - silent: silent, - logStdErrAsMessage: logStdErrAsMessage, + logErrorsAndWarningsFromOutput: logErrorsAndWarningsFromOutput, label: label, - debugMessageImportance: debugMessageImportance); + messageImportance: messageImportance); static string CreateTemporaryBatchFile(string command) { @@ -82,8 +80,7 @@ public static string RunProcess( IDictionary? envVars = null, string? workingDir = null, bool ignoreErrors = false, - bool silent = true, - MessageImportance debugMessageImportance=MessageImportance.High) + MessageImportance messageImportance=MessageImportance.High) { (int exitCode, string output) = TryRunProcess( logger, @@ -91,8 +88,7 @@ public static string RunProcess( args, envVars, workingDir, - silent: silent, - debugMessageImportance: debugMessageImportance); + messageImportance: messageImportance); if (exitCode != 0 && !ignoreErrors) throw new Exception("Error: Process returned non-zero exit code: " + output); @@ -106,13 +102,12 @@ public static (int, string) TryRunProcess( string args = "", IDictionary? envVars = null, string? workingDir = null, - bool silent = true, - bool logStdErrAsMessage = false, - MessageImportance debugMessageImportance=MessageImportance.High, + bool logErrorsAndWarningsFromOutput = false, + MessageImportance messageImportance = MessageImportance.High, string? label=null) { string msgPrefix = label == null ? string.Empty : $"[{label}] "; - logger.LogMessage(debugMessageImportance, $"{msgPrefix}Running: {path} {args}"); + logger.LogMessage(messageImportance, $"{msgPrefix}Running: {path} {args}"); var outputBuilder = new StringBuilder(); var processStartInfo = new ProcessStartInfo { @@ -127,7 +122,7 @@ public static (int, string) TryRunProcess( if (workingDir != null) processStartInfo.WorkingDirectory = workingDir; - logger.LogMessage(debugMessageImportance, $"{msgPrefix}Using working directory: {workingDir ?? Environment.CurrentDirectory}", msgPrefix); + logger.LogMessage(messageImportance, $"{msgPrefix}Using working directory: {workingDir ?? Environment.CurrentDirectory}", msgPrefix); if (envVars != null) { @@ -145,42 +140,36 @@ public static (int, string) TryRunProcess( if (process == null) throw new ArgumentException($"{msgPrefix}Process.Start({path} {args}) returned null process"); - process.ErrorDataReceived += (sender, e) => - { - lock (s_SyncObj) - { - if (string.IsNullOrEmpty(e.Data)) - return; + process.ErrorDataReceived += LogOutput; + process.OutputDataReceived += LogOutput; - string msg = $"{msgPrefix}{e.Data}"; - if (!silent) - { - if (logStdErrAsMessage) - logger.LogMessage(debugMessageImportance, e.Data, msgPrefix); - else - logger.LogWarning(msg); - } - outputBuilder.AppendLine(e.Data); - } - }; - process.OutputDataReceived += (sender, e) => + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + process.WaitForExit(); + + process.ErrorDataReceived -= LogOutput; + process.OutputDataReceived -= LogOutput; + + logger.LogMessage(messageImportance, $"{msgPrefix}Exit code: {process.ExitCode}"); + return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n')); + + void LogOutput(object sender, DataReceivedEventArgs args) { + string? line = args.Data; lock (s_SyncObj) { - if (string.IsNullOrEmpty(e.Data)) + if (string.IsNullOrEmpty(line)) return; - if (!silent) - logger.LogMessage(debugMessageImportance, e.Data, msgPrefix); - outputBuilder.AppendLine(e.Data); - } - }; - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - process.WaitForExit(); + string message = $"{msgPrefix}{line}"; + if (logErrorsAndWarningsFromOutput) + logger.LogMessageFromText(message, messageImportance); + else + logger.LogMessage(messageImportance, message); - logger.LogMessage(debugMessageImportance, $"{msgPrefix}Exit code: {process.ExitCode}"); - return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n')); + outputBuilder.AppendLine(line); + } + } } internal static string CreateTemporaryBatchFile(string command) diff --git a/src/tasks/WasmAppBuilder/EmccCompile.cs b/src/tasks/WasmAppBuilder/EmccCompile.cs index 0011a50a831c41..d48e8aa900266c 100644 --- a/src/tasks/WasmAppBuilder/EmccCompile.cs +++ b/src/tasks/WasmAppBuilder/EmccCompile.cs @@ -201,8 +201,7 @@ bool ProcessSourceFile(string srcFile, string objFile) command, envVarsDict, workingDir: Environment.CurrentDirectory, - logStdErrAsMessage: true, - debugMessageImportance: messageImportance, + messageImportance: messageImportance, label: Path.GetFileName(srcFile)); var endTime = DateTime.Now; diff --git a/src/tasks/WorkloadBuildTasks/InstallWorkloadFromArtifacts.cs b/src/tasks/WorkloadBuildTasks/InstallWorkloadFromArtifacts.cs index 08f933b2b6e01d..15d0613fb0312c 100644 --- a/src/tasks/WorkloadBuildTasks/InstallWorkloadFromArtifacts.cs +++ b/src/tasks/WorkloadBuildTasks/InstallWorkloadFromArtifacts.cs @@ -88,8 +88,7 @@ private bool ExecuteInternal() Path.Combine(SdkDir, "dotnet"), $"workload install --skip-manifest-update --no-cache --configfile \"{nugetConfigPath}\" {WorkloadId.ItemSpec}", workingDir: Path.GetTempPath(), - silent: false, - debugMessageImportance: MessageImportance.High); + messageImportance: MessageImportance.High); if (exitCode != 0) { Log.LogError($"workload install failed: {output}"); diff --git a/src/tasks/WorkloadBuildTasks/PackageInstaller.cs b/src/tasks/WorkloadBuildTasks/PackageInstaller.cs index 9442b0b3f65f5a..7cada4a0c71f8c 100644 --- a/src/tasks/WorkloadBuildTasks/PackageInstaller.cs +++ b/src/tasks/WorkloadBuildTasks/PackageInstaller.cs @@ -60,7 +60,7 @@ private bool InstallActual(PackageReference[] references, bool stopOnMissing) _logger.LogMessage(MessageImportance.Low, $"Restoring packages: {string.Join(", ", references.Select(r => $"{r.Name}/{r.Version}"))}"); string args = $"restore \"{projectPath}\" /p:RestorePackagesPath=\"{_packagesDir}\""; - (int exitCode, string output) = Utils.TryRunProcess(_logger, "dotnet", args, silent: false, debugMessageImportance: MessageImportance.Low); + (int exitCode, string output) = Utils.TryRunProcess(_logger, "dotnet", args, messageImportance: MessageImportance.Low); if (exitCode != 0) { LogErrorOrWarning($"Restoring packages failed with exit code: {exitCode}. Output:{Environment.NewLine}{output}", stopOnMissing);