blob: ea0d9e289a13a5d4c83f566acac3f0efe72a0199 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#pragma once
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>
/*!
* \brief The elapsed_logger class - print `message: <own life time>` on destruction
*/
template<typename T = std::chrono::microseconds, int W = 8>
class elapsed_logger
{
using clk = std::chrono::high_resolution_clock;
public:
explicit elapsed_logger(const std::string &&message)
: m_start{clk::now()}
, m_message{std::move(message)}
{}
~elapsed_logger()
{
using namespace std::chrono;
const auto duration = duration_cast<T>(clk::now() - m_start);
std::cout << m_message << ": " << std::setw(W) << duration << std::endl;
}
private:
const std::chrono::time_point<clk> m_start;
const std::string m_message;
};
|