在 Visual Studio 2005 输出窗口中捕获 cout?

发布于 2024-07-05 11:20:43 字数 107 浏览 5 评论 0原文

我创建了一个 C++ 控制台应用程序,只想捕获 Visual Studio 2005 IDE 中输出窗口中的 cout/cerr 语句。 我确信这只是我缺少的一个设置。 有人能指出我正确的方向吗?

I created a C++ console app and just want to capture the cout/cerr statements in the Output Window within the Visual Studio 2005 IDE. I'm sure this is just a setting that I'm missing. Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不一样的天空 2024-07-12 11:20:43

Ben 的答案和 Mike Dimmick 的答案的组合:您将实现一个最终调用 OutputDebugString 的stream_buf_。 也许有人已经这样做了? 看一下建议的两个 Boost 日志记录库。

A combination of ben's answer and Mike Dimmick's: you would be implementing a stream_buf_ that ends up calling OutputDebugString. Maybe someone has done this already? Take a look at the two proposed Boost logging libraries.

开始看清了 2024-07-12 11:20:43

这是输出屏幕闪烁然后消失的情况吗? 如果是这样,您可以使用 cin 作为返回前的最后一条语句来保持它打开。

Is this a case of the output screen just flashing and then dissapearing? if so you can keep it open by using cin as your last statement before return.

塔塔猫 2024-07-12 11:20:43

另外,根据您的意图以及您使用的库,您可能需要使用 TRACE 宏 (MFC) 或 ATLTRACE (ATL)。

Also, depending on your intentions, and what libraries you are using, you may want to use the TRACE macro (MFC) or ATLTRACE (ATL).

幽蝶幻影 2024-07-12 11:20:43

你不能这样做。

如果要输出到调试器的输出窗口,请调用OutputDebugString。

我发现了“teestream”的这种实现,它允许一个输出进入多个流。 您可以实现一个将数据发送到 OutputDebugString 的流。

You can't do this.

If you want to output to the debugger's output window, call OutputDebugString.

I found this implementation of a 'teestream' which allows one output to go to multiple streams. You could implement a stream that sends data to OutputDebugString.

一世旳自豪 2024-07-12 11:20:43

您可以像这样捕获 cout 的输出,例如:

std::streambuf* old_rdbuf = std::cout.rdbuf();
std::stringbuf new_rdbuf;
// replace default output buffer with string buffer
std::cout.rdbuf(&new_rdbuf);

// write to new buffer, make sure to flush at the end
std::cout << "hello, world" << std::endl;

std::string s(new_rdbuf.str());
// restore the default buffer before destroying the new one
std::cout.rdbuf(old_rdbuf);

// show that the data actually went somewhere
std::cout << s.size() << ": " << s;

将其神奇地放入 Visual Studio 2005 输出窗口中作为 Visual Studio 2005 插件开发人员的练习。 但是您可能可以将其重定向到其他地方,例如文件或自定义窗口,也许可以通过编写自定义streambuf类(另请参阅boost.iostream)。

You can capture the output of cout like this, for example:

std::streambuf* old_rdbuf = std::cout.rdbuf();
std::stringbuf new_rdbuf;
// replace default output buffer with string buffer
std::cout.rdbuf(&new_rdbuf);

// write to new buffer, make sure to flush at the end
std::cout << "hello, world" << std::endl;

std::string s(new_rdbuf.str());
// restore the default buffer before destroying the new one
std::cout.rdbuf(old_rdbuf);

// show that the data actually went somewhere
std::cout << s.size() << ": " << s;

Magicking it into the Visual Studio 2005 output window is left as an exercise to a Visual Studio 2005 plugin developer. But you could probably redirect it elsewhere, like a file or a custom window, perhaps by writing a custom streambuf class (see also boost.iostream).

三生一梦 2024-07-12 11:20:43

我终于实现了这个,所以我想与您分享:

#include <vector>
#include <iostream>
#include <windows.h>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>

using namespace std;
namespace io = boost::iostreams;

struct DebugSink
{
    typedef char char_type;
    typedef io::sink_tag category;

    std::vector<char> _vec;

    std::streamsize write(const char *s, std::streamsize n)
    {
        _vec.assign(s, s+n);
        _vec.push_back(0); // we must null-terminate for WINAPI
        OutputDebugStringA(&_vec[0]);
        return n;
    }
};

int main()
{
    typedef io::tee_device<DebugSink, std::streambuf> TeeDevice;
    TeeDevice device(DebugSink(), *cout.rdbuf());
    io::stream_buffer<TeeDevice> buf(device);
    cout.rdbuf(&buf);

    cout << "hello world!\n";
    cout.flush(); // you may need to flush in some circumstances
}

额外提示:如果您

X:\full\file\name.txt(10) : message

在输出窗口中写入:然后双击它,那么 Visual Studio 将跳转到给定的位置文件第 10 行,并在状态栏中显示“消息”。 它非常很有用。

I've finally implemented this, so I want to share it with you:

#include <vector>
#include <iostream>
#include <windows.h>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>

using namespace std;
namespace io = boost::iostreams;

struct DebugSink
{
    typedef char char_type;
    typedef io::sink_tag category;

    std::vector<char> _vec;

    std::streamsize write(const char *s, std::streamsize n)
    {
        _vec.assign(s, s+n);
        _vec.push_back(0); // we must null-terminate for WINAPI
        OutputDebugStringA(&_vec[0]);
        return n;
    }
};

int main()
{
    typedef io::tee_device<DebugSink, std::streambuf> TeeDevice;
    TeeDevice device(DebugSink(), *cout.rdbuf());
    io::stream_buffer<TeeDevice> buf(device);
    cout.rdbuf(&buf);

    cout << "hello world!\n";
    cout.flush(); // you may need to flush in some circumstances
}

BONUS TIP: If you write:

X:\full\file\name.txt(10) : message

to the output window and then double-click on it, then Visual Studio will jump to the given file, line 10, and display the 'message' in status bar. It's very useful.

迷乱花海 2024-07-12 11:20:43

写入 std::ostringsteam 然后跟踪它。

std::ostringstream oss;

oss << "w:=" << w << " u=" << u << " vt=" << vt << endl;

TRACE(oss.str().data());

Write to a std::ostringsteam and then TRACE that.

std::ostringstream oss;

oss << "w:=" << w << " u=" << u << " vt=" << vt << endl;

TRACE(oss.str().data());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文