From d6fa60a3b8c48d7a41afe178e32ae7e4e9d64668 Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Thu, 4 Jun 2020 14:20:11 -0400 Subject: [PATCH] Add DreamDaemon logging to Windows --- .../Session/SessionControllerFactory.cs | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs b/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs index e5d61c60d1..9be2d437be 100644 --- a/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs +++ b/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.Linq; using System.Net; using System.Net.Sockets; +using System.Text; using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Api; @@ -233,12 +234,18 @@ namespace Tgstation.Server.Host.Components.Session var visibility = apiValidate ? "invisible" : "public"; // important to run on all ports to allow port changing - var arguments = String.Format(CultureInfo.InvariantCulture, "{0} -port {1} -ports 1-65535 {2}-close -{3} -{4} -public -params \"{5}\"", + Guid? logFileGuid = null; + var arguments = String.Format( + CultureInfo.InvariantCulture, + "{0} -port {1} -ports 1-65535 {2}-close -{3} -{4}{5} -public -params \"{6}\"", dmbProvider.DmbName, portToUse, launchParameters.AllowWebClient.Value ? "-webclient " : String.Empty, SecurityWord(launchParameters.SecurityLevel.Value), visibility, + platformIdentifier.IsWindows + ? $" -log {logFileGuid = Guid.NewGuid()}" + : String.Empty, // Just use stdout on linux parameters); // See https://github.com/tgstation/tgstation-server/issues/719 @@ -253,16 +260,52 @@ namespace Tgstation.Server.Host.Components.Session noShellExecute, noShellExecute: noShellExecute); - if (noShellExecute) + async Task GetDDOutput() { - // Log DD output - _ = process.Lifetime.ContinueWith( - x => logger.LogTrace( - "DreamDaemon Output:{0}{1}", - Environment.NewLine, process.GetCombinedOutput()), - TaskScheduler.Current); + if (!platformIdentifier.IsWindows) + return process.GetCombinedOutput(); + + var logFilePath = ioManager.ConcatPath(basePath, logFileGuid.ToString()); + try + { + var dreamDaemonLogBytes = await ioManager.ReadAllBytes( + logFilePath, + default) + .ConfigureAwait(false); + + return Encoding.UTF8.GetString(dreamDaemonLogBytes); + } + finally + { + try + { + await ioManager.DeleteFile(logFilePath, default).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.LogWarning("Failed to delete DreamDaemon log file {0}: {1}", logFilePath, ex); + } + } } + // Log DD output + _ = process.Lifetime.ContinueWith( + async x => + { + try + { + var ddOutput = await GetDDOutput().ConfigureAwait(false); + logger.LogTrace( + "DreamDaemon Output:{0}{1}", + Environment.NewLine, ddOutput); + } + catch (Exception ex) + { + logger.LogWarning("Error reading DreamDaemon output: {0}", ex); + } + }, + TaskScheduler.Current); + try { networkPromptReaper.RegisterProcess(process);