Agora  1.2.0
Agora project
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 
27 namespace spdlog {
28 class stopwatch
29 {
30  using clock = std::chrono::steady_clock;
31  std::chrono::time_point<clock> start_tp_;
32 
33 public:
36  {}
37 
38  std::chrono::duration<double> elapsed() const
39  {
40  return std::chrono::duration<double>(clock::now() - start_tp_);
41  }
42 
43  void reset()
44  {
46  }
47 };
48 } // namespace spdlog
49 
50 // Support for fmt formatting (e.g. "{:012.9}" or just "{}")
51 namespace
52 #ifdef SPDLOG_USE_STD_FORMAT
53  std
54 #else
55  fmt
56 #endif
57 {
58 
59 template<>
60 struct formatter<spdlog::stopwatch> : formatter<double>
61 {
62  template<typename FormatContext>
63  auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
64  {
65  return formatter<double>::format(sw.elapsed().count(), ctx);
66  }
67 };
68 } // namespace std
spdlog::stopwatch::stopwatch
stopwatch()
Definition: stopwatch.h:34
spdlog::stopwatch::elapsed
std::chrono::duration< double > elapsed() const
Definition: stopwatch.h:38
spdlog::stopwatch::reset
void reset()
Definition: stopwatch.h:43
spdlog::details::os::now
SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT
Definition: os-inl.h:71
spdlog::stopwatch::clock
std::chrono::steady_clock clock
Definition: stopwatch.h:30
spdlog::stopwatch
Definition: stopwatch.h:28
spdlog
Definition: async.h:25
fmt::v8::formatter
Definition: core.h:707
spdlog::stopwatch::start_tp_
std::chrono::time_point< clock > start_tp_
Definition: stopwatch.h:31
std
Definition: json.hpp:5213
fmt
Definition: bin_to_hex.h:102
fmt::formatter< spdlog::stopwatch >::format
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
Definition: stopwatch.h:63
fmt.h
utils::format
std::string format(const T &value)
Definition: utils.h:15