CatapultServer  v0.5.0.1 (Elephant)
StackLogger.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Logging.h"
23 #include "StackTimer.h"
24 #include "TimeSpan.h"
25 
26 namespace catapult { namespace utils {
27 
29  class StackLogger {
30  public:
32  StackLogger(const char* message, LogLevel level)
33  : m_message(message)
34  , m_level(level) {
35  CATAPULT_LOG_LEVEL(m_level) << "pushing scope '" << m_message << "'";
36  }
37 
40  auto elapsedMillis = m_timer.millis();
41  CATAPULT_LOG_LEVEL(m_level) << "popping scope '" << m_message << "' (" << elapsedMillis << "ms)";
42  }
43 
44  private:
45  const char* m_message;
48  };
49 
52  public:
54  SlowOperationLogger(const char* message, LogLevel level, const TimeSpan& threshold = TimeSpan::FromSeconds(1))
55  : m_message(message)
56  , m_level(level)
57  , m_threshold(threshold)
58  {}
59 
62  auto elapsedMillis = m_timer.millis();
63  if (TimeSpan::FromMilliseconds(elapsedMillis) > m_threshold)
64  CATAPULT_LOG_LEVEL(m_level) << "slow operation detected: '" << m_message << "' (" << elapsedMillis << "ms)";
65  }
66 
67  private:
68  const char* m_message;
72  };
73 }}
catapult::utils::SlowOperationLogger::m_level
LogLevel m_level
Definition: StackLogger.h:69
catapult::utils::StackLogger::~StackLogger
~StackLogger()
Destructor.
Definition: StackLogger.h:39
catapult::utils::SlowOperationLogger::~SlowOperationLogger
~SlowOperationLogger()
Destructor.
Definition: StackLogger.h:61
catapult::utils::StackLogger::StackLogger
StackLogger(const char *message, LogLevel level)
Constructs a logger with a message and log level.
Definition: StackLogger.h:32
catapult::utils::TimeSpan::FromMilliseconds
static constexpr TimeSpan FromMilliseconds(uint64_t milliseconds)
Creates a time span from the given number of milliseconds.
Definition: TimeSpan.h:59
catapult::utils::TimeSpan
Represents a time duration.
Definition: TimeSpan.h:30
catapult::utils::TimeSpan::FromSeconds
static constexpr TimeSpan FromSeconds(uint64_t seconds)
Creates a time span from the given number of seconds.
Definition: TimeSpan.h:54
catapult::utils::SlowOperationLogger
Simple RAII class that logs slow operation messages.
Definition: StackLogger.h:51
catapult::utils::LogLevel
LogLevel
Catapult log levels.
Definition: Logging.h:36
catapult::utils::SlowOperationLogger::m_timer
StackTimer m_timer
Definition: StackLogger.h:71
catapult::utils::StackLogger::m_level
LogLevel m_level
Definition: StackLogger.h:46
catapult::utils::StackLogger
Simple RAII class that logs scope messages.
Definition: StackLogger.h:29
CATAPULT_LOG_LEVEL
#define CATAPULT_LOG_LEVEL(LEVEL)
Writes a log entry to the default logger with LEVEL severity.
Definition: Logging.h:336
catapult::utils::StackLogger::m_message
const char * m_message
Definition: StackLogger.h:45
TimeSpan.h
catapult::utils::StackTimer
Stack based timer.
Definition: StackTimer.h:27
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::SlowOperationLogger::m_threshold
TimeSpan m_threshold
Definition: StackLogger.h:70
Logging.h
StackTimer.h
catapult::utils::StackLogger::m_timer
StackTimer m_timer
Definition: StackLogger.h:47
catapult::utils::SlowOperationLogger::SlowOperationLogger
SlowOperationLogger(const char *message, LogLevel level, const TimeSpan &threshold=TimeSpan::FromSeconds(1))
Constructs a logger with a message and log level for messages longer than threshold.
Definition: StackLogger.h:54
catapult::utils::StackTimer::millis
uint64_t millis() const
Gets the number of elapsed milliseconds since this logger was created.
Definition: StackTimer.h:38
catapult::utils::SlowOperationLogger::m_message
const char * m_message
Definition: StackLogger.h:68