From ad8e28a50a206ab2beca7c77e6737f6a305f7c12 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Fri, 18 Aug 2023 15:17:08 -0700 Subject: [PATCH] Fixing File writing for EngineLogger (#1415) * Fixing File writing for EngineLogger With the Natvis Diagnostics Logger work, the file logger would fail since it would not close the file. This PR also runs 'File.CreateText' in MIDebugCommandDispatcher so errors can be caught earlier and not set an invalid file path. --- src/DebugEngineHost/HostLogger.cs | 2 ++ src/MIDebugEngine/MIDebugCommandDispatcher.cs | 18 ++++++++++++++++++ src/MIDebugPackage/MIDebugPackagePackage.cs | 10 ++++++++++ tools/Setup.csx | 2 +- tools/VS.list | 2 ++ 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/DebugEngineHost/HostLogger.cs b/src/DebugEngineHost/HostLogger.cs index 9ce19cf22..22a1934e3 100644 --- a/src/DebugEngineHost/HostLogger.cs +++ b/src/DebugEngineHost/HostLogger.cs @@ -54,7 +54,9 @@ public static ILogChannel GetNatvisLogChannel() public static void Reset() { + s_natvisLogChannel?.Close(); s_natvisLogChannel = null; + s_engineLogChannel?.Close(); s_engineLogChannel = null; } } diff --git a/src/MIDebugEngine/MIDebugCommandDispatcher.cs b/src/MIDebugEngine/MIDebugCommandDispatcher.cs index e07370d38..748d6904a 100644 --- a/src/MIDebugEngine/MIDebugCommandDispatcher.cs +++ b/src/MIDebugEngine/MIDebugCommandDispatcher.cs @@ -4,6 +4,7 @@ using MICore; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -89,6 +90,23 @@ private static void process_DebuggerExitEvent(object sender, EventArgs e) public static void EnableLogging(bool output, string logFile) { + if (!string.IsNullOrEmpty(logFile)) + { + string tempDirectory = Path.GetTempPath(); + if (Path.IsPathRooted(logFile) || (!string.IsNullOrEmpty(tempDirectory) && Directory.Exists(tempDirectory))) + { + string filePath = Path.Combine(tempDirectory, logFile); + + File.CreateText(filePath).Dispose(); // Test to see if we can create a text file in HostLogChannel. This will allow the error to be shown when enabling the setting. + + logFile = filePath; + } + else + { + throw new ArgumentOutOfRangeException(nameof(logFile)); + } + } + Logger.CmdLogInfo.logFile = logFile; if (output) Logger.CmdLogInfo.logToOutput = WriteLogToOutput; diff --git a/src/MIDebugPackage/MIDebugPackagePackage.cs b/src/MIDebugPackage/MIDebugPackagePackage.cs index c28fe9162..be94cc4cd 100644 --- a/src/MIDebugPackage/MIDebugPackagePackage.cs +++ b/src/MIDebugPackage/MIDebugPackagePackage.cs @@ -403,6 +403,16 @@ private void LaunchDebugTarget(string filePath, string options) private void EnableLogging(bool sendToOutputWindow, string logFile) { ThreadHelper.ThrowIfNotOnUIThread(); + + IVsDebugger debugger = (IVsDebugger)GetService(typeof(IVsDebugger)); + DBGMODE[] mode = new DBGMODE[] { DBGMODE.DBGMODE_Design }; + int hr = debugger.GetMode(mode); + + if (hr == VSConstants.S_OK && mode[0] != DBGMODE.DBGMODE_Design) + { + throw new ArgumentException("Unable to update MIDebugLog while debugging."); + } + try { MIDebugCommandDispatcher.EnableLogging(sendToOutputWindow, logFile); diff --git a/tools/Setup.csx b/tools/Setup.csx index 807d4dda9..9931534ce 100644 --- a/tools/Setup.csx +++ b/tools/Setup.csx @@ -218,7 +218,7 @@ class Setup { { listFilePath = Path.Join(scriptDirectoryPath, "VS.list"); // Use folder. - binDirectoryPath = Path.Join(binDirectoryPath, Configuration.ToString()); + binDirectoryPath = Path.Join(binDirectoryPath, "Lab." + Configuration.ToString()); } else if (Client == Client.VSCode) { diff --git a/tools/VS.list b/tools/VS.list index 3106e302b..dc274f437 100644 --- a/tools/VS.list +++ b/tools/VS.list @@ -11,6 +11,8 @@ Microsoft.MICore.dll,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger Microsoft.MICore.XmlSerializers.dll,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger Microsoft.MIDebugEngine.dll,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger Microsoft.MIDebugEngine.pkgdef,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger +Microsoft.MIDebugPackage.dll,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger +Microsoft.MIDebugPackage.pkgdef,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger Microsoft.SSHDebugPS.dll,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger Microsoft.SSHDebugPS.pkgdef,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger OpenFolderSchema.json,bin,,\Common7\IDE\CommonExtensions\Microsoft\MDD\Debugger \ No newline at end of file