自定义C ++ cout类 - 输出到控制台和日志文件 [英] Custom C++ cout class - output to both console and log file
问题描述
我正在使用一个程序,大量使用cout<< strSomething;以将信息记录到控制台。我需要修改程序,以便所有控制台输出到控制台和文件。虽然我可以修改cout <在我们的代码中,有几个大的第三方库也使用cout<;那些库由于其许可证而不能被修改 - 因此修改对cout<<的所有引用不是解决方案。此外,由于执行命令行的方式,使用wtee.exe是不可能的。
I'm working on a program that makes heavy use of "cout << strSomething;" to log information to the console. I need to modify the program so that all console output goes to both the console AND a file. Although I can modify the "cout <<" in our code, there are several large third party libraries that also use "cout <<"; those libraries cannot be modified due to their licenses - so modifying all references to "cout <<" is not a solution. Also, the use of "wtee.exe" isn't possible due to the manner in which the command lines are executed.
我使用Visual Studio 2008。已在 Google网上论坛:将cout重定向到文件,这看起来很有用我想做什么。唯一的问题是代码不会编译。我得到C2248错误无法访问保护成员上 - > overflow()和 - > sync()方法调用。
I am using Visual Studio 2008. I've seen the posting at Google Groups: redirect cout to file, which appears to do EXACTLY what I want to do. The only problem is that the code won't compile. I get C2248 errors "cannot access protected member" on the ->overflow() and ->sync() method calls.
有人知道如何获取这个代码编译?
Would anyone know how to get this code to compile? Or an alternate way of redirecting cout to both console and file simultaneously?
推荐答案
sync
调用可以替换为 pubsync
。至于 overflow
调用我认为可能是一个错字。因为它看起来像是调用 sputc
。
The sync
calls can be replaced with pubsync
. As for the overflow
call I think that may be a typo. as it looks as if it should be a call to sputc
.
这篇关于自定义C ++ cout类 - 输出到控制台和日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!