17

C++ コンソール アプリを作成しましたが、Visual Studio 2005 IDE 内の出力ウィンドウで cout/cerr ステートメントをキャプチャしたいだけです。これは私が見逃している設定に過ぎないと確信しています。誰かが私を正しい方向に向けることができますか?

4

7 に答える 7

14

やっとこれを実装したので、あなたと共有したいと思います:

#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 行目にジャンプし、ステータス バーに「メッセージ」を表示します。とても便利です。

于 2011-05-22T08:22:04.470 に答える
8

たとえば、次のように 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 も参照) を作成することで、ファイルやカスタム ウィンドウなどの別の場所にリダイレクトすることができます。

于 2008-09-16T16:59:26.543 に答える
6

これはできません。

デバッガーの出力ウィンドウに出力する場合は、OutputDebugString を呼び出します。

1 つの出力を複数のストリームに送ることができる「teestream」のこの実装を見つけました。データを OutputDebugString に送信するストリームを実装できます。

于 2008-09-16T15:07:21.073 に答える
2

ベンの答えとマイク・ディミックの答えの組み合わせ:最終的にOutputDebugStringを呼び出すstream_buf_を実装することになります。たぶん、誰かがすでにこれを行っていますか?提案されている 2 つの Boost ロギング ライブラリを見てみましょう。

于 2008-09-17T00:39:30.853 に答える
1

これは、出力画面が点滅して消えるだけの場合ですか? その場合は、戻る前の最後のステートメントとして cin を使用して、開いたままにすることができます。

于 2008-09-16T15:07:29.823 に答える
0

また、目的と使用しているライブラリによっては、 TRACE マクロ( MFC ) またはATLTRACE ( ATL )を使用することもできます。

于 2008-09-17T07:24:22.643 に答える
0

std::ostringsteam に書き込み、それを TRACE します。

std::ostringstream oss;

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

TRACE(oss.str().data());
于 2019-08-20T12:17:53.213 に答える