From c84372dad2de57a181b5aad244c6b91925794113 Mon Sep 17 00:00:00 2001 From: aboedo Date: Fri, 20 Nov 2020 16:43:47 -0300 Subject: [PATCH] Logging v2 part 2 (#399) * improved logger for swift * added a few log entries * cleanup in a few more log entries * improved consistency in log messages --- Purchases/Public/RCPurchases.m | 9 +++++---- .../IntroEligibilityCalculator.swift | 5 ++++- .../LocalReceiptParsing/ReceiptParser.swift | 6 ++++-- PurchasesCoreSwift/Logging/Logger.swift | 16 ++++++++++++++++ .../Purchasing/ProductsManager.swift | 12 ++++++------ 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Purchases/Public/RCPurchases.m b/Purchases/Public/RCPurchases.m index 83a68823e0..d2b1200b2f 100644 --- a/Purchases/Public/RCPurchases.m +++ b/Purchases/Public/RCPurchases.m @@ -590,7 +590,7 @@ - (void)syncPurchasesWithReceiptRefreshPolicy:(RCReceiptRefreshPolicy)refreshPol isRestore:(BOOL)isRestore completion:(nullable RCReceivePurchaserInfoBlock)completion { if (!self.allowSharingAppStoreAccount) { - RCDebugLog(@"allowSharingAppStoreAccount is set to false and restoreTransactions has been called. " + RCWarnLog(@"allowSharingAppStoreAccount is set to false and restoreTransactions has been called. " "Are you sure you want to do this?"); } // Refresh the receipt and post to backend, this will allow the transactions to be transferred. @@ -598,8 +598,8 @@ - (void)syncPurchasesWithReceiptRefreshPolicy:(RCReceiptRefreshPolicy)refreshPol [self receiptDataWithReceiptRefreshPolicy:refreshPolicy completion:^(NSData *_Nonnull data) { if (data.length == 0) { if (RCSystemInfo.isSandbox) { - RCLog(@"App running on sandbox without a receipt file. Restoring transactions won't work unless " - "you've purchased before and there is a receipt available."); + RCWarnLog(@"App running on sandbox without a receipt file. Restoring transactions won't work unless " + "you've purchased before and there is a receipt available."); } CALL_IF_SET_ON_MAIN_THREAD(completion, nil, [RCPurchasesErrorUtils missingReceiptFileError]); return; @@ -671,7 +671,8 @@ - (void)checkTrialOrIntroductoryPriceEligibility:(NSArray *)productI CALL_IF_SET_ON_MAIN_THREAD(receiveEligibility, convertedEligibility); } else { - NSLog(@"There was an error when trying to parse the receipt locally, details: %@", error.localizedDescription); + RCErrorLog(@"There was an error when trying to parse the receipt locally, details: %@", + error.localizedDescription); [self.backend getIntroEligibilityForAppUserID:self.appUserID receiptData:data productIdentifiers:productIdentifiers diff --git a/PurchasesCoreSwift/IntroEligibilityCalculator.swift b/PurchasesCoreSwift/IntroEligibilityCalculator.swift index f0d24395b2..2883055e86 100644 --- a/PurchasesCoreSwift/IntroEligibilityCalculator.swift +++ b/PurchasesCoreSwift/IntroEligibilityCalculator.swift @@ -32,7 +32,8 @@ import StoreKit completion([:], nil) return } - + Logger.debug("attempting to check intro eligibility locally") + var result: [String: NSNumber] = candidateProductIdentifiers.reduce(into: [:]) { resultDict, productId in resultDict[productId] = IntroEligibilityStatus.unknown.toNSNumber() } @@ -52,10 +53,12 @@ import StoreKit purchasedProductsWithIntroOffers: purchasedProductsWithIntroOffersOrFreeTrials) result.merge(eligibility) { (_, new) in new } + Logger.debug("local intro eligibility computed locally. Result: \(result)") completion(result, nil) } } catch let error { + Logger.error("couldn't check intro eligibility locally, error: \(error.localizedDescription)") completion([:], error) return } diff --git a/PurchasesCoreSwift/LocalReceiptParsing/ReceiptParser.swift b/PurchasesCoreSwift/LocalReceiptParsing/ReceiptParser.swift index 9fabc269a7..ee458410ef 100644 --- a/PurchasesCoreSwift/LocalReceiptParsing/ReceiptParser.swift +++ b/PurchasesCoreSwift/LocalReceiptParsing/ReceiptParser.swift @@ -29,12 +29,12 @@ import Foundation } @objc public func receiptHasTransactions(receiptData: Data) -> Bool { - Logger.log(level: .info, message: "Parsing local receipt") + Logger.info("Parsing receipt") if let receipt = try? parse(from: receiptData) { return receipt.inAppPurchases.count > 0 } - Logger.log(level: .warn, message: "\(#file) \(#function) Could not parse receipt, conservatively returning true") + Logger.warn("\(#file)-\(#function): Could not parse receipt, conservatively returning true") return true } @@ -44,9 +44,11 @@ import Foundation let asn1Container = try containerBuilder.build(fromPayload: ArraySlice(intData)) guard let receiptASN1Container = try findASN1Container(withObjectId: ASN1ObjectIdentifier.data, inContainer: asn1Container) else { + Logger.error("the data object identifier couldn't be found on the receipt") throw ReceiptReadingError.dataObjectIdentifierMissing } let receipt = try receiptBuilder.build(fromContainer: receiptASN1Container) + Logger.info("receipt parsed successfully") return receipt } } diff --git a/PurchasesCoreSwift/Logging/Logger.swift b/PurchasesCoreSwift/Logging/Logger.swift index 40296a9a5d..b54c6d80cc 100644 --- a/PurchasesCoreSwift/Logging/Logger.swift +++ b/PurchasesCoreSwift/Logging/Logger.swift @@ -29,4 +29,20 @@ import Foundation guard level != .debug || shouldShowDebugLogs else { return } NSLog("[\(frameworkDescription)] - \(level.description()): \(message)") } + + static func debug(_ message: String) { + log(level: .debug, message: message) + } + + static func info(_ message: String) { + log(level: .debug, message: message) + } + + static func warn(_ message: String) { + log(level: .debug, message: message) + } + + static func error(_ message: String) { + log(level: .debug, message: message) + } } diff --git a/PurchasesCoreSwift/Purchasing/ProductsManager.swift b/PurchasesCoreSwift/Purchasing/ProductsManager.swift index 50ddff2f0e..ef0ccaabb6 100644 --- a/PurchasesCoreSwift/Purchasing/ProductsManager.swift +++ b/PurchasesCoreSwift/Purchasing/ProductsManager.swift @@ -26,18 +26,18 @@ internal class ProductsManager: NSObject { let productsAlreadyCached = self.cachedProductsByIdentifier.filter { key, _ in identifiers.contains(key) } if productsAlreadyCached.count == identifiers.count { let productsAlreadyCachedSet = Set(productsAlreadyCached.values) - NSLog("skipping products request because products were already cached. products: \(identifiers)") + Logger.debug("skipping products request because products were already cached. products: \(identifiers)") completion(productsAlreadyCachedSet) return } if let existingHandlers = self.completionHandlers[identifiers] { - NSLog("found an existing request for products: \(identifiers), appending to completion") + Logger.debug("found an existing request for products: \(identifiers), appending to completion") self.completionHandlers[identifiers] = existingHandlers + [completion] return } - NSLog("no existing requests and products not cached, starting SKProducts request for: \(identifiers)") + Logger.debug("no existing requests and products not cached, starting SKProducts request for: \(identifiers)") let request = self.productsRequestFactory.request(productIdentifiers: identifiers) request.delegate = self self.completionHandlers[identifiers] = [completion] @@ -51,7 +51,7 @@ extension ProductsManager: SKProductsRequestDelegate { func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { queue.async { [self] in - NSLog("products request received response") + Logger.debug("SKProductsRequest request received response") guard let requestProducts = self.productsByRequests[request] else { fatalError("couldn't find request") } guard let completionBlocks = self.completionHandlers[requestProducts] else { fatalError("couldn't find completion") @@ -67,12 +67,12 @@ extension ProductsManager: SKProductsRequestDelegate { } func requestDidFinish(_ request: SKRequest) { - NSLog("request did finish") + Logger.debug("SKProductsRequest did finish") } func request(_ request: SKRequest, didFailWithError error: Error) { queue.async { [self] in - NSLog("products request failed! error: \(error.localizedDescription)") + Logger.error("SKProductsRequest failed! error: \(error.localizedDescription)") guard let products = self.productsByRequests[request] else { fatalError("couldn't find request") } guard let completionBlocks = self.completionHandlers[products] else { fatalError("couldn't find completion")