From 9950e87a5aadb8e82ef4e63d24e13ff4dce2ca13 Mon Sep 17 00:00:00 2001 From: Soumen Rautray Date: Fri, 23 Aug 2024 11:02:39 +0530 Subject: [PATCH] chore: add production logs and refactor Logger (RMCCX-6877) --- .../InAppMessagingModule.swift | 3 ++ .../Protocols/HttpRequestable.swift | 2 +- Sources/RInAppMessaging/RInAppMessaging.swift | 1 + .../Utilities/CommonUtility.swift | 14 --------- .../RInAppMessaging/Utilities/Logger.swift | 30 +++++++++++++++++++ 5 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 Sources/RInAppMessaging/Utilities/Logger.swift diff --git a/Sources/RInAppMessaging/InAppMessagingModule.swift b/Sources/RInAppMessaging/InAppMessagingModule.swift index b0e5dbaf..a704ca29 100644 --- a/Sources/RInAppMessaging/InAppMessagingModule.swift +++ b/Sources/RInAppMessaging/InAppMessagingModule.swift @@ -86,10 +86,12 @@ internal class InAppMessagingModule: ErrorDelegate, CampaignDispatcherDelegate, checkUserChanges() eventMatcher.matchAndStore(event: event) campaignTriggerAgent.validateAndTriggerCampaigns() + Logger.debugLog("function: LogEvent") return true } func registerPreference(_ preference: UserInfoProvider) { + Logger.debugLog("function: registerPreference") accountRepository.setPreference(preference) guard isInitialized else { @@ -103,6 +105,7 @@ internal class InAppMessagingModule: ErrorDelegate, CampaignDispatcherDelegate, } func closeMessage(clearQueuedCampaigns: Bool) { + Logger.debugLog("function: closeMessage") if clearQueuedCampaigns { readyCampaignDispatcher.resetQueue() } diff --git a/Sources/RInAppMessaging/Protocols/HttpRequestable.swift b/Sources/RInAppMessaging/Protocols/HttpRequestable.swift index 3836a991..2504dd85 100644 --- a/Sources/RInAppMessaging/Protocols/HttpRequestable.swift +++ b/Sources/RInAppMessaging/Protocols/HttpRequestable.swift @@ -141,7 +141,7 @@ extension HttpRequestable { if let error = error { completion(.failure(.taskFailed(error))) - Logger.debug(error) + Logger.debug("Error: \(error)") return } diff --git a/Sources/RInAppMessaging/RInAppMessaging.swift b/Sources/RInAppMessaging/RInAppMessaging.swift index 1bb625d1..fcd2632a 100644 --- a/Sources/RInAppMessaging/RInAppMessaging.swift +++ b/Sources/RInAppMessaging/RInAppMessaging.swift @@ -100,6 +100,7 @@ import RSDKUtils let validConfigURL = tryGettingValidConfigURL(config) let mainContainer = MainContainerFactory.create(dependencyManager: dependencyManager, configURL: validConfigURL) dependencyManager.appendContainer(mainContainer) + Logger.debugLog("function: Configure") configure(dependencyManager: dependencyManager, moduleConfig: config) } diff --git a/Sources/RInAppMessaging/Utilities/CommonUtility.swift b/Sources/RInAppMessaging/Utilities/CommonUtility.swift index 60f0d252..c401fc45 100644 --- a/Sources/RInAppMessaging/Utilities/CommonUtility.swift +++ b/Sources/RInAppMessaging/Utilities/CommonUtility.swift @@ -103,17 +103,3 @@ internal struct CommonUtility { return nil } } - -internal enum Logger { - static func debug(_ value: Any) { - #if DEBUG - print("InAppMessaging: " + String(describing: value)) - #endif - } - - static func debug(_ message: String) { - #if DEBUG - print("InAppMessaging: " + message) - #endif - } -} diff --git a/Sources/RInAppMessaging/Utilities/Logger.swift b/Sources/RInAppMessaging/Utilities/Logger.swift new file mode 100644 index 00000000..bf448bd8 --- /dev/null +++ b/Sources/RInAppMessaging/Utilities/Logger.swift @@ -0,0 +1,30 @@ +import os +import Foundation + +extension OSLog { + static let sdk = OSLog(category: "RInAppMessaging SDK") + + private convenience init(category: String, bundle: Bundle = Bundle(for: Logger.self)) { + let identifier = bundle.infoDictionary?["CFBundleIdentifier"] as? String + self.init(subsystem: (identifier ?? "").appending(".logs"), category: category) + } +} + + class Logger { + // Debug Logging + class func debug(_ message: String) { + #if DEBUG + print("InAppMessaging: " + message) + #else + os_log("%s", log: OSLog.sdk, type: .error, message) + #endif + } + + class func debugLog(_ message: String) { + #if DEBUG + // do nothing + #else + os_log("%s", log: OSLog.sdk, type: .error, message) + #endif + } + }