-
redirecting stdout
Hey all,
I'm working on a scripting console for my game engine and I've run into a problem getting everything from the real console to come up on my fake one. What I need to do is have some kind of buffered connection (preferably into a string or something similar) between stdout and my own acceptString function (ideally sending a string for every line that comes in from stdout). The solution I have to this right now works a little but not completely. Here's the python code that does it:
Code:
class RedirectStdout:
def __init__(self):
self.oldstdout = sys.stdout
def __del__(self):
sys.stdout = self.oldstdout
def write(self, s):
if s.strip() != "":
self.oldstdout.write(s + "\n")
gfxServer.appendString(s)
sys.stdout = RedirectStdout()
The problem with this is that it only redirects some of the python output. What I need is something similar in C++ that will work at a lower level and hopefully catch all of the output from stdout.
-
Look at rdbuf(). You can set the object associated with a stream.
-
This windows specific code executes without error but does not actually redirect anything. It confuses me as to why.
Code:
HANDLE hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hFile = ::CreateFile
("C:\\abc.txt", GENERIC_WRITE,
FILE_SHARE_READ, 0, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
if(::SetStdHandle(STD_OUTPUT_HANDLE, hFile))
{
std::cout << "Testing\n";
}
else
std::cout << "Failed\n";
::SetStdHandle(STD_OUTPUT_HANDLE, hStdOut);
::CloseHandle(hFile);
}
else
std::cout << "INVALID_HANDLE_VALUE: " << ::GetLastError() << std::endl;