mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-31 09:39:15 +08:00
83c9ede9e6
Some checks failed
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[asan:ON build_type:Debug … (push) Has been cancelled
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Debug compiler… (push) Has been cancelled
linux / ${{ matrix.config.compiler}} ${{ matrix.config.version }} (C++${{ matrix.config.cppstd }} ${{ matrix.config.build_type }} ${{ matrix.config.asan == 'ON' && 'ASAN' || '' }}${{ matrix.config.tsan == 'ON' && 'TSAN' || '' }}) (map[build_type:Release compil… (push) Has been cancelled
macos / macOS Clang (C++17, Release) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:OFF BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Has been cancelled
windows / build (map[BUILD_EXAMPLE:ON BUILD_SHARED:OFF BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 17 2022]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:17 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019]) (push) Has been cancelled
windows / build_2019 (map[BUILD_EXAMPLE:ON BUILD_SHARED:ON BUILD_TYPE:Release CXX_STANDARD:20 FATAL_ERRORS:ON GENERATOR:Visual Studio 16 2019]) (push) Has been cancelled
Replace async logger with async sink
61 lines
1.8 KiB
C++
61 lines
1.8 KiB
C++
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
#pragma once
|
|
|
|
#include <chrono>
|
|
|
|
#include "fmt/base.h"
|
|
|
|
// Stopwatch support for spdlog (using std::chrono::steady_clock).
|
|
// Displays elapsed seconds since construction as double.
|
|
//
|
|
// Usage:
|
|
//
|
|
// spdlog::stopwatch sw;
|
|
// ...
|
|
// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds"
|
|
// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds"
|
|
//
|
|
//
|
|
// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use
|
|
// "duration_cast<..>(sw.elapsed())":
|
|
//
|
|
// #include "spdlog/fmt/chrono.h"
|
|
//..
|
|
// using std::chrono::duration_cast;
|
|
// using std::chrono::milliseconds;
|
|
// spdlog::info("Elapsed {}", duration_cast<milliseconds>(sw.elapsed())); => "Elapsed 5ms"
|
|
|
|
namespace spdlog {
|
|
class stopwatch {
|
|
using clock = std::chrono::steady_clock;
|
|
std::chrono::time_point<clock> start_tp_;
|
|
|
|
public:
|
|
stopwatch()
|
|
: start_tp_{clock::now()} {}
|
|
|
|
[[nodiscard]]
|
|
std::chrono::duration<double> elapsed() const {
|
|
return std::chrono::duration<double>(clock::now() - start_tp_);
|
|
}
|
|
|
|
[[nodiscard]]
|
|
std::chrono::milliseconds elapsed_ms() const {
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start_tp_);
|
|
}
|
|
|
|
void reset() { start_tp_ = clock::now(); }
|
|
};
|
|
} // namespace spdlog
|
|
|
|
// Support for fmt formatting (e.g. "{:012.9}" or just "{}")
|
|
template <>
|
|
struct fmt::formatter<spdlog::stopwatch> : formatter<double> {
|
|
template <typename FormatContext>
|
|
auto format(const spdlog::stopwatch &sw, FormatContext &ctx) const -> decltype(ctx.out()) {
|
|
return formatter<double>::format(sw.elapsed().count(), ctx);
|
|
}
|
|
}; // namespace fmt
|