From 3d083859c17db6872a0a08650b162fe0f6dfc660 Mon Sep 17 00:00:00 2001
From: Carl Kenner <carl.kenner@gmail.com>
Date: Fri, 5 Oct 2018 12:52:49 +0930
Subject: [PATCH] logging: Add DebuggerBackend for logging to Visual Studio

---
 src/citra/citra.cpp            |  3 +++
 src/citra_qt/main.cpp          |  3 +++
 src/common/logging/backend.cpp | 11 +++++++++--
 src/common/logging/backend.h   | 14 ++++++++++++++
 4 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp
index 71693f21f..b64510957 100644
--- a/src/citra/citra.cpp
+++ b/src/citra/citra.cpp
@@ -126,6 +126,9 @@ static void InitializeLogging() {
     const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
     FileUtil::CreateFullPath(log_dir);
     Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
+#ifdef _WIN32
+    Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
+#endif
 }
 
 /// Application entry point
diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp
index db6ba6202..7e7b3891b 100644
--- a/src/citra_qt/main.cpp
+++ b/src/citra_qt/main.cpp
@@ -116,6 +116,9 @@ static void InitializeLogging() {
     const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
     FileUtil::CreateFullPath(log_dir);
     Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
+#ifdef _WIN32
+    Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
+#endif
 }
 
 GMainWindow::GMainWindow() : config(new Config()), emu_thread(nullptr) {
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 722799fad..d93c5bbd7 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -11,7 +11,8 @@
 #include <thread>
 #include <vector>
 #ifdef _WIN32
-#include <share.h> // For _SH_DENYWR
+#include <share.h>   // For _SH_DENYWR
+#include <windows.h> // For OutputDebugStringW
 #else
 #define _SH_DENYWR 0
 #endif
@@ -133,12 +134,18 @@ void FileBackend::Write(const Entry& entry) {
     if (!file.IsOpen() || bytes_written > MAX_BYTES_WRITTEN) {
         return;
     }
-    bytes_written += file.WriteString(FormatLogMessage(entry) + '\n');
+    bytes_written += file.WriteString(FormatLogMessage(entry).append(1, '\n'));
     if (entry.log_level >= Level::Error) {
         file.Flush();
     }
 }
 
+void DebuggerBackend::Write(const Entry& entry) {
+#ifdef _WIN32
+    ::OutputDebugStringW(Common::UTF8ToUTF16W(FormatLogMessage(entry).append(1, '\n')).c_str());
+#endif
+}
+
 /// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this.
 #define ALL_LOG_CLASSES()                                                                          \
     CLS(Log)                                                                                       \
diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h
index f8b39ad0b..781c63628 100644
--- a/src/common/logging/backend.h
+++ b/src/common/logging/backend.h
@@ -105,6 +105,20 @@ private:
     std::size_t bytes_written;
 };
 
+/**
+ * Backend that writes to Visual Studio's output window
+ */
+class DebuggerBackend : public Backend {
+public:
+    static const char* Name() {
+        return "debugger";
+    }
+    const char* GetName() const override {
+        return Name();
+    }
+    void Write(const Entry& entry) override;
+};
+
 void AddBackend(std::unique_ptr<Backend> backend);
 
 void RemoveBackend(std::string_view backend_name);