From 4153bd8d171ffe7cd11c467b338f843859565d51 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 29 Jun 2019 17:17:35 -0400
Subject: [PATCH] core: Add LM::Manager to system

Allows centralized control over logging mechanisms.
---
 src/core/CMakeLists.txt          |  2 ++
 src/core/core.cpp                | 15 ++++++++++++---
 src/core/core.h                  |  8 ++++++++
 src/core/hle/service/lm/lm.cpp   | 25 +++++++++++++------------
 src/core/hle/service/lm/lm.h     |  6 +++---
 src/core/hle/service/service.cpp |  2 +-
 6 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index a6b56c9c6..be3ffb779 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -324,6 +324,8 @@ add_library(core STATIC
     hle/service/ldr/ldr.h
     hle/service/lm/lm.cpp
     hle/service/lm/lm.h
+    hle/service/lm/manager.cpp
+    hle/service/lm/manager.h
     hle/service/mig/mig.cpp
     hle/service/mig/mig.h
     hle/service/mii/mii.cpp
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 92ba42fb9..eb2c2e204 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -35,6 +35,7 @@
 #include "core/hle/service/apm/controller.h"
 #include "core/hle/service/filesystem/filesystem.h"
 #include "core/hle/service/glue/manager.h"
+#include "core/hle/service/lm/manager.h"
 #include "core/hle/service/service.h"
 #include "core/hle/service/sm/sm.h"
 #include "core/loader/loader.h"
@@ -337,6 +338,7 @@ struct System::Impl {
     bool is_powered_on = false;
     bool exit_lock = false;
 
+    Reporter reporter;
     std::unique_ptr<Memory::CheatEngine> cheat_engine;
     std::unique_ptr<Tools::Freezer> memory_freezer;
 
@@ -346,8 +348,9 @@ struct System::Impl {
     /// APM (Performance) services
     Service::APM::Controller apm_controller{core_timing};
 
-    /// Glue services
+    /// Service State
     Service::Glue::ARPManager arp_manager;
+    Service::LM::Manager lm_manager{reporter};
 
     /// Service manager
     std::shared_ptr<Service::SM::ServiceManager> service_manager;
@@ -355,8 +358,6 @@ struct System::Impl {
     /// Telemetry session for this emulation session
     std::unique_ptr<Core::TelemetrySession> telemetry_session;
 
-    Reporter reporter;
-
     ResultStatus status = ResultStatus::Success;
     std::string status_details = "";
 
@@ -632,6 +633,14 @@ const Service::APM::Controller& System::GetAPMController() const {
     return impl->apm_controller;
 }
 
+Service::LM::Manager& System::GetLogManager() {
+    return impl->lm_manager;
+}
+
+const Service::LM::Manager& System::GetLogManager() const {
+    return impl->lm_manager;
+}
+
 void System::SetExitLock(bool locked) {
     impl->exit_lock = locked;
 }
diff --git a/src/core/core.h b/src/core/core.h
index ff10ebe12..0170e0b05 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -58,6 +58,10 @@ namespace Glue {
 class ARPManager;
 }
 
+namespace LM {
+class Manager;
+} // namespace LM
+
 namespace SM {
 class ServiceManager;
 } // namespace SM
@@ -326,6 +330,10 @@ public:
 
     const Service::APM::Controller& GetAPMController() const;
 
+    Service::LM::Manager& GetLogManager();
+
+    const Service::LM::Manager& GetLogManager() const;
+
     void SetExitLock(bool locked);
 
     bool GetExitLock() const;
diff --git a/src/core/hle/service/lm/lm.cpp b/src/core/hle/service/lm/lm.cpp
index 2a61593e2..efba18fe7 100644
--- a/src/core/hle/service/lm/lm.cpp
+++ b/src/core/hle/service/lm/lm.cpp
@@ -6,8 +6,10 @@
 #include <string>
 
 #include "common/logging/log.h"
+#include "common/scope_exit.h"
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/service/lm/lm.h"
+#include "core/hle/service/lm/manager.h"
 #include "core/hle/service/service.h"
 #include "core/memory.h"
 
@@ -194,31 +196,30 @@ private:
 
 class LM final : public ServiceFramework<LM> {
 public:
-    explicit LM() : ServiceFramework{"lm"} {
+    explicit LM(Manager& manager) : ServiceFramework{"lm"}, manager(manager) {
+        // clang-format off
         static const FunctionInfo functions[] = {
-            {0x00000000, &LM::OpenLogger, "OpenLogger"},
+            {0, &LM::OpenLogger, "OpenLogger"},
         };
+        // clang-format on
+
         RegisterHandlers(functions);
     }
 
-    /**
-     * LM::OpenLogger service function
-     *  Inputs:
-     *      0: 0x00000000
-     *  Outputs:
-     *      0: ResultCode
-     */
+private:
     void OpenLogger(Kernel::HLERequestContext& ctx) {
         LOG_DEBUG(Service_LM, "called");
 
         IPC::ResponseBuilder rb{ctx, 2, 0, 1};
         rb.Push(RESULT_SUCCESS);
-        rb.PushIpcInterface<ILogger>();
+        rb.PushIpcInterface<ILogger>(manager);
     }
+
+    Manager& manager;
 };
 
-void InstallInterfaces(SM::ServiceManager& service_manager) {
-    std::make_shared<LM>()->InstallAsService(service_manager);
+void InstallInterfaces(Core::System& system) {
+    std::make_shared<LM>(system.GetLogManager())->InstallAsService(system.ServiceManager());
 }
 
 } // namespace Service::LM
diff --git a/src/core/hle/service/lm/lm.h b/src/core/hle/service/lm/lm.h
index 7806ae27b..d40410b5c 100644
--- a/src/core/hle/service/lm/lm.h
+++ b/src/core/hle/service/lm/lm.h
@@ -4,13 +4,13 @@
 
 #pragma once
 
-namespace Service::SM {
-class ServiceManager;
+namespace Core {
+class System;
 }
 
 namespace Service::LM {
 
 /// Registers all LM services with the specified service manager.
-void InstallInterfaces(SM::ServiceManager& service_manager);
+void InstallInterfaces(Core::System& system);
 
 } // namespace Service::LM
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index 831a427de..c12a746c8 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -226,7 +226,7 @@ void Init(std::shared_ptr<SM::ServiceManager>& sm, Core::System& system) {
     LBL::InstallInterfaces(*sm);
     LDN::InstallInterfaces(*sm);
     LDR::InstallInterfaces(*sm, system);
-    LM::InstallInterfaces(*sm);
+    LM::InstallInterfaces(system);
     Migration::InstallInterfaces(*sm);
     Mii::InstallInterfaces(*sm);
     MM::InstallInterfaces(*sm);