我可以访问一个"好东西"的第三方库.它向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'就足够了.对于更精美的应用程序...我想写的是什么......创建线程会有一些额外的工作,等等.