I’m writing a logger for my program in C++.
I have this piece of code:
void Log::Write(char* logline)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
m_stream << asctime (timeinfo) << logline << endl;
}
This is a log
Tue Oct 11 13:07:28 2011
I want a different output on a single line like this:
Tue Oct 11 13:07:28 2011 – This is a log
How can I do it?
Thank you!
Your problem is asctime():
http://www.cplusplus.com/reference/clibrary/ctime/asctime/
As the returned string is a C string, you could replace the \n with a \0:
AS long as you don’t call ctime or asctime again, the content won’t be overwritten.