浏览代码

log messages with os_log

Andrey Filipenkov 4 年之前
父节点
当前提交
75282366c0
共有 4 个文件被更改,包括 44 次插入2 次删除
  1. 0 2
      lib/CConsoleHandler.cpp
  2. 2 0
      lib/CIOSUtils.h
  3. 2 0
      lib/CIOSUtils.m
  4. 40 0
      lib/logging/CLogger.cpp

+ 0 - 2
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()

+ 2 - 0
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();

+ 2 - 0
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; }

+ 40 - 0
lib/logging/CLogger.cpp

@@ -30,6 +30,11 @@ namespace ELogLevel
 		return ANDROID_LOG_UNKNOWN;
 	}
 }
+#elif defined(VCMI_IOS)
+extern "C" {
+#import "../CIOSUtils.h"
+#include <os/log.h>
+}
 #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<std::string, decltype(currentLog)> 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;