Hi,
I apologise if I have placed this in the wrong section of the forum, it is my first post.
I wrote the following simple class to stream the output either to console or to a log file (something like a ostream wrapper):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
class Log {
public:
// Constructors
Log ( std::ostream& );
Log ( std::ofstream&, std::string );
// Destructor
~Log ();
std::ostream& operator<<(const std::string& str)
{
return m_out << str;
}
private:
// data member
std::ostream& m_out;
};
|
Then, in the
main (or in another class) I do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int main() {
...
Log* pLog;
std::ofstream mfile;
if ( selectedLogFile ) // stream to logfile
{
mfile.open("somefile");
pLog = new Log( mfile, "somefile" );
}
else // stream to console
pLog = new Log( std::cout );
...
(*pLog) << "text1" << " text2" << "text3\n";
...
if ( file ) mfile.close();
return 0;
}
|
Up to this point everything works fine. However, in some cases I would like to stream part of my output both to logfile and console. In principle I can do this by duplicating lines in the
main, e.g.:
1 2
|
(*pLog) << "text1" << " text2" << "text3\n";
std::cout << "text1" << " text2" << "text3\n";
|
but this makes the code quite ugly. Therefore, in my
Log class I tried to replicate the stream and print it to console upon request:
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
|
class Log {
public:
// Constructors
Log ( std::ostream& ) : m_isFile(false) {};
Log ( std::ofstream&, std::string ) : m_isFile(true) {};;
// Destructor
~Log ();
std::ostream& operator<<(const std::string& str)
{
m_buffer += str;
return m_out << str;
}
// public method
void copyToConsole()
{
if (m_isFile) std::cout << m_buffer.c_str() << std::flush;
};
private:
// data members
std::ostream& m_out;
std::string m_buffer;
bool m_isFile;
};
|
Then, in the
main I did:
1 2 3
|
pLog = new Log( mfile, "somefile" );
(*pLog) << "text1," << " text2," << " text3\n";
(*pLog).copyToConsole();
|
Unfortunately, the result is:
1 2
|
logfile: text1, text2, text3
console: text1,
|
Is there a way to print text2 and text3 also to console without splitting the initial line into three lines and call copyToConsole() after each one ?