|
4 | 4 | * |
5 | 5 | */ |
6 | 6 |
|
| 7 | +#include "Common/Cpp/PrettyPrint.h" |
7 | 8 | #include "Common/SerialPABotBase/SerialPABotBase_Protocol.h" |
8 | 9 | #include "ClientSource/Libraries/MessageConverter.h" |
9 | 10 | #include "BotBaseMessage.h" |
@@ -83,17 +84,45 @@ void MessageLogger::on_recv(const BotBaseMessage& message){ |
83 | 84 | SerialLogger::SerialLogger(Logger& logger, bool log_everything) |
84 | 85 | : MessageLogger(log_everything) |
85 | 86 | , m_logger(logger) |
| 87 | + , m_history(1000) |
86 | 88 | {} |
87 | 89 | void SerialLogger::log(const char* msg, Color color){ |
88 | | - m_logger.log(msg, color); |
| 90 | + if (ok_to_log()){ |
| 91 | + m_logger.log(msg, color); |
| 92 | + } |
89 | 93 | } |
90 | 94 | void SerialLogger::log(const std::string& msg, Color color){ |
91 | | - m_logger.log(msg, color); |
| 95 | + if (ok_to_log()){ |
| 96 | + m_logger.log(msg, color); |
| 97 | + } |
92 | 98 | } |
93 | 99 | void SerialLogger::log(std::string msg){ |
94 | | - m_logger.log(msg, COLOR_DARKGREEN); |
| 100 | + if (ok_to_log()){ |
| 101 | + m_logger.log(msg, COLOR_DARKGREEN); |
| 102 | + } |
95 | 103 | } |
96 | 104 |
|
| 105 | +bool SerialLogger::ok_to_log(){ |
| 106 | + WallClock now = current_time(); |
| 107 | + WriteSpinLock lg(m_lock); |
| 108 | + while (!m_history.empty()){ |
| 109 | + if (now - m_history.front() < std::chrono::seconds(1)){ |
| 110 | + break; |
| 111 | + } |
| 112 | + m_history.pop_front(); |
| 113 | + } |
| 114 | + if (!m_history.try_push_back(now)){ |
| 115 | + m_messages_dropped++; |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + if (m_messages_dropped != 0){ |
| 120 | + m_logger.log("Dropped " + tostr_u_commas(m_messages_dropped) + " message(s) due to logging rate limit.", COLOR_RED); |
| 121 | + m_messages_dropped = 0; |
| 122 | + } |
| 123 | + |
| 124 | + return true; |
| 125 | +} |
97 | 126 |
|
98 | 127 |
|
99 | 128 |
|
|
0 commit comments