From 6daebaaa5798ec7e104ac7a2221b1310a0ba0116 Mon Sep 17 00:00:00 2001
From: James Rowe <jroweboy@gmail.com>
Date: Sat, 14 Jul 2018 11:57:13 -0600
Subject: [PATCH 1/2] Logging: Don't lock the queue for the duration of the
 write

---
 src/common/logging/backend.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 242914c6ae..5313b85103 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -83,8 +83,10 @@ private:
                 }
             };
             while (true) {
-                std::unique_lock<std::mutex> lock(message_mutex);
-                message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); });
+                {
+                    std::unique_lock<std::mutex> lock(message_mutex);
+                    message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); });
+                }
                 if (!running) {
                     break;
                 }
@@ -282,4 +284,4 @@ void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
 
     Impl::Instance().PushEntry(std::move(entry));
 }
-} // namespace Log
\ No newline at end of file
+} // namespace Log

From 497b81558e2543f3b42e4267d8ea9acc40bdf03b Mon Sep 17 00:00:00 2001
From: James Rowe <jroweboy@gmail.com>
Date: Sat, 14 Jul 2018 12:47:14 -0600
Subject: [PATCH 2/2] Logging: Dump all logs in the queue on close in debug
 mode

---
 src/common/logging/backend.cpp | 3 ++-
 src/common/logging/filter.cpp  | 7 +++++++
 src/common/logging/filter.h    | 3 +++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 5313b85103..ed1e93cc2c 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -5,6 +5,7 @@
 #include <algorithm>
 #include <array>
 #include <chrono>
+#include <climits>
 #include <condition_variable>
 #include <memory>
 #include <thread>
@@ -94,7 +95,7 @@ private:
             }
             // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case
             // where a system is repeatedly spamming logs even on close.
-            constexpr int MAX_LOGS_TO_WRITE = 100;
+            const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100;
             int logs_written = 0;
             while (logs_written++ < MAX_LOGS_TO_WRITE && message_queue.Pop(entry)) {
                 write_logs(entry);
diff --git a/src/common/logging/filter.cpp b/src/common/logging/filter.cpp
index 4e783a577b..6ed087bebe 100644
--- a/src/common/logging/filter.cpp
+++ b/src/common/logging/filter.cpp
@@ -94,4 +94,11 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin,
 bool Filter::CheckMessage(Class log_class, Level level) const {
     return static_cast<u8>(level) >= static_cast<u8>(class_levels[static_cast<size_t>(log_class)]);
 }
+
+bool Filter::IsDebug() const {
+    return std::any_of(class_levels.begin(), class_levels.end(), [](const Level& l) {
+        return static_cast<u8>(l) <= static_cast<u8>(Level::Debug);
+    });
+}
+
 } // namespace Log
diff --git a/src/common/logging/filter.h b/src/common/logging/filter.h
index ccca289bdc..2a4f7c8452 100644
--- a/src/common/logging/filter.h
+++ b/src/common/logging/filter.h
@@ -47,6 +47,9 @@ public:
     /// Matches class/level combination against the filter, returning true if it passed.
     bool CheckMessage(Class log_class, Level level) const;
 
+    /// Returns true if any logging classes are set to debug
+    bool IsDebug() const;
+
 private:
     std::array<Level, (size_t)Class::Count> class_levels;
 };