Photon 1.0.0
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#include <chrono>
8
9// Stopwatch support for spdlog (using std::chrono::steady_clock).
10// Displays elapsed seconds since construction as double.
11//
12// Usage:
13//
14// spdlog::stopwatch sw;
15// ...
16// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds"
17// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds"
18//
19//
20// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use "duration_cast<..>(sw.elapsed())":
21//
22// #include <spdlog/fmt/chrono.h>
23//..
24// using std::chrono::duration_cast;
25// using std::chrono::milliseconds;
26// spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
27
28namespace spdlog
29{
31 {
32 using clock = std::chrono::steady_clock;
33 std::chrono::time_point<clock> start_tp_;
34
35 public:
37 : start_tp_{clock::now()}
38 {
39 }
40
41 std::chrono::duration<double> elapsed() const
42 {
43 return std::chrono::duration<double>(clock::now() - start_tp_);
44 }
45
46 void reset()
47 {
48 start_tp_ = clock::now();
49 }
50 };
51} // namespace spdlog
52
53// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
54namespace
55#ifdef SPDLOG_USE_STD_FORMAT
56 std
57#else
58 fmt
59#endif
60{
61
62 template <>
63 struct formatter<spdlog::stopwatch> : formatter<double>
64 {
65 template <typename FormatContext>
66 auto format(const spdlog::stopwatch& sw, FormatContext& ctx) -> decltype(ctx.out())
67 {
68 return formatter<double>::format(sw.elapsed().count(), ctx);
69 }
70 };
71} // namespace std
Definition stopwatch.h:31
stopwatch()
Definition stopwatch.h:36
std::chrono::time_point< clock > start_tp_
Definition stopwatch.h:33
void reset()
Definition stopwatch.h:46
std::chrono::duration< double > elapsed() const
Definition stopwatch.h:41
std::chrono::steady_clock clock
Definition stopwatch.h:32
Definition bin_to_hex.h:111
Definition async.h:26
Definition uuid.h:926
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
Definition stopwatch.h:66
Definition core.h:944