[英]How to get rid of std::cout quickly
I have inherited a large codebase where std::cout
is very frequently used with the purpose of printing out debugging information. 我继承了一个很大的代码库,其中
std::cout
经常用于打印调试信息。 There is no real debug/log functionality in the code and since this will be a short lived task (bugfix) implementing debug/log functionality for now is out of the question. 代码中没有真正的调试/日志功能,由于这是一个短暂的任务(错误修正),因此现在实现调试/日志功能是不可能的。
It really bothers me the amount of text this program outputs, and at least till I have to work on it I would like to disable all the printouts on a temporary basis. 这确实使我感到困扰,该程序输出的文本数量很大,至少在我不得不对其进行处理之前,我想暂时禁用所有打印输出。
I see two solutions for now: 我现在看到两种解决方案:
std::couts
span accross multiple line, so I would have to find them manually std::couts
跨越多行,所以我必须手动找到它们 std::cout
with a construct that has its own operator <<
and simply swallows all the output operator <<
的构造替换std::cout
所有出现,然后简单地吞下所有输出 Do you have a better solution for this problem? 您对此问题有更好的解决方案吗?
You can redirect stdout programmatically with something like 您可以使用以下方式以编程方式重定向标准输出
FILE origSTDOUT = *stdout;
static FILE* nul = fopen( "NUL", "w" );
*stdout = *nul;
setvbuf( stdout, NULL, _IONBF, 0 );
from the point you want to suppress output and restore with something like 从您想抑制输出并使用诸如
*stdout = origSTDOUT;
to restore it. 恢复它。
If you want to do this from within the application itself, you can change the underlying std::streambuf
of std::cout
so that any output effectively ends up somewhere else. 如果要从应用程序内部执行此操作,则可以更改
std::cout
的基础std::streambuf
,以便任何输出有效地终止于其他位置。
Example 例
std::ostringstream buffer;
std::streambuf * cout_streambuf = std::cout.rdbuf (buffer.rdbuf ()); // (A)
std::cout << "hello world\n"; // (B)
std::cout.rdbuf (orig_cout_streambuf); // (C)
std::cout
to that of buffer
, and return the old streambuf (effectively initializing cout_streambuf with this value). std::cout
的基础streambuf设置为buffer
,并返回旧的streambuf (使用该值有效地初始化cout_streambuf )。 std::ostreamstream
std::ostreamstream
Note : (B) represents the part of your application where you'd like to have a "redirected std::cout" .
注意 : (B)代表应用程序中您希望具有“重定向的std :: cout”的部分 。
Instead of messing around with the application itself you could use the facilities provided by your shell, and simply redirect the output to STDOUT to /dev/null
or equivalent (effectively discarding it) 不用搞乱应用程序本身,您可以使用外壳程序提供的功能,只需将输出到STDOUT的重定向到
/dev/null
或等效文件(有效地将其丢弃)
*NIX * NIX
./binary > /dev/null # send STDOUT to /dev/null
WINDOWS 视窗
./binary > NUL # send STDOUT to NUL
This: 这个:
std::cout.rdbuf(nulllptr);
Disables any output. 禁用任何输出。 Just add it to main.
只需将其添加到main。
You can redirect standard output to a null file. 您可以将标准输出重定向到空文件。 So, the outputs become invisible.
因此,输出变得不可见。 In Unix systems, you can run your program like this:
program_name > /dev/null
on a console. 在Unix系统中,您可以像在控制台上那样运行程序:
program_name > /dev/null
。 For windows, you can try program_name > nul
对于Windows,您可以尝试
program_name > nul
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.