From a5713a9abfe968ca80b3fd3f99fc1ec3ce5f8878 Mon Sep 17 00:00:00 2001 From: Brandon Stalnaker Date: Wed, 25 Sep 2024 09:29:59 -0400 Subject: [PATCH 1/3] refactor: Port MPILogger to Swift --- mParticle-Apple-SDK.xcodeproj/project.pbxproj | 6 +++ mParticle-Apple-SDK/Logger/MPLogger.swift | 38 +++++++++++++++++++ mParticle-Apple-SDK/Utils/MPIHasher.swift | 2 + 3 files changed, 46 insertions(+) create mode 100644 mParticle-Apple-SDK/Logger/MPLogger.swift diff --git a/mParticle-Apple-SDK.xcodeproj/project.pbxproj b/mParticle-Apple-SDK.xcodeproj/project.pbxproj index 5163f622..b95a117c 100644 --- a/mParticle-Apple-SDK.xcodeproj/project.pbxproj +++ b/mParticle-Apple-SDK.xcodeproj/project.pbxproj @@ -517,6 +517,8 @@ D3CEDABF2C9C5B5B001B32DF /* MPGDPRConsent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDABD2C9C5B5B001B32DF /* MPGDPRConsent.swift */; }; D3CEDAC02C9C5B5B001B32DF /* MPGDPRConsent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDABD2C9C5B5B001B32DF /* MPGDPRConsent.swift */; }; D3CEDAC12C9C5B5B001B32DF /* MPGDPRConsent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDABD2C9C5B5B001B32DF /* MPGDPRConsent.swift */; }; + D3CEDAC82CA2F214001B32DF /* MPLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDAC72CA2F214001B32DF /* MPLogger.swift */; }; + D3CEDAC92CA2F214001B32DF /* MPLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDAC72CA2F214001B32DF /* MPLogger.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -807,6 +809,7 @@ D3BA75152B614E3D008C3C65 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = ""; }; D3CEDAB82C9B14BF001B32DF /* MPCCPAConsent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MPCCPAConsent.swift; sourceTree = ""; }; D3CEDABD2C9C5B5B001B32DF /* MPGDPRConsent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPGDPRConsent.swift; sourceTree = ""; }; + D3CEDAC72CA2F214001B32DF /* MPLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPLogger.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -1117,6 +1120,7 @@ isa = PBXGroup; children = ( 53A79B4629CDFB1F00E7489F /* MPILogger.h */, + D3CEDAC72CA2F214001B32DF /* MPLogger.swift */, ); path = Logger; sourceTree = ""; @@ -1805,6 +1809,7 @@ 53B33E8A2A4606CD00CC8A19 /* MPSideloadedKit.swift in Sources */, 53A79B9629CDFB2000E7489F /* MPSession.m in Sources */, 53A79B9729CDFB2000E7489F /* MPIntegrationAttributes.m in Sources */, + D3CEDAC82CA2F214001B32DF /* MPLogger.swift in Sources */, 53A79B6A29CDFB2000E7489F /* MPAliasRequest.m in Sources */, 53A79B6829CDFB2000E7489F /* MParticleUser.m in Sources */, 53B28FB22C938C26009072FC /* MPLocationManager.swift in Sources */, @@ -1972,6 +1977,7 @@ 53B33E8B2A4606CD00CC8A19 /* MPSideloadedKit.swift in Sources */, 53A79D9A29CE23F700E7489F /* MPSession.m in Sources */, 53A79D9B29CE23F700E7489F /* MPIntegrationAttributes.m in Sources */, + D3CEDAC92CA2F214001B32DF /* MPLogger.swift in Sources */, 53A79D9C29CE23F700E7489F /* MPAliasRequest.m in Sources */, 53A79D9D29CE23F700E7489F /* MParticleUser.m in Sources */, 53B28FB32C938C26009072FC /* MPLocationManager.swift in Sources */, diff --git a/mParticle-Apple-SDK/Logger/MPLogger.swift b/mParticle-Apple-SDK/Logger/MPLogger.swift new file mode 100644 index 00000000..bf3d6324 --- /dev/null +++ b/mParticle-Apple-SDK/Logger/MPLogger.swift @@ -0,0 +1,38 @@ +// +// MPLogger.swift +// mParticle-Apple-SDK +// +// Created by Brandon Stalnaker on 9/22/24. +// + +import Foundation + +public struct MPLogger { + + public static func MPLogger(loggerLevel: MPILogLevel, format: String, arguments: any CVarArg...) { + if (MParticle.sharedInstance().logLevel.rawValue >= loggerLevel.rawValue && loggerLevel != .none) { + let msg = String.localizedStringWithFormat(format, arguments) +// Custom Logger is marked as nonnull despite the fact that we expect it to be null if not set by the client +// if let customLogger = MParticle.sharedInstance().customLogger { +// customLogger(msg) +// } else + NSLog("%@", msg) + } + } + + public static func MPLogError(format: String, arguments: any CVarArg...) { + MPLogger(loggerLevel: .error, format: format, arguments: arguments) + } + + public static func MPLogWarning(format: String, arguments: any CVarArg...) { + MPLogger(loggerLevel: .warning, format: format, arguments: arguments) + } + + public static func MPLogDebug(format: String, arguments: any CVarArg...) { + MPLogger(loggerLevel: .debug, format: format, arguments: arguments) + } + + public static func MPLogVerbose(format: String, arguments: any CVarArg...) { + MPLogger(loggerLevel: .verbose, format: format, arguments: arguments) + } +} diff --git a/mParticle-Apple-SDK/Utils/MPIHasher.swift b/mParticle-Apple-SDK/Utils/MPIHasher.swift index f12d9aa2..8fc350cf 100644 --- a/mParticle-Apple-SDK/Utils/MPIHasher.swift +++ b/mParticle-Apple-SDK/Utils/MPIHasher.swift @@ -25,6 +25,7 @@ import Foundation let lowercaseStringToHash = stringToHash.lowercased() guard let dataToHash = lowercaseStringToHash.data(using: .utf8) else { + MPLogger.MPLogWarning(format: "Hash String Failed. Could not encode string as data") return "" } @@ -38,6 +39,7 @@ import Foundation @objc public class func hashStringUTF16(_ stringToHash: String) -> String { guard let data = stringToHash.data(using: .utf16LittleEndian) else { + MPLogger.MPLogWarning(format: "Hash String UTF16 Failed. Could not encode string as data") return "" } let hash = hashFNV1a(data) From 50f0ba0d9291bc68764301bd155f7565dcbb63fb Mon Sep 17 00:00:00 2001 From: Brandon Stalnaker Date: Wed, 25 Sep 2024 10:48:04 -0400 Subject: [PATCH 2/3] address CR --- mParticle-Apple-SDK/Logger/MPLogger.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mParticle-Apple-SDK/Logger/MPLogger.swift b/mParticle-Apple-SDK/Logger/MPLogger.swift index bf3d6324..c387d770 100644 --- a/mParticle-Apple-SDK/Logger/MPLogger.swift +++ b/mParticle-Apple-SDK/Logger/MPLogger.swift @@ -9,10 +9,10 @@ import Foundation public struct MPLogger { - public static func MPLogger(loggerLevel: MPILogLevel, format: String, arguments: any CVarArg...) { + private static func MPLogger(loggerLevel: MPILogLevel, format: String, arguments: any CVarArg...) { if (MParticle.sharedInstance().logLevel.rawValue >= loggerLevel.rawValue && loggerLevel != .none) { let msg = String.localizedStringWithFormat(format, arguments) -// Custom Logger is marked as nonnull despite the fact that we expect it to be null if not set by the client +// TODO: Uncomment the following once MParticle.h converted to Swift. Custom Logger is marked as nonnull despite the fact that we expect it to be null if not set by the client // if let customLogger = MParticle.sharedInstance().customLogger { // customLogger(msg) // } else From 2e9bb13bcdcdf2756bdf0a2ea419f36f3962b0ff Mon Sep 17 00:00:00 2001 From: Brandon Stalnaker Date: Mon, 30 Sep 2024 12:36:58 -0400 Subject: [PATCH 3/3] change nullability --- mParticle-Apple-SDK/Include/mParticle.h | 2 +- mParticle-Apple-SDK/Logger/MPLogger.swift | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mParticle-Apple-SDK/Include/mParticle.h b/mParticle-Apple-SDK/Include/mParticle.h index d705e29f..0608c7cc 100644 --- a/mParticle-Apple-SDK/Include/mParticle.h +++ b/mParticle-Apple-SDK/Include/mParticle.h @@ -514,7 +514,7 @@ Defaults to false. Prevents the eventsHost above from overwriting the alias endp N.B.: The format/wording of mParticle log messages may change between releases--please avoid using this programatically to detect SDK behavior unless absolutely necessary, and then only as a temporary workaround. @see MParticleOptions */ -@property (nonatomic, copy, readwrite) void (^customLogger)(NSString *message); +@property (nonatomic, copy, nullable, readwrite) void (^customLogger)(NSString *message); /** Gets/Sets the opt-in/opt-out status for the application. Set it to YES to opt-out of event tracking. Set it to NO to opt-in of event tracking. diff --git a/mParticle-Apple-SDK/Logger/MPLogger.swift b/mParticle-Apple-SDK/Logger/MPLogger.swift index c387d770..1bd34cf1 100644 --- a/mParticle-Apple-SDK/Logger/MPLogger.swift +++ b/mParticle-Apple-SDK/Logger/MPLogger.swift @@ -12,11 +12,11 @@ public struct MPLogger { private static func MPLogger(loggerLevel: MPILogLevel, format: String, arguments: any CVarArg...) { if (MParticle.sharedInstance().logLevel.rawValue >= loggerLevel.rawValue && loggerLevel != .none) { let msg = String.localizedStringWithFormat(format, arguments) -// TODO: Uncomment the following once MParticle.h converted to Swift. Custom Logger is marked as nonnull despite the fact that we expect it to be null if not set by the client -// if let customLogger = MParticle.sharedInstance().customLogger { -// customLogger(msg) -// } else - NSLog("%@", msg) + if let customLogger = MParticle.sharedInstance().customLogger { + customLogger(msg) + } else { + NSLog("%@", msg) + } } }