Apr 11, 2015 at 12:11pm UTC
Hey there,
So, I've worked a little bit with the std::ostream objects and I know how to redirect std::cerr and std::cout to files but I can't find any information on how to redirect std::clog in a file. For some reason the log-stream gets redirected with the error-stream automatically.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int main(void )
{
std::cerr << "err" << std::endl;
std::clog << "log" << std::endl;
std::cout << "out" << std::endl;
return 0;
}
When i compile and do:
test.exe 2> errors.txt
this is the content of the errors.txt file:
Did I do something wrong?
I'm using Windows 8.1 if that's important
Last edited on Apr 11, 2015 at 12:12pm UTC
Apr 11, 2015 at 2:33pm UTC
Both std::clog and std::cerr output to stderr. The difference is that std::cerr automatically flush all output as soon as it is written.
Apr 11, 2015 at 3:02pm UTC
Ah so i can use std::clog.rdbuf(); to read the logging information?
Apr 11, 2015 at 10:44pm UTC
std::clog is an output stream only; you can't read from it. However, you can redirect where it goes by replacing the stream buffer.
Apr 12, 2015 at 7:50pm UTC
Okey, good to know...
I have swapped the streambuffer of clog to an other stream (an ofstream).
Peter87 mentioned that std::cerr is automatically flushed after writing something in there.
How can i modify a buffer to flush automatically?
Last edited on Apr 12, 2015 at 7:52pm UTC
Apr 12, 2015 at 8:30pm UTC
Thank you, it works perfectly :D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <fstream>
#include <sstream>
class log_stream : public std::ofstream
{
public :
template <typename T>
void operator <<(const T& data) { std::ofstream::operator <<(data); std::ofstream::flush(); }
static log_stream& get() { static log_stream ls; return ls; }
private :
log_stream() : std::ofstream("log_file.txt" ) {}
log_stream(const log_stream&) = delete ;
void operator =(const log_stream&) = delete ;
};
int main(void )
{
std::clog.rdbuf(log_stream::get().rdbuf());
std::cerr << "err" << std::endl;
std::clog << "log" << std::endl;
std::cout << "out" << std::endl;
return 0;
}
Last edited on Apr 12, 2015 at 8:30pm UTC
Apr 12, 2015 at 8:36pm UTC
In that code, defining your own streambuffer class is overkill because you don't make it do anything that ofstream's streambuffer doesn't already do.
Apr 12, 2015 at 8:50pm UTC
std::clog is the standard output stream for logging, right?
I wanted to redirect that stream to a file and automatically flush it, how would you do that?
Apr 12, 2015 at 8:54pm UTC
does std::ofstream flush automatically?
Apr 12, 2015 at 9:00pm UTC
All streams flush automatically. Only std::cerr writes directly to the console and skips the whole concept of flushing entirely.
Apr 12, 2015 at 9:33pm UTC
Okey thank you, still thanks, I learned some things about streams today :)