我可以访问一个"好东西"的第三方库.它向stdout发出状态和进度消息.在控制台应用程序中,我可以很好地看到这些消息.在Windows应用程序中,他们只是转到位桶.
有没有一种相当简单的方法将stdout和stderr重定向到文本控件或其他可见的位置.理想情况下,这不需要重新编译第三方代码.它只是在低水平拦截蒸汽.我想要一个解决方案,我只需#include标头,调用初始化函数并链接库,如...
#include "redirectStdFiles.h"
void function(args...)
{
TextControl* text = new TextControl(args...);
initializeRedirectLibrary(text, ...);
printf("Message that will show up in the TextControl\n");
std::cout << "Another message that also shows up in TextControl\n";
}
Run Code Online (Sandbox Code Playgroud)
更好的是,如果它使用了一些我可以覆盖的接口,那么它就不会绑定到任何特定的GUI库.
class StdFilesRedirector
{
public:
writeStdout(std::string const& message) = 0;
writeStderr(std::string const& errorMessage) = 0;
readStdin(std::string &putReadStringHere) = 0;
};
Run Code Online (Sandbox Code Playgroud)
我只是在做梦吗?或者有人知道可以做这样的事情吗?
在两个答案后编辑:我认为使用freopen重定向文件是一个很好的第一步.要获得完整的解决方案,需要创建一个新线程来读取文件并显示输出.对于调试,在cygwin shell窗口中执行'tail -f'就足够了.对于更精美的应用程序...我想写的是什么......创建线程会有一些额外的工作,等等.
我正在使用一个共享库,其功能是在各处执行std :: cout.是否可以在呼叫者级别执行任何操作,我可以抑制cout outout或将其重定向到某个位置?
是否有可能在c ++中尝试这样的事情.
从这个问题:将函数输出重定向到/ dev / null我尝试使用以下代码:
std::ofstream catchPCLStream("/dev/null");
std::streambuf *originalOutputBuffer = std::cout.rdbuf();
std::cout.rdbuf(catchPCLStream.rdbuf());
std::cerr.rdbuf(catchPCLStream.rdbuf());
icp_.align(dataCloudTransformedByIcp_, icpInternalUpdatePose_);
std::cout.rdbuf(originalOutputBuffer);
std::cerr.rdbuf(originalOutputBuffer);
Run Code Online (Sandbox Code Playgroud)
但是我仍然从注册库中获得大量输出:
[pcl::IterativeClosestPoint::computeTransformation] Not enough correspondences found. Relax your threshold parameters.
Run Code Online (Sandbox Code Playgroud)
此输出是否有其他与众不同之处,可以阻止它被此捕获?是不是去cout还是cerr?