コード例 #1
0
ファイル: MojTimeTest.cpp プロジェクト: BigBlueHat/db8
MojErr MojTimeTest::run()
{
	MojTimevalT tv;
	tv.tv_sec = 400;
	tv.tv_usec = 54321;
	MojTime time = -8;
	time.fromTimeval(&tv);
	MojTestAssert(time == 400054321);
	MojTestAssert(time.secs() == 400);
	MojTestAssert(time.millisecs() == 400054);
	MojTestAssert(time.microsecs() == 400054321);
	MojTestAssert(time.millisecsPart() == 54);
	MojTestAssert(time.microsecsPart() == 54321);
	MojTimevalT tv2;
	time.toTimeval(&tv2);
	MojTestAssert(tv.tv_sec == tv2.tv_sec && tv.tv_usec == tv2.tv_usec);
	MojTimespecT ts;
	ts.tv_sec = 400;
	ts.tv_nsec = 54321;
	time.fromTimespec(&ts);
	MojTestAssert(time == 400000054);
	MojTimespecT ts2;
	time.toTimespec(&ts2);
	MojTestAssert(ts2.tv_sec = 400 && ts2.tv_nsec == 54000);

	time = MojSecs(3);
	MojTestAssert(time == 3000000);
	time = MojMillisecs(45);
	MojTestAssert(time == 45000);
	time = MojMicrosecs(5);
	MojTestAssert(time == 5);
	time = MojSecs(1) + MojMillisecs(2) + MojMicrosecs(3);
	MojTestAssert(time == 1002003);

	time = 1;
	MojTestAssert(time++ == 1);
	MojTestAssert(time == 2);
	MojTestAssert(++time == 3);
	MojTestAssert(--time == 2);
	MojTestAssert(time-- == 2);
	MojTestAssert(time == 1);
	MojTestAssert((time += 2) == 3);
	MojTestAssert((time -= 4) == -1);
	MojTestAssert((time *= -10) == 10);
	MojTestAssert((time /= 2) == 5);
	time = MojTime(1) + MojTime(2);
	MojTestAssert(time == 3);
	time = MojTime(8) - MojTime(6);
	MojTestAssert(time == 2);
	time = MojTime(8) * MojTime(6);
	MojTestAssert(time == 48);
	time = MojTime(8) / MojTime(4);
	MojTestAssert(time == 2);

	return MojErrNone;
}
コード例 #2
0
ファイル: MojLogEngine.cpp プロジェクト: ctbrowser/db8
MojErr MojFileAppender::append(MojLogger::Level level, MojLogger* logger, const MojChar* format, va_list args)
{
	MojAssertNoLog(format);
	MojAssertNoLog(m_file != MojInvalidFile);

	// get current time
	MojTime time = 0;
	MojErr err = MojGetCurrentTime(time);
	MojErrCheckNoLog(err);
	MojTmT tm;
	err = MojLocalTime(time, tm);
	MojErrCheckNoLog(err);

	// format message
	err =  m_buf.format(_T("[%04d-%02d-%02d %02d:%02d:%02d:%03d] [%p] [%s]"),
			tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
			tm.tm_min, tm.tm_sec, time.millisecsPart(),
			(void*) (MojIntPtr) MojThreadCurrentId(),
			MojLogger::stringFromLevel(level));
	MojErrCheckNoLog(err);
	if (logger) {
		err = m_buf.appendFormat(_T(" [%s]: "), logger->name());
		MojErrCheckNoLog(err);
	} else {
		err = m_buf.append(_T(": "));
		MojErrCheckNoLog(err);
	}
	err =  m_buf.appendVFormat(format, args);
	MojErrCheckNoLog(err);
	err = m_buf.append(_T('\n'));
	MojErrCheckNoLog(err);

	// append to file
	const MojByte* begin = (const MojByte*) m_buf.begin();
	const MojByte* end = (const MojByte*) m_buf.end();
	while (begin < end) {
		MojSize written = 0;
		err = MojFileWrite(m_file, begin, end - begin, written);
		MojErrCheckNoLog(err);
		begin += written;
	}
	return MojErrNone;
}