From 75282366c00b013376d1c5f586c25898086eb5d8 Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Sat, 13 Mar 2021 13:47:06 +0300 Subject: [PATCH] log messages with os_log --- lib/CConsoleHandler.cpp | 2 -- lib/CIOSUtils.h | 2 ++ lib/CIOSUtils.m | 2 ++ lib/logging/CLogger.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/CConsoleHandler.cpp b/lib/CConsoleHandler.cpp index ebff37841..b5489b984 100644 --- a/lib/CConsoleHandler.cpp +++ b/lib/CConsoleHandler.cpp @@ -165,7 +165,6 @@ LONG WINAPI onUnhandledException(EXCEPTION_POINTERS* exception) void CConsoleHandler::setColor(EConsoleTextColor::EConsoleTextColor color) { -#ifndef VCMI_IOS TColor colorCode; switch(color) { @@ -205,7 +204,6 @@ void CConsoleHandler::setColor(EConsoleTextColor::EConsoleTextColor color) #else std::cout << colorCode; #endif -#endif } int CConsoleHandler::run() diff --git a/lib/CIOSUtils.h b/lib/CIOSUtils.h index 0e5ac20cc..682429747 100644 --- a/lib/CIOSUtils.h +++ b/lib/CIOSUtils.h @@ -13,3 +13,5 @@ extern const char *ios_cachesPath(); extern const char *ios_bundlePath(); extern const char *ios_frameworksPath(); + +extern const char *ios_bundleIdentifier(); diff --git a/lib/CIOSUtils.m b/lib/CIOSUtils.m index f3f765352..0621f1230 100644 --- a/lib/CIOSUtils.m +++ b/lib/CIOSUtils.m @@ -22,3 +22,5 @@ const char *ios_cachesPath() { return standardPath(NSCachesDirectory); } const char *ios_bundlePath() { return NSBundle.mainBundle.bundlePath.UTF8String; } const char *ios_frameworksPath() { return [NSBundle.mainBundle.bundlePath stringByAppendingPathComponent:@"Frameworks"].UTF8String; } // TODO: sharedFrameworksPath? + +const char *ios_bundleIdentifier() { return NSBundle.mainBundle.bundleIdentifier.UTF8String; } diff --git a/lib/logging/CLogger.cpp b/lib/logging/CLogger.cpp index fc6e57cfe..651cf5d91 100644 --- a/lib/logging/CLogger.cpp +++ b/lib/logging/CLogger.cpp @@ -30,6 +30,11 @@ namespace ELogLevel return ANDROID_LOG_UNKNOWN; } } +#elif defined(VCMI_IOS) +extern "C" { +#import "../CIOSUtils.h" +#include +} #endif namespace vstd @@ -353,6 +358,41 @@ void CLogConsoleTarget::write(const LogRecord & record) #ifdef VCMI_ANDROID __android_log_write(ELogLevel::toAndroid(record.level), ("VCMI-" + record.domain.getName()).c_str(), message.c_str()); +#elif defined(VCMI_IOS) + os_log_type_t type; + switch (record.level) + { + case ELogLevel::TRACE: + type = OS_LOG_TYPE_DEBUG; + break; + case ELogLevel::DEBUG: + type = OS_LOG_TYPE_DEFAULT; + break; + case ELogLevel::INFO: + type = OS_LOG_TYPE_INFO; + break; + case ELogLevel::WARN: + type = OS_LOG_TYPE_ERROR; + break; + case ELogLevel::ERROR: + type = OS_LOG_TYPE_FAULT; + break; + default: + return; + } + + os_log_t currentLog; + static std::unordered_map logs; + const auto& domainName = record.domain.getName(); + auto logIt = logs.find(domainName); + if (logIt != logs.end()) + currentLog = logIt->second; + else + { + currentLog = os_log_create(ios_bundleIdentifier(), domainName.c_str()); + logs.insert({domainName, currentLog}); + } + os_log_with_type(currentLog, type, "%s", message.c_str()); #else const bool printToStdErr = record.level >= ELogLevel::WARN;