自定义C ++ cout类 - 输出到控制台和日志文件
我正在使用一个程序,大量使用cout<< strSomething;以将信息记录到控制台。我需要修改程序,以便所有控制台输出到控制台和文件。虽然我可以修改cout
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
.