Refactor desync log msg ring buffer into a struct

Increase message limit
This commit is contained in:
Jonathan G Rennison
2021-04-24 00:21:50 +01:00
parent 51e22617cd
commit 77c11d148e

View File

@@ -321,42 +321,62 @@ struct DesyncMsgLogEntry {
: date(_date), date_fract(_date_fract), tick_skip_counter(_tick_skip_counter), msg(msg) { } : date(_date), date_fract(_date_fract), tick_skip_counter(_tick_skip_counter), msg(msg) { }
}; };
static std::array<DesyncMsgLogEntry, 64> desync_msg_log; struct DesyncMsgLog {
static unsigned int desync_msg_log_count = 0; std::array<DesyncMsgLogEntry, 128> log;
static unsigned int desync_msg_log_next = 0; unsigned int count = 0;
unsigned int next = 0;
void ClearDesyncMsgLog() void Clear()
{ {
desync_msg_log_count = 0; this->count = 0;
desync_msg_log_next = 0; this->next = 0;
} }
char *DumpDesyncMsgLog(char *buffer, const char *last) void LogMsg(std::string msg)
{ {
if (!desync_msg_log_count) return buffer; this->log[this->next] = DesyncMsgLogEntry(std::move(msg));
this->next = (this->next + 1) % this->log.size();
this->count++;
}
const unsigned int count = std::min<unsigned int>(desync_msg_log_count, desync_msg_log.size()); char *Dump(char *buffer, const char *last)
unsigned int log_index = (desync_msg_log_next + desync_msg_log.size() - count) % desync_msg_log.size(); {
unsigned int display_num = desync_msg_log_count - count; if (!this->count) return buffer;
buffer += seprintf(buffer, last, "Desync Msg Log:\n Showing most recent %u of %u messages\n", count, desync_msg_log_count); const unsigned int count = std::min<unsigned int>(this->count, this->log.size());
unsigned int log_index = (this->next + this->log.size() - count) % this->log.size();
unsigned int display_num = this->count - count;
buffer += seprintf(buffer, last, "Desync Msg Log:\n Showing most recent %u of %u messages\n", count, this->count);
for (unsigned int i = 0 ; i < count; i++) { for (unsigned int i = 0 ; i < count; i++) {
const DesyncMsgLogEntry &entry = desync_msg_log[log_index]; const DesyncMsgLogEntry &entry = this->log[log_index];
YearMonthDay ymd; YearMonthDay ymd;
ConvertDateToYMD(entry.date, &ymd); ConvertDateToYMD(entry.date, &ymd);
buffer += seprintf(buffer, last, "%5u | %4i-%02i-%02i, %2i, %3i | %s\n", display_num, ymd.year, ymd.month + 1, ymd.day, entry.date_fract, entry.tick_skip_counter, entry.msg.c_str()); buffer += seprintf(buffer, last, "%5u | %4i-%02i-%02i, %2i, %3i | %s\n", display_num, ymd.year, ymd.month + 1, ymd.day, entry.date_fract, entry.tick_skip_counter, entry.msg.c_str());
log_index = (log_index + 1) % desync_msg_log.size(); log_index = (log_index + 1) % this->log.size();
display_num++; display_num++;
} }
buffer += seprintf(buffer, last, "\n"); buffer += seprintf(buffer, last, "\n");
return buffer; return buffer;
} }
};
static DesyncMsgLog _desync_msg_log;
void ClearDesyncMsgLog()
{
_desync_msg_log.Clear();
}
char *DumpDesyncMsgLog(char *buffer, const char *last)
{
buffer = _desync_msg_log.Dump(buffer, last);
return buffer;
}
void LogDesyncMsg(std::string msg) void LogDesyncMsg(std::string msg)
{ {
desync_msg_log[desync_msg_log_next] = DesyncMsgLogEntry(std::move(msg)); _desync_msg_log.LogMsg(std::move(msg));
desync_msg_log_next = (desync_msg_log_next + 1) % desync_msg_log.size();
desync_msg_log_count++;
} }