-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[gh-9] Potential fix to the startup crash.
- Loading branch information
Showing
5 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |