Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:44

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 <spdlog/fmt/fmt.h>
0007 #include <chrono>
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 "duration_cast<..>(sw.elapsed())":
0021 //
0022 // #include <spdlog/fmt/chrono.h>
0023 //..
0024 // using std::chrono::duration_cast;
0025 // using std::chrono::milliseconds;
0026 // spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
0027 
0028 namespace spdlog {
0029 class stopwatch
0030 {
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 
0039     std::chrono::duration<double> elapsed() const
0040     {
0041         return std::chrono::duration<double>(clock::now() - start_tp_);
0042     }
0043 
0044     void reset()
0045     {
0046         start_tp_ = clock::now();
0047     }
0048 };
0049 } // namespace spdlog
0050 
0051 // Support for fmt formatting  (e.g. "{:012.9}" or just "{}")
0052 namespace
0053 #ifdef SPDLOG_USE_STD_FORMAT
0054     std
0055 #else
0056     fmt
0057 #endif
0058 {
0059 
0060 template<>
0061 struct formatter<spdlog::stopwatch> : formatter<double>
0062 {
0063     template<typename FormatContext>
0064     auto format(const spdlog::stopwatch &sw, FormatContext &ctx) -> decltype(ctx.out())
0065     {
0066         return formatter<double>::format(sw.elapsed().count(), ctx);
0067     }
0068 };
0069 } // namespace std