简体   繁体   English

如何在Visual Studio 2013中写入输出窗口?

[英]how to write to output window in Visual Studio 2013?

I am able to compile and run my visual c++ program. 我能够编译并运行我的可视c ++程序。 It is not a console application. 它不是控制台应用程序。 I am modifying an existing MFC application. 我正在修改现有的MFC应用程序。 I am trying to troubleshoot my program. 我正在尝试对程序进行故障排除。 I cannot run in debug and get the traces I need because my program also reads the mouse cursor. 我无法在调试中运行并获取所需的跟踪信息,因为我的程序还会读取鼠标光标。 I also tried to use a messagebox to output this string, but again, a message box interferes with the mouse. 我也尝试使用消息框输出此字符串,但是同样,消息框会干扰鼠标。

In the output window, I right-click and make sure that Program Output is enabled. 在输出窗口中,右键单击并确保已启用“程序输出”。

cout << "something" << endl;

However, in the output window, I do not see anything. 但是,在输出窗口中,我什么都看不到。

Looking at SO posts, I tried 看着SO帖子,我尝试

std::string stro = "something ";
OutputDebugString(stro);

error C2664: 'void OutputDebugStringW(LPCWSTR)' : cannot convert argument 1 from 'std::string' to 'LPCWSTR' 错误C2664:'无效OutputDebugStringW(LPCWSTR)':无法将参数1从'std :: string'转换为'LPCWSTR'

So changed to std::wstring stro 因此更改为std :: wstring stro

and

OutputDebugString(stro.c_str());

to append an int, I had to 附加一个整数,我不得不

std::wostringstream wso;    
wso << i;   
stro = stro + wso.str();
OutputDebugString(stro.c_str());

But I cannot see output in the window when not running in DEBUG. 但是,如果不在DEBUG中运行,则无法在窗口中看到输出。 Is there any way to see output in non-DEBUG? 有没有办法查看非DEBUG中的输出? This is surprisingly frustrating. 这令人惊讶地令人沮丧。

In the comments, writing a separate overloaded class was suggested; 在评论中,建议编写一个单独的重载类。 this seems like overkill. 这似乎太过分了。 Java has System.out.println even in GUI programs. Java即使在GUI程序中也具有System.out.println。 Android has Log.v(). Android具有Log.v()。 Such a pity the equivalent is not available here. 遗憾的是,此处没有等效项。

Console applications have console output, which is what cout sends to. 控制台应用程序具有控制台输出,这是cout发送到的内容。 Since this isn't a console application, you'll want to try another approach. 由于这不是控制台应用程序,因此您将尝试另一种方法。 It's strange you object "I don't see Debug output when I'm not in Debug", that's the point of it - don't use OutputDebugString and expect it to be for something other than debug output. 奇怪的是,您反对“当我不在调试状态时,我看不到调试输出”,这就是重点-不要使用OutputDebugString并期望它用于调试输出以外的其他目的。

I think the best way to understand what your application is doing, without interacting with it under the debugger (I know the frustration of trying to debug event handlers that keep being re-triggered by your debugging activities) is to try tracepoints. 我认为,了解您的应用程序正在执行的,而不与它在调试器下进行交互的最佳方法(我知道尝试调试不断被调试活动重新触发的事件处理程序的挫败感)是尝试跟踪点。 I blogged about them back in 2006 and they're still a great technique. 早在2006年,我就在博客上发布了有关它们的信息 ,但它们仍然是一项很棒的技术。 Reading the mouse cursor won't interfere with tracepoints, and you can turn them on and off without rebuilding your app or making any code changes. 读取鼠标光标不会干扰跟踪点,您可以打开或关闭它们,而无需重建应用程序或更改任何代码。

Set up a breakpoint, then right-click the red dot and choose When Hit. 设置一个断点,然后右键单击红点并选择“命中时”。 You can adjust the default message that goes into the output window to show you any values you are interested in - you can even call functions in the trace message, as you can see in the blog entry where I call the size() method of a collection. 您可以调整输出窗口中显示的默认消息,以显示您感兴趣的任何值-您甚至可以在跟踪消息中调用函数,就像您在博客条目中看到的那样,其中我调用了a的size()方法采集。 If necessary, you can even debug a release build. 如有必要,您甚至可以调试发行版。

Actually OutputDebugStrng should work in release builds - as long as you're running the app from the debugger. 实际上,只要您从调试器运行应用程序,OutputDebugStrng就应该在发行版中工作。 However cout cannot route output to the VS output pane. 但是,cout无法将输出路由到VS输出窗格。

If you already have a lot of 'cout' style debugging code, the easiest route might be to replace it with a custom ostream overload which does print to the output pane. 如果您已经有很多'cout'风格的调试代码,最简单的方法可能是将其替换为自定义的ostream重载,该重载确实会打印到输出窗格中。 Here's one , here's another . 这是一个这是另一个

If you can rewrite the debugging code completely, some macro wrappers around OutputDebugString might be better for you. 如果您可以完全重写调试代码,则一些围绕OutputDebugString的宏包装程序可能更适合您。

Having a stdout/stderr console window for debugging is very useful, and I always miss it when working on WinMain based apps. 具有用于调试的stdout / stderr控制台窗口非常有用,在基于WinMain的应用程序上工作时,我总是会错过它。 I use this code fragment to create a console for windows apps (and chain it in with existing loggers, etc.) After running this, your cout/cerr should work fine. 我使用此代码片段为Windows应用程序创建一个控制台(并将其与现有的记录器等链接在一起)。运行此代码后,您的cout / cerr应该可以正常工作。 OutputDebugString output can be seen outside of DevStudio using the DebugView app, but I prefer this: 使用DebugView应用程序可以在DevStudio外部看到OutputDebugString输出,但是我更喜欢这样:

#include <io.h>
#include <fcntl.h>
...

//  DOS box console for stdin/stdout/stderr
void makeConsole()
{
    AllocConsole();

    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
    int hCrt = _open_osfhandle((long)handle_out, _O_TEXT);
    FILE* hf_out = _fdopen(hCrt, "w");
    setvbuf(hf_out, NULL, _IONBF, 2);
    *stdout = *hf_out;

    HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
    hCrt = _open_osfhandle((long)handle_in, _O_TEXT);
    FILE* hf_in = _fdopen(hCrt, "r");
    setvbuf(hf_in, NULL, _IONBF, 2);
    *stdin = *hf_in;

    HANDLE handle_err = GetStdHandle(STD_ERROR_HANDLE);
    hCrt = _open_osfhandle((long)handle_err, _O_TEXT);
    FILE* hf_err = _fdopen(hCrt, "w");
    setvbuf(hf_err, NULL, _IONBF, 2);
    *stderr = *hf_err;

    ios::sync_with_stdio();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将日志记录写入Visual Studio输出窗口? - How to write loggings to the Visual Studio output window? 如何从visual studio 2013的输出窗口中显示“排除未选择构建此解决方案配置的项目”消息 - How to exclude “Project not selected to build for this solution configuration” message from displaying in output window in visual studio 2013 在 Visual Studio 2010 中将输出消息写入“输出窗口”的最简单方法? - Simplest way to write output message to 'output window' in Visual Studio 2010? 如何将单独控制台 window 的 output 传递给 Visual Studio output Z05B8C74CBD96FBF2DE4C1A352702? - How to pass output of separate console window to Visual Studio output window? Visual Studio - 如何摆脱输出窗口? - Visual Studio - how to get rid of output window? 如何使visual studio输出窗口可编辑 - how to make visual studio output window editable 如何在 Visual Studio 上过滤调试 output window? - How to filter debug output window on visual studio? 如何从 c# 将彩色文本写入 Visual Studio 输出窗口? - How do I write color text to the Visual Studio output window from c#? 如何在我的自定义工具中写入 Visual Studio Output Window? - How do I write to the Visual Studio Output Window in My Custom Tool? Visual Studio 2013中的“输出”窗口不显示“调试”或“跟踪”的输出 - Output window in Visual Studio 2013 does not show output from Debug or Trace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM