From 8738192476cd5bf684cd81f92b5128cb3b9d5e3e Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 23 Jul 2015 17:37:43 +1000 Subject: [PATCH] log: add scoped timer --- log.cpp | 20 ++++++++++++++++++++ log.hpp | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/log.cpp b/log.cpp index a4798688..9f197995 100644 --- a/log.cpp +++ b/log.cpp @@ -19,6 +19,7 @@ #include "debug.hpp" #include "types.hpp" +#include "time.hpp" #include #include @@ -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); +} diff --git a/log.hpp b/log.hpp index 7f32602a..037f09ef 100644 --- a/log.hpp +++ b/log.hpp @@ -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"