Hi all,
i would like to create a flexible logger class. I want it to be able to output data to a file or to standard output. Also, i want to use streams. The class should look something like:
class Logger
{
private:
std::ostream m_out; // or ofstream, iostream? i don't know
public:
void useFile( std::string fname);
void useStdOut();
void log( symbol_id si, int val );
void log( symbol_id si, std::string str );
//etc..
};
The symbol_id
is an enum and defines the formatting. What i want to achieve is to be able to easily switch from standart output to a file and vice versa (this is the purpose of the use*
methods). Preferably by just using m_out
and simply writing m_out << "something";
without any checks whether i want to write to a file or stdout.
I know there are many ways how to get around this (using if's
to test if i want to write to a file or stdout, the "C way" (using FILE*
and fprintf
)) and so on, but i'm sure there must be a way how to achieve this with C++ streams in a nice way. But i can't seem to find the way how to do it. Can somebody help me please?