From 0aa44e238db7a72f4fb8b347168ec76c3ce48ad5 Mon Sep 17 00:00:00 2001
From: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Date: Fri, 6 Mar 2015 19:15:02 +0100
Subject: [PATCH] Logging: check for filter before sending to the queue, to
 skip all heavy formatting on the other thread.

---
 src/citra/citra.cpp                   | 3 ++-
 src/citra_qt/main.cpp                 | 3 ++-
 src/common/logging/backend.cpp        | 9 +++++++++
 src/common/logging/backend.h          | 3 +++
 src/common/logging/filter.h           | 2 ++
 src/common/logging/text_formatter.cpp | 7 ++-----
 src/common/logging/text_formatter.h   | 3 +--
 7 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index 69f0b35b35..2c6ced920e 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -22,7 +22,8 @@
 int __cdecl main(int argc, char **argv) {
     std::shared_ptr<Log::Logger> logger = Log::InitGlobalLogger();
     Log::Filter log_filter(Log::Level::Debug);
-    std::thread logging_thread(Log::TextLoggingLoop, logger, &log_filter);
+    Log::SetFilter(&log_filter);
+    std::thread logging_thread(Log::TextLoggingLoop, logger);
     SCOPE_EXIT({
         logger->Close();
         logging_thread.join();
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index 881c7d3378..df7699921a 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -310,7 +310,8 @@ int __cdecl main(int argc, char* argv[])
 {
     std::shared_ptr<Log::Logger> logger = Log::InitGlobalLogger();
     Log::Filter log_filter(Log::Level::Info);
-    std::thread logging_thread(Log::TextLoggingLoop, logger, &log_filter);
+    Log::SetFilter(&log_filter);
+    std::thread logging_thread(Log::TextLoggingLoop, logger);
     SCOPE_EXIT({
         logger->Close();
         logging_thread.join();
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 7c1010b225..7b479b5692 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -135,9 +135,18 @@ Entry CreateEntry(Class log_class, Level log_level,
     return std::move(entry);
 }
 
+static Filter* filter;
+
+void SetFilter(Filter* new_filter) {
+    filter = new_filter;
+}
+
 void LogMessage(Class log_class, Level log_level,
                 const char* filename, unsigned int line_nr, const char* function,
                 const char* format, ...) {
+    if (!filter->CheckMessage(log_class, log_level))
+        return;
+
     va_list args;
     va_start(args, format);
     Entry entry = CreateEntry(log_class, log_level,
diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h
index 1c44c929ee..3114f864cd 100644
--- a/src/common/logging/backend.h
+++ b/src/common/logging/backend.h
@@ -10,6 +10,7 @@
 
 #include "common/concurrent_ring_buffer.h"
 
+#include "common/logging/filter.h"
 #include "common/logging/log.h"
 
 namespace Log {
@@ -131,4 +132,6 @@ Entry CreateEntry(Class log_class, Level log_level,
 /// Initializes the default Logger.
 std::shared_ptr<Logger> InitGlobalLogger();
 
+void SetFilter(Filter* filter);
+
 }
diff --git a/src/common/logging/filter.h b/src/common/logging/filter.h
index c3da9989f4..b53e4e6334 100644
--- a/src/common/logging/filter.h
+++ b/src/common/logging/filter.h
@@ -2,6 +2,8 @@
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
+#pragma once
+
 #include <array>
 #include <string>
 
diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp
index ef5739d842..36c91c4f65 100644
--- a/src/common/logging/text_formatter.cpp
+++ b/src/common/logging/text_formatter.cpp
@@ -11,7 +11,6 @@
 #endif
 
 #include "common/logging/backend.h"
-#include "common/logging/filter.h"
 #include "common/logging/log.h"
 #include "common/logging/text_formatter.h"
 
@@ -116,7 +115,7 @@ void PrintColoredMessage(const Entry& entry) {
 #endif
 }
 
-void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) {
+void TextLoggingLoop(std::shared_ptr<Logger> logger) {
     std::array<Entry, 256> entry_buffer;
 
     while (true) {
@@ -126,9 +125,7 @@ void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter) {
         }
         for (size_t i = 0; i < num_entries; ++i) {
             const Entry& entry = entry_buffer[i];
-            if (filter->CheckMessage(entry.log_class, entry.log_level)) {
-                PrintColoredMessage(entry);
-            }
+            PrintColoredMessage(entry);
         }
     }
 }
diff --git a/src/common/logging/text_formatter.h b/src/common/logging/text_formatter.h
index 2f05794f08..8474a19047 100644
--- a/src/common/logging/text_formatter.h
+++ b/src/common/logging/text_formatter.h
@@ -11,7 +11,6 @@ namespace Log {
 
 class Logger;
 struct Entry;
-class Filter;
 
 /**
  * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's
@@ -36,6 +35,6 @@ void PrintColoredMessage(const Entry& entry);
  * Logging loop that repeatedly reads messages from the provided logger and prints them to the
  * console. It is the baseline barebones log outputter.
  */
-void TextLoggingLoop(std::shared_ptr<Logger> logger, const Filter* filter);
+void TextLoggingLoop(std::shared_ptr<Logger> logger);
 
 }