Skip to content

Commit

Permalink
Added menu item to enable debug logs in release build
Browse files Browse the repository at this point in the history
Menu items are disabled until extension is loaded.
  • Loading branch information
WheretIB committed May 30, 2020
1 parent b690718 commit 65683d3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
12 changes: 12 additions & 0 deletions LuaDkmDebugger/LuaDkmDebugger.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,22 @@
<Button guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaEnableBreakpointsCommandId" priority="0x0100" type="Button">
<Parent guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaMenuGroup" />
<CommandFlag>DontCache</CommandFlag>
<CommandFlag>DefaultDisabled</CommandFlag>
<Strings>
<ButtonText>Attach on Launch</ButtonText>
<CommandName>LuaEnableBreakpointsCommandId</CommandName>
</Strings>
</Button>

<Button guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaEnableLoggingCommandId" priority="0x0100" type="Button">
<Parent guid="guidLuaDkmDebuggerPackageCmdSet" id="LuaMenuGroup" />
<CommandFlag>DontCache</CommandFlag>
<CommandFlag>DefaultDisabled</CommandFlag>
<Strings>
<ButtonText>Enable Debug Logs</ButtonText>
<CommandName>LuaEnableLoggingCommandId</CommandName>
</Strings>
</Button>
</Buttons>
</Commands>

Expand All @@ -39,6 +50,7 @@
<IDSymbol name="LuaMainExtensionMenu" value="0x1021" />
<IDSymbol name="LuaMenuGroup" value="0x1010" />
<IDSymbol name="LuaEnableBreakpointsCommandId" value="0x0120" />
<IDSymbol name="LuaEnableLoggingCommandId" value="0x0130" />
</GuidSymbol>
</Symbols>
</CommandTable>
68 changes: 60 additions & 8 deletions LuaDkmDebugger/LuaDkmDebuggerPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ public sealed class LuaDkmDebuggerPackage : AsyncPackage
/// </summary>
public const string PackageGuidString = "99662a30-53ec-42a6-be5d-80aed0e1e2ea";

public const int CommandId = 0x0120;
public const int AttachCommandId = 0x0120;
public const int LoggingCommandId = 0x0130;

public static readonly Guid CommandSet = new Guid("6EB675D6-C146-4843-990E-32D43B56706C");

private IServiceProvider ServiceProvider => this;

#region Package Members

public static bool attachOnLaunch = true;
public static bool releaseDebugLogs = false;

private WritableSettingsStore configurationSettingsStore = null;

/// <summary>
Expand All @@ -70,8 +74,10 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
configurationSettingsStore.CreateCollection("LuaDkmDebugger");

attachOnLaunch = configurationSettingsStore.GetBoolean("LuaDkmDebugger", "AttachOnLaunch", true);
releaseDebugLogs = configurationSettingsStore.GetBoolean("LuaDkmDebugger", "ReleaseDebugLogs", false);

LuaDkmDebuggerComponent.LocalComponent.attachOnLaunch = attachOnLaunch;
LuaDkmDebuggerComponent.LocalComponent.releaseDebugLogs = releaseDebugLogs;
}
catch (Exception e)
{
Expand All @@ -82,19 +88,35 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke

if (commandService != null)
{
CommandID menuCommandID = new CommandID(CommandSet, CommandId);
{
CommandID menuCommandID = new CommandID(CommandSet, AttachCommandId);

OleMenuCommand menuItem = new OleMenuCommand(AttachMenuItemCallback, menuCommandID);

menuItem.BeforeQueryStatus += AttachOnBeforeQueryStatus;

OleMenuCommand menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID);
menuItem.Enabled = true;
menuItem.Checked = attachOnLaunch;

commandService.AddCommand(menuItem);
}

{
CommandID menuCommandID = new CommandID(CommandSet, LoggingCommandId);

menuItem.BeforeQueryStatus += OnBeforeQueryStatus;
OleMenuCommand menuItem = new OleMenuCommand(LoggingMenuItemCallback, menuCommandID);

menuItem.Checked = attachOnLaunch;
menuItem.BeforeQueryStatus += LoggingOnBeforeQueryStatus;

commandService.AddCommand(menuItem);
menuItem.Enabled = true;
menuItem.Checked = releaseDebugLogs;

commandService.AddCommand(menuItem);
}
}
}

private void MenuItemCallback(object sender, EventArgs args)
private void AttachMenuItemCallback(object sender, EventArgs args)
{
if (sender is OleMenuCommand command)
{
Expand All @@ -116,14 +138,44 @@ private void MenuItemCallback(object sender, EventArgs args)
}
}

private void OnBeforeQueryStatus(object sender, EventArgs args)
private void AttachOnBeforeQueryStatus(object sender, EventArgs args)
{
if (sender is OleMenuCommand command)
{
command.Checked = attachOnLaunch;
}
}

private void LoggingMenuItemCallback(object sender, EventArgs args)
{
if (sender is OleMenuCommand command)
{
releaseDebugLogs = !releaseDebugLogs;

try
{
if (configurationSettingsStore != null)
configurationSettingsStore.SetBoolean("LuaDkmDebugger", "ReleaseDebugLogs", releaseDebugLogs);

LuaDkmDebuggerComponent.LocalComponent.releaseDebugLogs = releaseDebugLogs;
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Failed to setup setting with " + e.Message);
}

command.Checked = releaseDebugLogs;
}
}

private void LoggingOnBeforeQueryStatus(object sender, EventArgs args)
{
if (sender is OleMenuCommand command)
{
command.Checked = releaseDebugLogs;
}
}

#endregion
}
}
10 changes: 10 additions & 0 deletions LuaDkmDebuggerComponent/LocalComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ internal class LuaResolvedDocumentItem : DkmDataItem
public class LocalComponent : IDkmCallStackFilter, IDkmSymbolQuery, IDkmSymbolCompilerIdQuery, IDkmSymbolDocumentCollectionQuery, IDkmLanguageExpressionEvaluator, IDkmSymbolDocumentSpanQuery, IDkmModuleInstanceLoadNotification, IDkmCustomMessageCallbackReceiver, IDkmLanguageInstructionDecoder
{
public static bool attachOnLaunch = true;
public static bool releaseDebugLogs = false;

#if DEBUG
public static Log log = new Log(Log.LogLevel.Debug);
Expand Down Expand Up @@ -1963,7 +1964,16 @@ void IDkmModuleInstanceLoadNotification.OnModuleInstanceLoad(DkmModuleInstance m
{
var process = moduleInstance.Process;

#if DEBUG
log.logPath = $"{Path.GetDirectoryName(process.Path)}\\lua_dkm_debug_log.txt";
#else
if (releaseDebugLogs)
{
log.logLevel = Log.LogLevel.Debug;

log.logPath = $"{Path.GetDirectoryName(process.Path)}\\lua_dkm_debug_log.txt";
}
#endif

var processData = DebugHelpers.GetOrCreateDataItem<LuaLocalProcessData>(process);

Expand Down
2 changes: 0 additions & 2 deletions LuaDkmDebuggerComponent/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ protected void Output(string text)

System.Diagnostics.Debug.WriteLine(formatted);

#if DEBUG
if (logPath != null)
{
using (var writer = File.AppendText(logPath))
writer.WriteLine(formatted);
}
#endif
}
catch (Exception)
{
Expand Down

0 comments on commit 65683d3

Please sign in to comment.