views:

2813

answers:

3

I am writing a custom tool and I currently have it doing what I want as far as functionality. I would like to be able to write to Visual Studio if something goes wrong. (Incorrectly formatted code or whatever).

Are there any standards for this? Right now I basically can force the tool to fail and Visual Studio puts in a warning that it has done so. I'd like a category in the Output window with any resulting messages I want to send. I could also live with a more descriptive task/warning in the Error list window.

+1  A: 

Hello,

You can use the Debug and/or Trace classes. There is some information here: http://msdn.microsoft.com/en-us/library/bs4c1wda(VS.71).aspx

Best of luck.

Jon
I tried trace.write (message, category) prior to asking this and it didn't output.
Jeff Martin
+1  A: 

If you want anything to appear in the Output window, it has to come from stdout. To do this, your app needs to be linked as a "console" app. Set the /SUBSYSTEM:CONSOLE flag in the project's property page, under Linker/System set the SubSystem property to CONSOLE.

Once you have your output in the window, if you include the text "Error:" it will appear as an error, or if you set "Warning:" it will appear as a warning. If your error text begins with a path/filename, followed by a line number in parenthesis, the IDE will recognize it as a "clickable" error, and navigate you automatically to the faulting line.

John Deters
I am doing this in C# and there is no /subsystem stuff in the project property, do i need to make a console app to do this? its working right now as a DLL, but obviously i'm not getting the output.
Jeff Martin
It is a DLL? How is it being invoked? Normally, the output window captures the tools that are executed in the tool chain that MSBuild constructs, but as far as I knew those all were independent executables, and MSBuild would simply capture all stdout for display in the window.It just occurred to me that your custom tool may not be related to building. The "Output" window is reserved for the build process. If you are looking to put out run-time information, you should be using the debug window (with OutputDebugString() or Debug.Print() or whatever.)
John Deters
+5  A: 

Output Window

To write to the "General" output window in Visual Studio, you need to do the following:

IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;

Guid generalPaneGuid = VSConstants.GUID_OutWindowGeneralPane; // P.S. There's also the GUID_OutWindowDebugPane available.
IVsOutputWindowPane generalPane;
outWindow.GetPane( ref generalPaneGuid , out generalPane );

generalPane.OutputString( "Hello World!" );
generalPane.Activate(); // Brings this pane into view

If, however, you want to write to a custom window, this is what you need to do:

IVsOutputWindow outWindow = Package.GetGlobalService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;

// Use e.g. Tools -> Create GUID to make a stable, but unique GUID for your pane.
// Also, in a real project, this should probably be a static constant, and not a local variable
Guid customGuid = new Guid("0F44E2D1-F5FA-4d2d-AB30-22BE8ECD9789");
string customTitle = "Custom Window Title";
outWindow.CreatePane( ref customGuid, customTitle, 1, 1 );

IVsOutputWindowPane customPane;
outWindow.GetPane( ref customGuid, out customPane);

customPane.OutputString( "Hello, Custom World!" );
customPane.Activate(); // Brings this pane into view

Details on IVsOutputWindow and IVsOutputWindowPane can be found on MSDN.

Error List

For adding items to the error list, the IVsSingleFileGenerator has a method call void Generate(...) which has a parameter of the type IVsGeneratorProgress. This interface has a method void GeneratorError() which lets you report errors and warnings to the Visual Studio error list.

public class MyCodeGenerator : IVsSingleFileGenerator
{
    ...
    public void Generate( string inputFilePath, string inputFileContents, string defaultNamespace, out IntPtr outputFileContents, out int output, IVsGeneratorProgress generateProgress )
    {
        ...
        generateProgress.GeneratorError( false, 0, "An error occured", 2, 4);
        ...
    }
    ...
}

The details of GeneratorError() can be found on MSDN.

Alex