Skip to content

Commit

Permalink
Logging v2 part 2 (#399)
Browse files Browse the repository at this point in the history
* improved logger for swift

* added a few log entries

* cleanup in a few more log entries

* improved consistency in log messages
  • Loading branch information
aboedo committed Nov 20, 2020
1 parent 53fed76 commit c84372d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
9 changes: 5 additions & 4 deletions Purchases/Public/RCPurchases.m
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,16 @@ - (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.
// https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StoreKitGuide/Chapters/Restoring.html
[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;
Expand Down Expand Up @@ -671,7 +671,8 @@ - (void)checkTrialOrIntroductoryPriceEligibility:(NSArray<NSString *> *)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
Expand Down
5 changes: 4 additions & 1 deletion PurchasesCoreSwift/IntroEligibilityCalculator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions PurchasesCoreSwift/LocalReceiptParsing/ReceiptParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
}
Expand Down
16 changes: 16 additions & 0 deletions PurchasesCoreSwift/Logging/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 6 additions & 6 deletions PurchasesCoreSwift/Purchasing/ProductsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit c84372d

Please sign in to comment.