Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Port MPILogger to Swift #295

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mParticle-Apple-SDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -807,6 +809,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>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -1117,6 +1120,7 @@
isa = PBXGroup;
children = (
53A79B4629CDFB1F00E7489F /* MPILogger.h */,
D3CEDAC72CA2F214001B32DF /* MPLogger.swift */,
);
path = Logger;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
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 @@ -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.
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