CatapultServer  v0.5.0.1 (Elephant)
ThrottleLogger.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Logging.h"
23 #include "SpinLock.h"
24 #include <chrono>
25 
26 namespace catapult { namespace utils {
27 
31  private:
32  using Clock = std::chrono::steady_clock;
33 
34  public:
36  explicit ThrottleLogger(uint64_t throttleMillis)
37  : m_throttleMillis(throttleMillis)
38  , m_counter(0)
39  {}
40 
41  public:
43  uint32_t counter() const {
44  return m_counter;
45  }
46 
47  public:
49  bool isThrottled() {
50  ++m_counter;
51 
52  SpinLockGuard guard(m_lock);
53  auto now = Clock::now();
54  if (Clock::time_point() != m_last && millis(now) < m_throttleMillis)
55  return true;
56 
57  m_last = now;
58  return false;
59  }
60 
61  private:
62  uint64_t millis(const Clock::time_point& now) const {
63  auto elapsedDuration = now - m_last;
64  return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsedDuration).count());
65  }
66 
67  private:
68  uint64_t m_throttleMillis;
69  std::atomic<uint32_t> m_counter; // atomic to allow increment / access outside of m_lock
70  Clock::time_point m_last; // protected by m_lock
72  };
73 
76 #define CATAPULT_LOG_THROTTLE(LEVEL, THROTTLE_MILLIS) \
77  static utils::ThrottleLogger BOOST_LOG_UNIQUE_IDENTIFIER_NAME_INTERNAL(throttle_logger, __LINE__)(THROTTLE_MILLIS); \
78  if (!BOOST_LOG_UNIQUE_IDENTIFIER_NAME_INTERNAL(throttle_logger, __LINE__).isThrottled()) \
79  CATAPULT_LOG(LEVEL) << "[" << BOOST_LOG_UNIQUE_IDENTIFIER_NAME_INTERNAL(throttle_logger, __LINE__).counter() << " log count] "
80 }}
SpinLock.h
catapult::utils::ThrottleLogger::m_counter
std::atomic< uint32_t > m_counter
Definition: ThrottleLogger.h:69
catapult::utils::ThrottleLogger::Clock
std::chrono::steady_clock Clock
Definition: ThrottleLogger.h:32
catapult::utils::ThrottleLogger::m_last
Clock::time_point m_last
Definition: ThrottleLogger.h:70
catapult::utils::ThrottleLogger::m_throttleMillis
uint64_t m_throttleMillis
Definition: ThrottleLogger.h:68
catapult::utils::ThrottleLogger::m_lock
utils::SpinLock m_lock
Definition: ThrottleLogger.h:71
catapult::utils::SpinLock
Definition: SpinLock.h:31
catapult::utils::ThrottleLogger
Definition: ThrottleLogger.h:30
catapult::utils::ThrottleLogger::isThrottled
bool isThrottled()
Returns false if a log should be output.
Definition: ThrottleLogger.h:49
catapult::utils::ThrottleLogger::ThrottleLogger
ThrottleLogger(uint64_t throttleMillis)
Constructs a logger with the specified throttle in milliseconds (throttleMillis).
Definition: ThrottleLogger.h:36
catapult::utils::ThrottleLogger::millis
uint64_t millis(const Clock::time_point &now) const
Definition: ThrottleLogger.h:62
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ThrottleLogger::counter
uint32_t counter() const
Gets the total number of log attempts.
Definition: ThrottleLogger.h:43
Logging.h
catapult::utils::SpinLockGuard
std::lock_guard< SpinLock > SpinLockGuard
A spin lock guard.
Definition: SpinLock.h:60