Просмотр исходного кода

common: logging: Implement Android logcat backend.

bunnei 3 лет назад
Родитель
Сommit
99296a1510
3 измененных файлов с 63 добавлено и 0 удалено
  1. 26 0
      src/common/logging/backend.cpp
  2. 35 0
      src/common/logging/text_formatter.cpp
  3. 2 0
      src/common/logging/text_formatter.h

+ 26 - 0
src/common/logging/backend.cpp

@@ -155,6 +155,26 @@ public:
     void EnableForStacktrace() override {}
     void EnableForStacktrace() override {}
 };
 };
 
 
+#ifdef ANDROID
+/**
+ * Backend that writes to the Android logcat
+ */
+class LogcatBackend : public Backend {
+public:
+    explicit LogcatBackend() = default;
+
+    ~LogcatBackend() override = default;
+
+    void Write(const Entry& entry) override {
+        PrintMessageToLogcat(entry);
+    }
+
+    void Flush() override {}
+
+    void EnableForStacktrace() override {}
+};
+#endif
+
 bool initialization_in_progress_suppress_logging = true;
 bool initialization_in_progress_suppress_logging = true;
 
 
 /**
 /**
@@ -260,6 +280,9 @@ private:
         lambda(static_cast<Backend&>(debugger_backend));
         lambda(static_cast<Backend&>(debugger_backend));
         lambda(static_cast<Backend&>(color_console_backend));
         lambda(static_cast<Backend&>(color_console_backend));
         lambda(static_cast<Backend&>(file_backend));
         lambda(static_cast<Backend&>(file_backend));
+#ifdef ANDROID
+        lambda(static_cast<Backend&>(lc_backend));
+#endif
     }
     }
 
 
     static void Deleter(Impl* ptr) {
     static void Deleter(Impl* ptr) {
@@ -272,6 +295,9 @@ private:
     DebuggerBackend debugger_backend{};
     DebuggerBackend debugger_backend{};
     ColorConsoleBackend color_console_backend{};
     ColorConsoleBackend color_console_backend{};
     FileBackend file_backend;
     FileBackend file_backend;
+#ifdef ANDROID
+    LogcatBackend lc_backend{};
+#endif
 
 
     MPSCQueue<Entry> message_queue{};
     MPSCQueue<Entry> message_queue{};
     std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
     std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};

+ 35 - 0
src/common/logging/text_formatter.cpp

@@ -8,6 +8,10 @@
 #include <windows.h>
 #include <windows.h>
 #endif
 #endif
 
 
+#ifdef ANDROID
+#include <android/log.h>
+#endif
+
 #include "common/assert.h"
 #include "common/assert.h"
 #include "common/logging/filter.h"
 #include "common/logging/filter.h"
 #include "common/logging/log.h"
 #include "common/logging/log.h"
@@ -106,4 +110,35 @@ void PrintColoredMessage(const Entry& entry) {
 #undef ESC
 #undef ESC
 #endif
 #endif
 }
 }
+
+void PrintMessageToLogcat(const Entry& entry) {
+#ifdef ANDROID
+    const auto str = FormatLogMessage(entry);
+
+    android_LogPriority android_log_priority;
+    switch (entry.log_level) {
+    case Level::Trace:
+        android_log_priority = ANDROID_LOG_VERBOSE;
+        break;
+    case Level::Debug:
+        android_log_priority = ANDROID_LOG_DEBUG;
+        break;
+    case Level::Info:
+        android_log_priority = ANDROID_LOG_INFO;
+        break;
+    case Level::Warning:
+        android_log_priority = ANDROID_LOG_WARN;
+        break;
+    case Level::Error:
+        android_log_priority = ANDROID_LOG_ERROR;
+        break;
+    case Level::Critical:
+        android_log_priority = ANDROID_LOG_FATAL;
+        break;
+    case Level::Count:
+        UNREACHABLE();
+    }
+    __android_log_print(android_log_priority, "CitraNative", "%s", str.c_str());
+#endif
+}
 } // namespace Common::Log
 } // namespace Common::Log

+ 2 - 0
src/common/logging/text_formatter.h

@@ -15,4 +15,6 @@ std::string FormatLogMessage(const Entry& entry);
 void PrintMessage(const Entry& entry);
 void PrintMessage(const Entry& entry);
 /// Prints the same message as `PrintMessage`, but colored according to the severity level.
 /// Prints the same message as `PrintMessage`, but colored according to the severity level.
 void PrintColoredMessage(const Entry& entry);
 void PrintColoredMessage(const Entry& entry);
+/// Formats and prints a log entry to the android logcat.
+void PrintMessageToLogcat(const Entry& entry);
 } // namespace Common::Log
 } // namespace Common::Log