From f73c67b8b8b3383985fb53d3859a1fc88dd9941d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 30 Jan 2013 21:20:26 -0800 Subject: [PATCH] Handle it better if the console log overflows. --- Common/ConsoleListener.cpp | 36 ++++++++++++++++++++++-------------- Common/ConsoleListener.h | 6 ------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Common/ConsoleListener.cpp b/Common/ConsoleListener.cpp index 096c2630a1..7bff1714d0 100644 --- a/Common/ConsoleListener.cpp +++ b/Common/ConsoleListener.cpp @@ -250,7 +250,6 @@ void ConsoleListener::LogWriterThread() u32 logRemotePos = Common::AtomicLoadAcquire(logPendingWritePos); if (logRemotePos == (u32) -1) break; - // TODO: if we're really unlucky, this could mean we're LOG_PENDING_MAX behind... else if (logRemotePos == logPendingReadPos) continue; else @@ -271,6 +270,7 @@ void ConsoleListener::LogWriterThread() const int count = logRemotePos - logPendingReadPos; memcpy(logLocal + start, logPending + logPendingReadPos, count); + logPendingReadPos = count; LeaveCriticalSection(&criticalSection); // Double check. @@ -278,7 +278,6 @@ void ConsoleListener::LogWriterThread() break; logLocalSize = start + count; - logPendingReadPos = count; } for (char *Text = logLocal, *End = logLocal + logLocalSize; Text < End; ) @@ -329,6 +328,7 @@ void ConsoleListener::SendToThread(LogTypes::LOG_LEVELS Level, const char *Text) EnterCriticalSection(&criticalSection); u32 logWritePos = Common::AtomicLoad(logPendingWritePos); + u32 prevLogWritePos = logWritePos; if (logWritePos + Len >= LOG_PENDING_MAX) { // One line shouldn't be this long... @@ -345,23 +345,31 @@ void ConsoleListener::SendToThread(LogTypes::LOG_LEVELS Level, const char *Text) } const int count = Len - start; memcpy(logPending + logWritePos, temp + start, count); - - // Double check we didn't start quitting. - if (logPendingWritePos == (u32) -1) - return; - - Common::AtomicStoreRelease(logPendingWritePos, logWritePos + count); + logWritePos += count; } else { snprintf(logPending + logWritePos, LOG_PENDING_MAX - logWritePos, "%s%s", ColorAttr, Text); - - // Double check we didn't start quitting. - if (logPendingWritePos == (u32) -1) - return; - - Common::AtomicStoreRelease(logPendingWritePos, logWritePos + Len); + logWritePos += Len; } + + // Oops, we passed the read pos. + if (prevLogWritePos < logPendingReadPos && logWritePos >= logPendingReadPos) + { + char *nextNewline = (char *) memchr(logPending + logWritePos, '\n', LOG_PENDING_MAX - logWritePos); + if (nextNewline == NULL && logWritePos > 0) + nextNewline = (char *) memchr(logPending, '\n', logWritePos); + + // Okay, have it go right after the next newline. + if (nextNewline != NULL) + logPendingReadPos = nextNewline - logPending + 1; + } + + // Double check we didn't start quitting. + if (logPendingWritePos == (u32) -1) + return; + + Common::AtomicStoreRelease(logPendingWritePos, logWritePos); LeaveCriticalSection(&criticalSection); SetEvent(hTriggerEvent); diff --git a/Common/ConsoleListener.h b/Common/ConsoleListener.h index 82da1d3cd5..b98cf444cd 100644 --- a/Common/ConsoleListener.h +++ b/Common/ConsoleListener.h @@ -22,12 +22,6 @@ #ifdef _WIN32 #include - -struct ConsolePendingEvent -{ - LogTypes::LOG_LEVELS Level; - const char *Text; -}; #endif class ConsoleListener : public LogListener