Wildmeshing Toolkit
Loading...
Searching...
No Matches
Stopwatch.cpp
Go to the documentation of this file.
1#include "Stopwatch.hpp"
2
3
4#include <spdlog/common.h>
5
6namespace wmtk::utils {
7
8
9StopWatch::StopWatch(const std::string& name)
10 : StopWatch(name, spdlog::level::info)
11{}
12
13StopWatch::StopWatch(const std::string& name, const spdlog::level::level_enum log_level)
14 : m_name(name)
15 , m_log_level(log_level)
16{
17 start();
18}
19
20StopWatch::~StopWatch()
21{
22 if (m_is_running) {
23 stop();
24 log_msg();
25 }
26}
27
28void StopWatch::start()
29{
30 m_is_running = true;
31 m_start = std::chrono::high_resolution_clock::now();
32}
33
34void StopWatch::stop()
35{
36 if (!m_is_running) {
37 return;
38 }
39 m_is_running = false;
40 m_stop = std::chrono::high_resolution_clock::now();
41}
42
43inline void StopWatch::log_msg()
44{
45 int64_t h = getElapsedTime<std::chrono::hours>();
46 int64_t m = getElapsedTime<std::chrono::minutes>() % 60;
47 int64_t s = getElapsedTime<std::chrono::seconds>() % 60;
48 int64_t ms = getElapsedTime<std::chrono::milliseconds>() % 1000;
49
50 logger().log(
52 "[runtime h:m:s.ms] {}: {:02d}:{:02d}:{:02d}.{:03d}",
53 m_name,
54 h,
55 m,
56 s,
57 ms);
58}
59
60} // namespace wmtk::utils
std::chrono::high_resolution_clock::time_point m_stop
Definition Stopwatch.hpp:31
std::chrono::high_resolution_clock::time_point m_start
Definition Stopwatch.hpp:30
spdlog::level::level_enum m_log_level
Definition Stopwatch.hpp:29
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:58