Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 08:50:53

0001 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
0002 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
0003 
0004 #pragma once
0005 
0006 #include <chrono>
0007 #include <spdlog/fmt/fmt.h>
0008 
0009 // Stopwatch support for spdlog  (using std::chrono::steady_clock).
0010 // Displays elapsed seconds since construction as double.
0011 //
0012 // Usage:
0013 //
0014 // spdlog::stopwatch sw;
0015 // ...
0016 // spdlog::debug("Elapsed: {} seconds", sw);    =>  "Elapsed 0.005116733 seconds"
0017 // spdlog::info("Elapsed: {:.6} seconds", sw);  =>  "Elapsed 0.005163 seconds"
0018 //
0019 //
0020 // If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use
0021 // "duration_cast<..>(sw.elapsed())":
0022 //
0023 // #include <spdlog/fmt/chrono.h>
0024 //..
0025 // using std::chrono::duration_cast;
0026 // using std::chrono::milliseconds;
0027 // spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
0028 
0029 namespace spdlog {
0030 class stopwatch {
0031     using clock = std::chrono::steady_clock;
0032     std::chrono::time_point<clock> start_tp_;
0033 
0034 public:
0035     stopwatch()
0036         : start_tp_{clock::now()} {}
0037 
0038     std::chrono::duration<double> elapsed() const {
0039         return std::chrono::duration<double>(clock::now() - start_tp_);
0040     }
0041 
0042     std::chrono::milliseconds elapsed_ms() const {
0043         return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start_tp_);
0044     }
0045 
0046     void reset() { start_tp_ = clock::now(); }
0047 };
0048 }  // namespace spdlog
0049 
0050 // Support for fmt formatting  (e.g. "{:012.9}" or just "{}")
0051 namespace
0052 #ifdef SPDLOG_USE_STD_FORMAT
0053     std
0054 #else
0055     fmt
0056 #endif
0057 {
0058 
0059 template <>
0060 struct formatter<spdlog::stopwatch> : formatter<double> {
0061     template <typename FormatContext>
0062     auto format(const spdlog::stopwatch &sw, FormatContext &ctx) const -> decltype(ctx.out()) {
0063         return formatter<double>::format(sw.elapsed().count(), ctx);
0064     }
0065 };
0066 }  // namespace std