Alternatively, write debug output to standard error (with std::cerr or std::clog) and redirect it to a file. https://linux.die.net/man/1/bash (See section REDIRECTION)
Example: ./program 2> stderr.txt
To solve the problem using C++ code, the behavior of std::cout can be changed by replacing its underlying stream buffer. For example see this thread: https://cplusplus.com/forum/general/97966/
There should be a direct Windows-equivalent, tee.exe, available somewhere on the web, but the one website I found that might have a link to it seems to be down. I have it locally on my machine. Try searching for something like "unix tools tee windows".
I found the way how to output data with tee to text, but there is no terminal output string yet
Please advise
Added
I have gcc version 4.9.1, can not use streambuf_type