public static string GetStringFromStdout(ProcessRunner runner, bool throwOnErrors = false, bool trimTrailingWhitespace = true, bool quietErrors = false) { using (var sw = new StringWriter()) { runner.AddStandardOutputSink(sw); if (!runner.Run()) { LogError("did not exit cleanly"); return(null); } if (runner.ExitCode != 0) { LogError($"failed with exit code {runner.ExitCode}"); return(null); } string ret = sw.ToString(); if (trimTrailingWhitespace) { return(ret.TrimEnd()); } return(ret); } void LogError(string message) { string msg = $"{runner.FullCommandLine}: {message}"; if (throwOnErrors) { throw new InvalidOperationException(msg); } if (quietErrors) { Log.DebugLine(msg); } else { Log.ErrorLine(msg); } } }
protected TextWriter SetupOutputSink(ProcessRunner runner, string tags = null, string messagePrefix = null) { string logFilePath = null; if (!String.IsNullOrEmpty(tags)) { logFilePath = Context.GetLogFilePath(tags ?? String.Empty); if (String.IsNullOrEmpty(messagePrefix)) { messagePrefix = "running"; } Log.StatusLine($"{LogMessageIndent}[{ToolName}] {messagePrefix}"); Log.StatusLine($"[{ToolName}] log file: ", $"{Utilities.GetRelativePath (BuildPaths.XamarinAndroidSourceRoot, logFilePath)}", tailColor: Log.DestinationColor); } TextWriter ret = CreateLogSink(logFilePath); runner.AddStandardErrorSink(ret); runner.AddStandardOutputSink(ret); return(ret); }