ImFusion SDK 4.3
LoggingTimer Class Reference

#include <ImFusion/Core/Timer.h>

Scope guard-like stopwatch class to automatically log measured execution time on destruction of the guard object. More...

Detailed Description

Scope guard-like stopwatch class to automatically log measured execution time on destruction of the guard object.

See also
Timer

Example:

{
auto tmpTimer = LoggingTimer("my algorithm task 1", "component");
} // <-- the run time is logged in this line upon LoggingTimer destruction
LoggingTimer(const std::string &name="", const std::string &component="", bool start=true, Log::Level logLevel=Log::Level::Info, bool resetStatsAfterLogging=true)
Create a new LoggingTimer instance, make sure to capture it in a variable.
T sleep_for(T... args)

Also, this class can be used to log the average execution time of a code snippet without flooding the logs, as illustrated in the example below:

// when constructing the LoggingTimer, make sure to set 'start' to false to prevent starting the timer before the code we want to measure
LoggingTimer myTimer("my algorithm", "component", false);
for (int i = 0; i < 12; ++i)
{
auto scopedTimer = myTimer.startScopedTimer(std::chrono::milliseconds(500)); // this makes sure that we only log the average run time each 500 ms
}

Public Types

using msec = std::chrono::milliseconds
 

Public Member Functions

 LoggingTimer (const std::string &name="", const std::string &component="", bool start=true, Log::Level logLevel=Log::Level::Info, bool resetStatsAfterLogging=true)
 Create a new LoggingTimer instance, make sure to capture it in a variable.
 
 ~LoggingTimer ()
 destructor, also calls pause() which logs the run time
 
void resume ()
 starts or resumes the timer
 
void pause (msec logSilencePeriod=msec(0))
 it pauses the timer and logs the output unless 'logSilencePeriod' is set non zero value
 
Utils::ScopeGuard< std::function< void()> > startScopedTimer (msec logSilencePeriod=msec(0))
 returns ScopeGuard stopwatch to automatically calls pause() on destruction of the guard object.
 
void reset ()
 Stop and reset timer.
 

Constructor & Destructor Documentation

◆ LoggingTimer()

LoggingTimer ( const std::string & name = "",
const std::string & component = "",
bool start = true,
Log::Level logLevel = Log::Level::Info,
bool resetStatsAfterLogging = true )
explicit

Create a new LoggingTimer instance, make sure to capture it in a variable.

Parameters
nameName of the section, will be appended to the log message.
componentLogger component to use
startIf true then it starts the timer upon class construction
logLevelLog level to use.
resetStatsAfterLoggingIf set to true, then the stats used for the average run time calculation are reset after each time we log a message

Member Function Documentation

◆ pause()

void pause ( msec logSilencePeriod = msec(0))

it pauses the timer and logs the output unless 'logSilencePeriod' is set non zero value

Parameters
logSilencePeriodif not set to zero, then the run time is only logged once during this duration

◆ startScopedTimer()

Utils::ScopeGuard< std::function< void()> > startScopedTimer ( msec logSilencePeriod = msec(0))

returns ScopeGuard stopwatch to automatically calls pause() on destruction of the guard object.

Parameters
logSilencePeriodif not set to zero, then the run time is only logged once during this duration Example:
auto timerScope = m_timer.startScopedTimer(); // m_timer is a member variable in this case

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close