将函数输出重定向到/ dev / null

时间:2021-02-27 00:06:49

I am using a library that is printing a warning message to cout or cerr. I don't want this warning message to reach the output of my program. How can I catch this output and put it into /dev/null or similar?

我正在使用一个正在向cout或cerr打印警告消息的库。我不希望此警告消息到达我的程序的输出。如何捕获此输出并将其放入/ dev / null或类似的?

MWE:

#include <iostream>

void foo()
{
    std::cout << "Boring message. " << std::endl;
};

int main()
{
    foo();
    std::cout << "Interesting message." << std::endl;
    return 0;
}

The output should be:

输出应该是:

Interesting message.

How should I modify main to get the desired output? (foo must not be changed.)

我应该如何修改main以获得所需的输出? (foo不得更改。)


I tried using freopen() and fclose(stdout) as suggested in this question How can I redirect stdout to some visible display in a Windows Application?. The result is that nothing is printed.

我尝试使用freopen()和fclose(stdout),如本问题所示。如何将stdout重定向到Windows应用程序中的某些可见显示?结果是没有打印任何东西。

3 个解决方案

#1


15  

If you are sure that thing does not redirect output (e.g. to /dev/tty/, which would be standard-out again) (which I don't think), you could redirect before calling them.

如果您确定该东西没有重定向输出(例如,再次标准输出/ dev / tty /)(我不认为),您可以在调用它们之前重定向。

#include <iostream>
#include <sstream>

void foobar() { std::cout << "foobar!\nfrob!!"; }

int main () {    
    using namespace std;

    streambuf *old = cout.rdbuf(); // <-- save        
    stringstream ss;

    cout.rdbuf (ss.rdbuf());       // <-- redirect
    foobar();                      // <-- call
    cout.rdbuf (old);              // <-- restore

    // test
    cout << "[[" << ss.str() << "]]" << endl;
}

#2


19  

May I suggest a hack? Set a bad/fail bit on the relevant stream before use of a library function.

我可以建议一个黑客?在使用库函数之前,在相关流上设置错误/失败位。

#include <iostream>

void foo()
{
    std::cout << "Boring message. " << std::endl;
}

int main()
{
    std::cout.setstate(std::ios::failbit) ;
    foo();
    std::cout.clear() ;
    std::cout << "Interesting message." << std::endl;
    return 0;
}

#3


5  

Use ios::rdbuf:

#include <iostream>

void foo()
{
    std::cout << "Boring message. " << std::endl;
}

int main()
{
    ofstream file("/dev/null");

    //save cout stream buffer
    streambuf* strm_buffer = cout.rdbuf();

    // redirect cout to /dev/null
    cout.rdbuf(file.rdbuf());

    foo();

    // restore cout stream buffer
    cout.rdbuf (strm_buffer);

    std::cout << "Interesting message." << std::endl;
    return 0;
}

#1


15  

If you are sure that thing does not redirect output (e.g. to /dev/tty/, which would be standard-out again) (which I don't think), you could redirect before calling them.

如果您确定该东西没有重定向输出(例如,再次标准输出/ dev / tty /)(我不认为),您可以在调用它们之前重定向。

#include <iostream>
#include <sstream>

void foobar() { std::cout << "foobar!\nfrob!!"; }

int main () {    
    using namespace std;

    streambuf *old = cout.rdbuf(); // <-- save        
    stringstream ss;

    cout.rdbuf (ss.rdbuf());       // <-- redirect
    foobar();                      // <-- call
    cout.rdbuf (old);              // <-- restore

    // test
    cout << "[[" << ss.str() << "]]" << endl;
}

#2


19  

May I suggest a hack? Set a bad/fail bit on the relevant stream before use of a library function.

我可以建议一个黑客?在使用库函数之前,在相关流上设置错误/失败位。

#include <iostream>

void foo()
{
    std::cout << "Boring message. " << std::endl;
}

int main()
{
    std::cout.setstate(std::ios::failbit) ;
    foo();
    std::cout.clear() ;
    std::cout << "Interesting message." << std::endl;
    return 0;
}

#3


5  

Use ios::rdbuf:

#include <iostream>

void foo()
{
    std::cout << "Boring message. " << std::endl;
}

int main()
{
    ofstream file("/dev/null");

    //save cout stream buffer
    streambuf* strm_buffer = cout.rdbuf();

    // redirect cout to /dev/null
    cout.rdbuf(file.rdbuf());

    foo();

    // restore cout stream buffer
    cout.rdbuf (strm_buffer);

    std::cout << "Interesting message." << std::endl;
    return 0;
}