ctime() and new line

Hello,

I'm writing a small logging class and I want to date/time stamp each line of output. I'm using ctime() for this, but it adds a new line /n at the end. I need to stop it from doing that as it's making my log a complete mess.

It does this:

1
2
| Wed Jun 02 19:01:41 2010
 | CORE_LAYER | Initialising Sub-Systems.


I need it to do this:

 
| Wed Jun 02 19:01:41 2010 | CORE_LAYER | Initialising Sub-Systems.


I've tried messily hacking it, but to no avail.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void irisLogger::setLine(std::string sLine)
{
#ifdef _DEBUG
	time_t raw;
	time(&raw);

	std::ostringstream sTime;
	sTime << ctime(&raw);
	std::string sTimeS = sTime.str();

	size_t      end      = sTimeS.find_last_of("\\");
	sTimeS               = sTimeS.substr(end + 1);

	std::ostringstream sBuff;
	sBuff << "| " << sTimeS << " | " << sLine;

	logFile << sBuff.str() << std::endl;
#endif
}


That didn't work :). Does anybody know how I can do this correctly?

Thanks.
1
2
std::string t( ctime( &raw ) );
sTime << t.substr( 0, t.length() -1  );

You probably meant:

1
2
size_t      end      = sTimeS.find_last_of('\n');
sTimeS               = sTimeS.substr(0,end);


Or since you already know that the last char is always a newline:
sTimeS = sTimeS.substr(0,sTimeS.length()-1);
Yes, that works. Thanks guys. :)
Topic archived. No new replies allowed.