spdlog
Loading...
Searching...
No Matches
stopwatch.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#include <spdlog/fmt/fmt.h>
7
8// Stopwatch support for spdlog (using std::chrono::steady_clock).
9// Displays elapsed seconds since construction as double.
10//
11// Usage:
12//
13// spdlog::stopwatch sw;
14// ...
15// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds"
16// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds"
17//
18//
19// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use "duration_cast<..>(sw.elapsed())":
20//
21// #include <spdlog/fmt/chrono.h>
22//..
23// using std::chrono::duration_cast;
24// using std::chrono::milliseconds;
25// spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
26
27namespace spdlog {
29{
32
33public:
35 : start_tp_{clock::now()}
36 {}
37
42
43 void reset()
44 {
45 start_tp_ = clock ::now();
46 }
47};
48} // namespace spdlog
49
50// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
51namespace fmt {
52template<>
53struct formatter<spdlog::stopwatch> : formatter<double>
54{
55 template<typename FormatContext>
56 auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
57 {
58 return formatter<double>::format(sw.elapsed().count(), ctx);
59 }
60};
61} // namespace fmt
std::chrono::time_point< clock > start_tp_
Definition stopwatch.h:31
std::chrono::duration< double > elapsed() const
Definition stopwatch.h:38
Definition async.h:25
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
Definition stopwatch.h:56