18 using Clock = std::chrono::steady_clock;
23 : _name(std::move(name)),
24 _logger(logger ? std::move(logger) :
Registry::defaultLogger()),
25 _warnThreshold(warnThreshold),
34 const bool slow = _warnThreshold.count() >= 0 && took >= _warnThreshold;
35 _logger->log(slow ?
LogLevel::Warn : _level, _source,
"{}: {}ms{}", _name, took.count(), slow ?
" [SLOW]" :
"");
43 return std::chrono::duration_cast<Duration>(Clock::now() - _start);
48 std::shared_ptr<Logger> _logger;
52 Clock::time_point _start;
57#define RST_LOG_CONCAT_IMPL(a, b) a##b
58#define RST_LOG_CONCAT(a, b) RST_LOG_CONCAT_IMPL(a, b)
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)
66#define RST_LOG_SCOPE_TIME_DEFAULT(name) RST_LOG_SCOPE_TIME(nullptr, name)
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)
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)
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