2

I output log/warning/error messages by OutputDebugString() so they are readable in "output" window of Visual Studio.

I want to make those messages to be links. After clicking they will open predefined source file and put a cursor on predefined line, much like this happens with compiler errors.

How would I do this?

Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61

1 Answers1

4

Format your output string in a special way:

file_path(line): message

where file_path is a full absolute path of a file to open by click, line is a number of line cursor to be put on and 'message` is anything else.

Visual Studio parses such a string when you double-click it and opens the file.

Example with C++ and boost::format:

#include <windows.h>
#include <boost/format.hpp>
#include <string>

int main()
{
    std::string errMsg = "Yay! Fancy link!";
    std::string formatted =(boost::format("%s(%i): in function \"%s\": %s\n\n")  
                          % __FILE__ % __LINE__ % __FUNCTION__ % errMsg ).str();
    OutputDebugStringA(formatted.c_str());
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • `file_path` can be a relative path, but I don't know for certain relative to _what_. Full paths always work. (Unrelated, Macros are great for adding paths and line numbers) – Mooing Duck Apr 03 '14 at 16:58
  • Hmm, no, this certainly doesn't work on my machine. You probably ought to mention what add-in you've got installed that makes it clickable as well as the VS version. – Hans Passant Apr 03 '14 at 17:16
  • @Hans I'm using for now latest VS2013 with tons of addons, so I've checked it also on a Windows 7 virtual machine where I have a snapshot with Visual Studio 2010 fresh install (no addons, no service pack). I've used sprintf instead of boost. It works fine. I think I've used it in VS2008 too, but not sure as it was long ago. Make sure that you don't have any spaces around braces (inside or outside) and before colon, but that you have a space after colon. Also, I have no idea how path with spaces handled. – Ivan Aksamentov - Drop Apr 03 '14 at 18:48
  • @Hans Try to pass directly to `OutputDebugStringA` something like `c:\projects\foo\foo\main.cpp(9): in function "main": Yay! Fancy link!`. Does it work for you? – Ivan Aksamentov - Drop Apr 03 '14 at 18:53
  • I found the answer in [this post](http://stackoverflow.com/a/12369186/17034). You have to **double-click** the line. That was not intuitive. – Hans Passant Apr 03 '14 at 19:15
  • @Hans Wow! Didn't know that we have such question already for C#. – Ivan Aksamentov - Drop Apr 03 '14 at 19:18
  • Double-click is consistent way to follow these types of links in the error list, build output, and everywhere else I've seen in VS. – Adrian McCarthy Apr 03 '14 at 20:25