Skip to content

Commit

Permalink
refactor: Port MPILogger to Swift (#295)
Browse files Browse the repository at this point in the history
* refactor: Port MPILogger to Swift

* address CR

* change nullability
  • Loading branch information
BrandonStalnaker authored Sep 30, 2024
1 parent 261102e commit 7828fed
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mParticle-Apple-SDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,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 */; };
D3CEDAC32C9DAC25001B32DF /* MPDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDAC22C9DAC25001B32DF /* MPDateFormatter.swift */; };
D3CEDAC42C9DB0E0001B32DF /* MPDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDAC22C9DAC25001B32DF /* MPDateFormatter.swift */; };
D3CEDAC52C9DB0E0001B32DF /* MPDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3CEDAC22C9DAC25001B32DF /* MPDateFormatter.swift */; };
Expand Down Expand Up @@ -802,6 +804,7 @@
D3BA75152B614E3D008C3C65 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
D3CEDAB82C9B14BF001B32DF /* MPCCPAConsent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MPCCPAConsent.swift; sourceTree = "<group>"; };
D3CEDABD2C9C5B5B001B32DF /* MPGDPRConsent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPGDPRConsent.swift; sourceTree = "<group>"; };
D3CEDAC72CA2F214001B32DF /* MPLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPLogger.swift; sourceTree = "<group>"; };
D3CEDAC22C9DAC25001B32DF /* MPDateFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDateFormatter.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -1112,6 +1115,7 @@
isa = PBXGroup;
children = (
53A79B4629CDFB1F00E7489F /* MPILogger.h */,
D3CEDAC72CA2F214001B32DF /* MPLogger.swift */,
);
path = Logger;
sourceTree = "<group>";
Expand Down Expand Up @@ -1796,6 +1800,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 */,
Expand Down Expand Up @@ -1965,6 +1970,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 */,
Expand Down
2 changes: 1 addition & 1 deletion mParticle-Apple-SDK/Include/mParticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,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.
Expand Down
38 changes: 38 additions & 0 deletions mParticle-Apple-SDK/Logger/MPLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// MPLogger.swift
// mParticle-Apple-SDK
//
// Created by Brandon Stalnaker on 9/22/24.
//

import Foundation

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)
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)
}
}
2 changes: 2 additions & 0 deletions mParticle-Apple-SDK/Utils/MPIHasher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand All @@ -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)
Expand Down

0 comments on commit 7828fed

Please sign in to comment.