// This file is part of the Coral project.
// Please read the copyright notice in the LICENSE file.

#include "timedebug.h"
#include "debugstream.h"

#include <iostream>

NAMESPACE_BEGIN(coral);

timedebug::timedebug(std::ostream& out, const std::string& name)
	: m_out(out), m_name(name) {
	gettimeofday(&m_start, NULL);
	m_previous = m_start;
}

timedebug::~timedebug() {
	mark("Final time-keeping");
	m_previous = m_start;
	mark("Ending time-keeping");
}

void timedebug::mark2(const std::string& comment,
		      const struct timeval& past,
		      const struct timeval& now) {
	double seconds = static_cast<double>(now.tv_sec - past.tv_sec)
		+ static_cast<double>(now.tv_usec - past.tv_usec) / 1000000.0f;
	m_out << DEBUGLEVEL_DEBUG
	      << "DEBUG TIME " << m_name << ": " << comment << " "
	      << seconds << " seconds" << std::endl;
}

void timedebug::mark(const std::string& comment) {
	struct timeval now;
	gettimeofday(&now, NULL);
	mark2(comment, m_previous, now);
	m_previous = now;
}

NAMESPACE_END();


