log: add scoped timer

This commit is contained in:
Danny Robson 2015-07-23 17:37:43 +10:00
parent ea953ec024
commit 8738192476
2 changed files with 32 additions and 0 deletions

20
log.cpp
View File

@ -19,6 +19,7 @@
#include "debug.hpp"
#include "types.hpp"
#include "time.hpp"
#include <array>
#include <ctime>
@ -158,3 +159,22 @@ scoped_logger::~scoped_logger ()
{
log (m_level, m_message);
}
///////////////////////////////////////////////////////////////////////////////
scoped_timer::scoped_timer (level_t _level,
std::string &&_message):
m_level (_level),
m_message (_message),
m_start (util::nanoseconds ())
{ ; }
//-----------------------------------------------------------------------------
scoped_timer::~scoped_timer ()
{
auto finish = util::nanoseconds ();
auto duration = finish - m_start;
log (m_level, "%fs, %s", duration / 1'000'000'000.f, m_message);
}

12
log.hpp
View File

@ -87,6 +87,18 @@ namespace util {
std::string m_message;
};
///////////////////////////////////////////////////////////////////////////
class scoped_timer : public nocopy {
public:
scoped_timer (const level_t, std::string&&);
~scoped_timer ();
private:
const level_t m_level;
const std::string m_message;
uint64_t m_start;
};
}
#include "log.ipp"