2011-11-10 3 views
2

콘솔에 오류를 인쇄하는 외부 libs를 사용하는 GL 응용 프로그램을 작성하고 있습니다. 나는 그것을 잡아서 게임 콘솔에서 인쇄하고 싶습니다.stdout 및 stderr 스트림을 리디렉션하는 방법 (멀티 플랫폼)?

PS : 죄송합니다, 내 나쁜 영어 ....이 걸릴 수있는 두 가지 기본 방법이 있습니다

+0

http://homepage.ntlworld.com/jonathan. deboynepollard/FGA/redirecting-standard-io.html – Flexo

답변

3

: 라이브러리 모두가 IO에 대한 std::cout를 사용하는 경우

  1. 당신이 원하는 캡처 할 수 있습니다 write your own basic_streambuf. 당신은 특정 플랫폼 접근 방식을 사용할 수 있습니다

    #include <sstream> 
    #include <iostream> 
    
    int main() { 
        static std::basic_stringbuf<std::ostream::char_type> buf; 
        std::cout.rdbuf(&buf); 
        std::cout << "Hello captured world!\n"; 
        std::cerr << "Stole: " << buf.str() << std::endl; 
    } 
    
  2. , 예를 들면 : 당신은 단지 사용 예를 들어, std::basic_stringbuf을 streambuffer을 대체 할 std::cout.rdbuf(mybufinst);를 호출 할 수 있습니다 POSIX 시스템에서는 dup2() will allow you to replace a file descriptor with another one 또는 Windows에서는 SetStdHandle()입니다. 아마도 다른 파일보다는 파이프를 사용하고 싶을 것이고 차단에 정말로주의해야 할 것입니다. (전용 스레드가 필요할 수도 있습니다)