简体   繁体   English

如何在运行时禁用 cout 输出?

[英]How to disable cout output in the runtime?

I often use cout for debugging purpose in many different places in my code, and then I get frustrated and comment all of them manually.我经常在代码中的许多不同地方使用cout进行调试,然后我感到沮丧并手动注释所有这些。

Is there a way to suppress cout output in the runtime?有没有办法在运行时抑制 cout 输出?

And more importantly, let's say I want to suppress all cout outputs, but I still want to see 1 specific output (let's say the final output of the program) in the terminal.更重要的是,假设我想抑制所有cout输出,但我仍然希望在终端中看到 1 个特定输出(假设程序的最终输出)。

Is it possible to use an ""other way"" of printing to the terminal for showing the program output, and then when suppressing cout still see things that are printed using this ""other way""?是否可以使用“其他方式”打印到终端来显示程序输出,然后在抑制 cout 时仍然看到使用这种“其他方式”打印的内容?

Sure, you can ( example here ):当然,你可以(这里的例子):

int main() {
    std::cout << "First message" << std::endl;

    std::cout.setstate(std::ios_base::failbit);
    std::cout << "Second message" << std::endl;

    std::cout.clear();
    std::cout << "Last message" << std::endl;

    return 0;
}

Outputs:输出:

First message
Last message

This is because putting the stream in fail state will make it silently discard any output, until the failbit is cleared.这是因为将流置于fail状态将使其静默丢弃任何输出,直到清除fail位。

不要将cout用于调试目的,而是定义一个调用它的不同对象(或函数或宏),然后您可以在一个地方禁用该函数或宏。

To supress output, you can disconnect the underlying buffer from cout.要抑制输出,您可以断开底层缓冲区与 cout 的连接。

#include <iostream>

using namespace std;

int main(){

    // get underlying buffer
    streambuf* orig_buf = cout.rdbuf();

    // set null
    cout.rdbuf(NULL);

    cout << "this will not be displayed." << endl;

    // restore buffer
    cout.rdbuf(orig_buf);

    cout << "this will be dispalyed." << endl;

    return 0;
}

You can user cerr - standard output stream for errors for your debug purposes.您可以使用cerr - 用于调试目的的错误的标准输出流。

Also, there is clog - standard output stream for logging.此外,还有clog - 用于记录的标准输出流。

Typically, they both behave like a cout .通常,它们的行为都像cout

Example:例子:

cerr << 74 << endl;

Details: http://www.cplusplus.com/reference/iostream/cerr/详情: http : //www.cplusplus.com/reference/iostream/cerr/

http://www.cplusplus.com/reference/iostream/clog/ http://www.cplusplus.com/reference/iostream/clog/

It seems you print debug messages.看来您打印了调试消息。 You could use TRACE within Visual C++/MFC or you just might want to create a Debug() function which takes care of it.您可以在 Visual C++/MFC 中使用TRACE ,或者您可能只想创建一个Debug()函数来处理它。 You can implement it to turn on only if a distinct flag is set.只有在设置了不同的标志时,您才能实现它以打开。 A lot of programs use a command line parameter called verbose or -v for instance, to control the behavior of their log and debug messages.例如,许多程序使用名为verbose-v的命令行参数来控制其日志和调试消息的行为。

If you include files which involve cout you may want to write the code at the start (outside of main), which can be done like this:如果您包含涉及cout文件,您可能希望在开始时(在 main 之外)编写代码,可以这样完成:

struct Clearer {
    Clearer() { std::cout.setstate(std::ios::failbit); }
} output_clearer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM