2008-09-16 6 views

答えて

6

これはできません。

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

私は、1つの出力が複数のストリームに行くことを可能にする 'teestream'のthis implementationを見つけました。 OutputDebugStringにデータを送信するストリームを実装できます。

+1

-1、間違っています。ベンの答えを見てください。 – MSalters

1

これは、出力画面がちょうど点滅して消えてしまうケースですか?もしそうなら、あなたは戻り前に最後のステートメントとしてcinを使って開いておくことができます。

+0

cinを単独で評価することは効果がないため、できません。 – ben

8

あなたは、たとえば、次のようにCOUTの出力をキャプチャすることができます:Visual Studio 2005の出力ウィンドウにそれをMagicking

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のプラグイン開発者への課題として残されています。しかし、ファイルやカスタムウインドウのように別の場所にリダイレクトすることもできます。おそらく、カスタムstreambufクラスを作成することもできます(boost.iostreamも参照してください)。

+3

プラグインは必要ありません。Mike Dimmickの言葉通りOutputDebugStringを使用してください。 – jwfearn

2

ベンの回答とMike Dimmickの組み合わせ:OutputDebugStringの呼び出しを終えるstream_buf_を実装しています。多分誰かがこれをやったのだろうか? 2つのBoostロギングライブラリを見てみましょう。

+0

これは次のようになります:http://www.codeproject.com/KB/debug/debugout.aspx – wimh

0

また、あなたの意図、そして何を使用しているライブラリに応じて、あなたが使用することをお勧めしますTRACE macroMFC)またはATLTRACEATL)。

14

私は最終的にこれを実装しましたので、私はあなたとそれを共有したい:あなたが書いた場合:

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

出力ウィンドウと、その後、二重に

#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 
} 

ボーナスTIPそれをクリックすると、Visual Studioは10行目のファイルにジャンプし、ステータスバーに「メッセージ」を表示します。 非常にです。

+1

これは私のためにはうまくいきましたが、VS2013とBoost 1.57ではBoostコードのアサーションエラーでクラッシュします。すぐにストリームがフラッシュされると、たくさんの印刷や 'std :: endl'をストリームに送ることで、もう使えなくなってしまいます:-(ブーストのバグかどうかは分かりません。 – Malvineous

関連する問題