diff --git a/src/android/app/src/main/jni/id_cache.cpp b/src/android/app/src/main/jni/id_cache.cpp index b7197389f..6c5c31acf 100644 --- a/src/android/app/src/main/jni/id_cache.cpp +++ b/src/android/app/src/main/jni/id_cache.cpp @@ -102,7 +102,7 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { Log::Filter log_filter; log_filter.ParseFilterString(Settings::values.log_filter); Log::SetGlobalFilter(log_filter); - Log::AddBackend(std::make_unique()); + Log::AddBackend(std::make_unique()); FileUtil::CreateFullPath(FileUtil::GetUserPath(FileUtil::UserPath::LogDir)); Log::AddBackend(std::make_unique( FileUtil::GetUserPath(FileUtil::UserPath::LogDir) + LOG_FILE)); diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 3191a354d..43382b66b 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -140,6 +140,10 @@ void ColorConsoleBackend::Write(const Entry& entry) { PrintColoredMessage(entry); } +void LogcatBackend::Write(const Entry& entry) { + PrintMessageToLogcat(entry); +} + // _SH_DENYWR allows read only access to the file for other programs. // It is #defined to 0 on other platforms FileBackend::FileBackend(const std::string& filename) diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index b0c78937f..907c6a297 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -81,6 +81,21 @@ public: void Write(const Entry& entry) override; }; +/** + * Backend that writes to the Android logcat + */ +class LogcatBackend : public Backend { +public: + static const char* Name() { + return "logcat"; + } + + const char* GetName() const override { + return Name(); + } + void Write(const Entry& entry) override; +}; + /** * Backend that writes to a file passed into the constructor */ diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp index aa0dbd0c6..43da4b60a 100644 --- a/src/common/logging/text_formatter.cpp +++ b/src/common/logging/text_formatter.cpp @@ -34,13 +34,7 @@ std::string FormatLogMessage(const Entry& entry) { void PrintMessage(const Entry& entry) { const auto str = FormatLogMessage(entry).append(1, '\n'); -#ifdef ANDROID - // Android's log level enum are offset by '2' - const int android_log_level = static_cast(entry.log_level) + 2; - __android_log_print(android_log_level, "CitraNative", "%s", str.c_str()); -#else fputs(str.c_str(), stderr); -#endif } void PrintColoredMessage(const Entry& entry) { @@ -78,7 +72,7 @@ void PrintColoredMessage(const Entry& entry) { } SetConsoleTextAttribute(console_handle, color); -#elif !defined(ANDROID) +#else #define ESC "\x1b" const char* color = ""; switch (entry.log_level) { @@ -111,9 +105,40 @@ void PrintColoredMessage(const Entry& entry) { #ifdef _WIN32 SetConsoleTextAttribute(console_handle, original_info.wAttributes); -#elif !defined(ANDROID) +#else fputs(ESC "[0m", stderr); #undef ESC #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 Log diff --git a/src/common/logging/text_formatter.h b/src/common/logging/text_formatter.h index b6d9e57c8..13430951d 100644 --- a/src/common/logging/text_formatter.h +++ b/src/common/logging/text_formatter.h @@ -17,4 +17,6 @@ std::string FormatLogMessage(const Entry& entry); void PrintMessage(const Entry& entry); /// Prints the same message as `PrintMessage`, but colored according to the severity level. void PrintColoredMessage(const Entry& entry); +/// Formats and prints a log entry to the android logcat. +void PrintMessageToLogcat(const Entry& entry); } // namespace Log