RSTools 3.1.0
Versatile C++20 utility library
Loading...
Searching...
No Matches
ScopedTimer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "RST/log/Log.hpp"
5
6#include <chrono>
7#include <memory>
8#include <string>
9#include <utility>
10
11namespace RST::Log {
12
17 public:
18 using Clock = std::chrono::steady_clock;
19 using Duration = std::chrono::milliseconds;
20
22 ScopedTimer(std::string name, std::shared_ptr<Logger> logger = nullptr, Duration warnThreshold = Duration(-1), LogLevel level = LogLevel::Debug, SourceLocation source = {})
23 : _name(std::move(name)),
24 _logger(logger ? std::move(logger) : Registry::defaultLogger()),
25 _warnThreshold(warnThreshold),
26 _level(level),
27 _source(source),
28 _start(Clock::now())
29 {}
30
32 {
33 const Duration took = elapsed();
34 const bool slow = _warnThreshold.count() >= 0 && took >= _warnThreshold;
35 _logger->log(slow ? LogLevel::Warn : _level, _source, "{}: {}ms{}", _name, took.count(), slow ? " [SLOW]" : "");
36 }
37
38 ScopedTimer(const ScopedTimer&) = delete;
40
41 [[nodiscard]] Duration elapsed() const
42 {
43 return std::chrono::duration_cast<Duration>(Clock::now() - _start);
44 }
45
46 private:
47 std::string _name;
48 std::shared_ptr<Logger> _logger;
49 Duration _warnThreshold;
50 LogLevel _level;
51 SourceLocation _source;
52 Clock::time_point _start;
53 };
54
55}
56
57#define RST_LOG_CONCAT_IMPL(a, b) a##b
58#define RST_LOG_CONCAT(a, b) RST_LOG_CONCAT_IMPL(a, b)
59
61#define RST_LOG_SCOPE_TIME(logger, name) \
62 ::RST::Log::ScopedTimer RST_LOG_CONCAT(rstScopedTimer, __LINE__)(name, logger, \
63 ::RST::Log::ScopedTimer::Duration(-1), ::RST::Log::LogLevel::Debug, RST_SOURCE_LOCATION)
64
66#define RST_LOG_SCOPE_TIME_DEFAULT(name) RST_LOG_SCOPE_TIME(nullptr, name)
67
69#define RST_LOG_SCOPE_TIME_WARN(logger, name, thresholdMs) \
70 ::RST::Log::ScopedTimer RST_LOG_CONCAT(rstScopedTimer, __LINE__)(name, logger, \
71 std::chrono::milliseconds(thresholdMs), ::RST::Log::LogLevel::Debug, RST_SOURCE_LOCATION)
72
73#if defined(RST_LOG_LEGACY_MACROS)
74# define LOG_SCOPE_TIME(logger, name) RST_LOG_SCOPE_TIME(logger, name)
75# define LOG_SCOPE_TIME_DEFAULT(name) RST_LOG_SCOPE_TIME_DEFAULT(name)
76# define LOG_SCOPE_TIME_WARN(logger, name, thresholdMs) RST_LOG_SCOPE_TIME_WARN(logger, name, thresholdMs)
77#endif
Global registry of named loggers, including the default logger used by the RST_LOG_* macros.
Definition LogRegistry.hpp:20
Logs the time spent in a scope when destroyed, usually through RST_LOG_SCOPE_TIME().
Definition ScopedTimer.hpp:16
ScopedTimer(const ScopedTimer &)=delete
ScopedTimer & operator=(const ScopedTimer &)=delete
Duration elapsed() const
Definition ScopedTimer.hpp:41
ScopedTimer(std::string name, std::shared_ptr< Logger > logger=nullptr, Duration warnThreshold=Duration(-1), LogLevel level=LogLevel::Debug, SourceLocation source={})
Starts timing; logger defaults to the default logger and a negative warnThreshold disables the warnin...
Definition ScopedTimer.hpp:22
std::chrono::milliseconds Duration
Definition ScopedTimer.hpp:19
~ScopedTimer()
Definition ScopedTimer.hpp:31
std::chrono::steady_clock Clock
Definition ScopedTimer.hpp:18
Thread-safe logging: loggers, sinks, patterns and macros.
Definition Terminal.hpp:8
LogLevel
Severity of a message, from Trace to Fatal; Off disables logging.
Definition LogLevel.hpp:11
File, function and line of a log call.
Definition SourceLocation.hpp:6