Skip to content

Commit

Permalink
[gh-9] Potential fix to the startup crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmind committed Jul 29, 2015
1 parent ea53e5f commit da07907
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions ExceptionBreaker/ExceptionBreaker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
<Compile Include="Implementation\VersionSpecific\DebuggerInternal11Adapter.cs" />
<Compile Include="Implementation\VersionSpecific\IDebuggerInternalAdapter.cs" />
<Compile Include="Implementation\VersionSpecific\VersionSpecificAdapterFactory.cs" />
<Compile Include="Implementation\VSInteropHelper.cs" />
<Compile Include="Options\ExceptionViewModel.cs" />
<Compile Include="Options\PatternData.cs" />
<Compile Include="Options\RegexValidationRule.cs" />
Expand Down
2 changes: 1 addition & 1 deletion ExceptionBreaker/ExceptionBreakerPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override void Initialize()
{
Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this));
base.Initialize();
Logger = new ExtensionLogger("ExceptionBreaker", paneCaption => GetOutputPane(GuidList.OutputPane, paneCaption));
Logger = new ExtensionLogger("ExceptionBreaker", this, GuidList.OutputPane);

_dte = (DTE)GetService(typeof(DTE));
SetupExceptionBreakManager();
Expand Down
46 changes: 41 additions & 5 deletions ExceptionBreaker/Implementation/ExtensionLogger.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,61 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;

namespace ExceptionBreaker.Implementation {
public class ExtensionLogger : IDiagnosticLogger {
private readonly IVsOutputWindowPane _outputPane;
private readonly string _traceCategory;
private readonly IServiceProvider _serviceProvider;
private readonly Guid _outputPaneGuid;
private readonly string _outputPaneCaption;

public ExtensionLogger(string name, Func<string, IVsOutputWindowPane> getOutputPane) {
_outputPane = getOutputPane("Ext: " + name + " (Diagnostic)");
public ExtensionLogger(string name, IServiceProvider serviceProvider, Guid outputPaneGuid) {
_traceCategory = name;
_serviceProvider = serviceProvider;
_outputPaneGuid = outputPaneGuid;
_outputPaneCaption = "Ext: " + _traceCategory + " (Diagnostic)";
}

public void WriteLine(string message) {
_outputPane.OutputString(message + Environment.NewLine);
var outputPane = GetOutputPane();
if (outputPane != null)
outputPane.OutputString(message + Environment.NewLine);

Trace.WriteLine(message, _traceCategory);
}

public void WriteLine(string format, params object[] args) {
WriteLine(string.Format(format, args));
}

public void WriteLine(string format, object arg1) {
WriteLine(string.Format(format, arg1));
}

public void WriteLine(string format, object arg1, object arg2) {
WriteLine(string.Format(format, arg1, arg2));
}

private IVsOutputWindowPane GetOutputPane() {
var outputWindow = (IVsOutputWindow)_serviceProvider.GetService(typeof(SVsOutputWindow));
if (outputWindow == null)
return null;

var guid = _outputPaneGuid;
var pane = (IVsOutputWindowPane)null;
var hr = outputWindow.GetPane(ref guid, out pane);
if (hr != VSConstants.E_FAIL && hr != VSConstants.E_INVALIDARG)
VSInteropHelper.Validate(hr);

if (pane == null) {
VSInteropHelper.Validate(outputWindow.CreatePane(ref guid, _outputPaneCaption, 1, 1));
VSInteropHelper.Validate(outputWindow.GetPane(ref guid, out pane));
}

VSInteropHelper.Validate(pane.Activate());
return pane;
}
}
}
}
6 changes: 5 additions & 1 deletion ExceptionBreaker/Implementation/IDiagnosticLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace ExceptionBreaker.Implementation {
using JetBrains.Annotations;

namespace ExceptionBreaker.Implementation {
public interface IDiagnosticLogger {
void WriteLine(string message);
void WriteLine(string format, params object[] args);
void WriteLine(string format, object arg1);
void WriteLine(string format, object arg1, object arg2);
}
}
21 changes: 21 additions & 0 deletions ExceptionBreaker/Implementation/VSInteropHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;

namespace ExceptionBreaker.Implementation {
public static class VSInteropHelper {
public static void Validate(int hresult) {
if (hresult != VSConstants.S_OK)
Marshal.ThrowExceptionForHR(hresult);
}

public static void Release(object comObject) {
if (comObject == null)
return;

Marshal.ReleaseComObject(comObject);
}
}
}

0 comments on commit da07907

Please sign in to comment.