Skip to content

Commit

Permalink
migrate os_log to new Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Nov 16, 2023
1 parent d532ff5 commit d145094
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 169 deletions.
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Clients/BetterCallDevClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class BetterCallDevClient {
completion(nil, nil)

case .failure(let error):
os_log(.error, log: .kukaiCoreSwift, "Parse error: %@", "\(error)")
Logger.kukaiCoreSwift.error("Parse error: \(error)")
completion(nil, KukaiError.internalApplicationError(error: error))
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/KukaiCoreSwift/Clients/TezosNodeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class TezosNodeClient {
self?.feeEstimatorService.estimate(operations: operations, operationMetadata: metadata, constants: constants, walletAddress: walletAddress, base58EncodedPublicKey: base58EncodedPublicKey, completion: completion)

case .failure(let error):
os_log(.error, log: .kukaiCoreSwift, "Unable to fetch metadata: %@", "\(error)")
Logger.kukaiCoreSwift.error("Unable to fetch metadata: \(error)")
completion(Result.failure(error))
}
}
Expand All @@ -160,7 +160,7 @@ public class TezosNodeClient {
self?.send(operationPayload: operationPayload, operationMetadata: metadata, withWallet: wallet, completion: completion)

case .failure(let error):
os_log(.error, log: .kukaiCoreSwift, "Unable to fetch metadata: %@", "\(error)")
Logger.kukaiCoreSwift.error("Unable to fetch metadata: \(error)")
completion(Result.failure(error))
}
}
Expand Down Expand Up @@ -317,7 +317,7 @@ public class TezosNodeClient {
dispatchGroup.enter()
dexterQueriesQueue.async { [weak self] in
guard let url = self?.config.primaryNodeURL else {
os_log(.default, log: .kukaiCoreSwift, "Invalid server url: %@", self?.config.primaryNodeURL.absoluteString ?? "nil")
Logger.kukaiCoreSwift.info("Invalid server url: \(self?.config.primaryNodeURL.absoluteString ?? "nil")")
completion(false, KukaiError.internalApplicationError(error: NetworkService.NetworkError.invalidURL))
return
}
Expand All @@ -338,7 +338,7 @@ public class TezosNodeClient {
dispatchGroup.enter()
dexterQueriesQueue.async { [weak self] in
guard let url = self?.config.primaryNodeURL else {
os_log(.default, log: .kukaiCoreSwift, "Invalid server url: %@", self?.config.primaryNodeURL.absoluteString ?? "nil")
Logger.kukaiCoreSwift.info("Invalid server url: \(self?.config.primaryNodeURL.absoluteString ?? "nil")")
completion(false, KukaiError.internalApplicationError(error: NetworkService.NetworkError.invalidURL))
return
}
Expand Down
21 changes: 10 additions & 11 deletions Sources/KukaiCoreSwift/Clients/TzKTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public class TzKTClient {
completion(operations, nil)

case .failure(let error):
os_log(.error, log: .kukaiCoreSwift, "Parse error: %@", "\(error)")
Logger.tzkt.error("Parse error: \(error)")
completion(nil, KukaiError.internalApplicationError(error: error))
}
}
Expand Down Expand Up @@ -636,12 +636,11 @@ public class TzKTClient {
signalrConnection = HubConnectionBuilder(url: url).build()
}


// Register for SignalR operation events
signalrConnection?.on(method: "accounts", callback: { [weak self] argumentExtractor in
do {
let obj = try argumentExtractor.getArgument(type: AccountSubscriptionResponse.self)
os_log("Incoming object parsed: %@", log: .tzkt, type: .default, "\(obj)")
Logger.tzkt.info("Incoming object parsed: \(String(describing: obj))")

if let data = obj.data {
var changedAddress: [String] = []
Expand All @@ -653,7 +652,7 @@ public class TzKTClient {
}

} catch (let error) {
os_log("Failed to parse incoming websocket data: %@", log: .tzkt, type: .error, "\(error)")
Logger.tzkt.error("Failed to parse incoming websocket data: \(error)")
self?.signalrConnection?.stop()
self?.isListening = false
//completion(false, error, KukaiError.internalApplicationError(error: error))
Expand All @@ -667,7 +666,7 @@ public class TzKTClient {
Close the websocket from `listenForAccountChanges`
*/
public func stopListeningForAccountChanges() {
os_log(.default, log: .kukaiCoreSwift, "Cancelling listenForAccountChanges")
Logger.tzkt.info("Cancelling listenForAccountChanges")
signalrConnection?.stop()
isListening = false
}
Expand Down Expand Up @@ -949,7 +948,7 @@ public class TzKTClient {
dispatchGroupTransactions.leave()

case .failure(let error):
os_log(.error, log: .kukaiCoreSwift, "Parse error 1: %@", "\(error)")
Logger.tzkt.error("Parse error 1: \(error)")
dispatchGroupTransactions.leave()
}
}
Expand All @@ -969,7 +968,7 @@ public class TzKTClient {
dispatchGroupTransactions.leave()

case .failure(let error):
os_log(.error, log: .kukaiCoreSwift, "Parse error 2: %@", "\(error)")
Logger.tzkt.error("Parse error 2: \(error)")
dispatchGroupTransactions.leave()
}
}
Expand Down Expand Up @@ -1095,16 +1094,16 @@ extension TzKTClient: HubConnectionDelegate {
let subscription = AccountSubscription(addresses: addressesToWatch)
signalrConnection?.invoke(method: "SubscribeToAccounts", subscription) { [weak self] error in
if let error = error {
os_log("Subscribe to account changes failed: %@", log: .tzkt, type: .error, "\(error)")
Logger.tzkt.error("Subscribe to account changes failed: \(error)")
self?.signalrConnection?.stop()
} else {
os_log("Subscribe to account changes succeeded, waiting for objects", log: .tzkt, type: .default)
Logger.tzkt.info("Subscribe to account changes succeeded, waiting for objects")
}
}
}

public func connectionDidClose(error: Error?) {
os_log("SignalR connection closed: %@", log: .tzkt, type: .default, String(describing: error))
Logger.tzkt.error("SignalR connection closed: \(error)")

if newAddressesToWatch.count > 0 {
self.listenForAccountChanges(addresses: newAddressesToWatch)
Expand All @@ -1113,6 +1112,6 @@ extension TzKTClient: HubConnectionDelegate {
}

public func connectionDidFailToOpen(error: Error) {
os_log("Failed to open SignalR connection to listen for changes: %@", log: .tzkt, type: .error, "\(error)")
Logger.tzkt.error("Failed to open SignalR connection to listen for changes: \(error)")
}
}
22 changes: 11 additions & 11 deletions Sources/KukaiCoreSwift/Extensions/OSLogs+categories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import Foundation
import os.log

/// Extension to OSLog to create some custom categories for logging
extension OSLog {
public extension Logger {
private static var subsystem = Bundle.main.bundleIdentifier ?? "app.kukai.kukai-core-swift"

static let kukaiCoreSwift = OSLog(subsystem: subsystem, category: "KukaiCoreSwift")
static let walletCache = OSLog(subsystem: subsystem, category: "WalletCache")
static let kukaiCoreSwiftError = OSLog(subsystem: subsystem, category: "KukaiCoreSwift-error")
static let keychain = OSLog(subsystem: subsystem, category: "KukaiCoreSwift-keychain")
static let network = OSLog(subsystem: subsystem, category: "KukaiCoreSwift-network")
static let bcd = OSLog(subsystem: subsystem, category: "BetterCallDev")
static let tzkt = OSLog(subsystem: subsystem, category: "TzKT")
static let taquitoService = OSLog(subsystem: subsystem, category: "TaquitoService")
static let torus = OSLog(subsystem: subsystem, category: "Torus")
static let ledger = OSLog(subsystem: subsystem, category: "Ledger")
static let kukaiCoreSwift = Logger(subsystem: subsystem, category: "KukaiCoreSwift")
static let walletCache = Logger(subsystem: subsystem, category: "WalletCache")
static let kukaiCoreSwiftError = Logger(subsystem: subsystem, category: "KukaiCoreSwift-error")
static let keychain = Logger(subsystem: subsystem, category: "KukaiCoreSwift-keychain")
static let network = Logger(subsystem: subsystem, category: "KukaiCoreSwift-network")
static let bcd = Logger(subsystem: subsystem, category: "BetterCallDev")
static let tzkt = Logger(subsystem: subsystem, category: "TzKT")
static let taquitoService = Logger(subsystem: subsystem, category: "TaquitoService")
static let torus = Logger(subsystem: subsystem, category: "Torus")
static let ledger = Logger(subsystem: subsystem, category: "Ledger")
}
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Extensions/URL+extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension URL {
if let u = urlComponents.url {
self = u
} else {
os_log("Unable to appendQueryItem %@ to URL", log: .kukaiCoreSwift, type: .error, name)
Logger.kukaiCoreSwift.error("Unable to appendQueryItem \(name) to URL")
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/KukaiCoreSwift/Factories/OperationFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class OperationFactory {

// Return empty array if `TokenAmount` is a negaitve value
if tokenAmount < TokenAmount.zeroBalance(decimalPlaces: tokenAmount.decimalPlaces) {
os_log(.error, log: .kukaiCoreSwift, "Negative value passed to OperationFactory.sendOperation")
Logger.kukaiCoreSwift.error("Negative value passed to OperationFactory.sendOperation")
return []
}

Expand All @@ -49,7 +49,7 @@ public class OperationFactory {

case .nonfungible:
// Can't send an entire NFT group, need to rethink this
os_log(.error, log: .kukaiCoreSwift, "Can't send an entire NFT group. Must send individual NFT's from token.nfts array, via the other sendOperation")
Logger.kukaiCoreSwift.error("Can't send an entire NFT group. Must send individual NFT's from token.nfts array, via the other sendOperation")
return []
}
}
Expand All @@ -66,7 +66,7 @@ public class OperationFactory {

// Return empty array if `amount` is a negaitve value
if amount < 0 {
os_log(.error, log: .kukaiCoreSwift, "Negative value passed to OperationFactory.sendOperation")
Logger.kukaiCoreSwift.error("Negative value passed to OperationFactory.sendOperation")
return []
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/KukaiCoreSwift/Models/RPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class RPC<T: Decodable> {
return try JSONEncoder().encode(encodable)

} catch(let error) {
os_log(.error, log: .kukaiCoreSwift, "Unable to encode object as string: %@", "\(error)")
Logger.kukaiCoreSwift.error("Unable to encode object as string: \(error)")
return nil
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ extension RPC where T == [OperationResponse] {
/// Creates an RPC to preapply an operation. This `OperationPayload` must have had its signature and protocol set
public static func preapply(operationPayload: OperationPayload) -> RPC<[OperationResponse]>? {
if operationPayload.signature == nil || operationPayload.protocol == nil {
os_log(.error, log: .kukaiCoreSwift, "RPC preapply was passed an operationPayload without a signature and/or protocol")
Logger.kukaiCoreSwift.error("RPC preapply was passed an operationPayload without a signature and/or protocol")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Models/RegularWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class RegularWallet: Wallet {
guard let privateKey = PrivateKey(base58String, signingCurve: .secp256k1),
let pubKey = KeyPair.secp256k1PublicKey(fromPrivateKeyBytes: privateKey.bytes),
let tempAddress = pubKey.publicKeyHash else {
os_log("Failed to construct private/public key", log: .kukaiCoreSwift, type: .error)
Logger.kukaiCoreSwift.error("Failed to construct private/public key")
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions Sources/KukaiCoreSwift/Models/Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class Token: Codable, CustomStringConvertible {

// TODO: make failable init
if let faVersion = faVersion, faVersion == .fa2 && tokenId == nil {
os_log("Error: FA2 tokens require having a tokenId set, %@", log: .kukaiCoreSwift, type: .error, name ?? tokenContractAddress ?? "")
Logger.kukaiCoreSwift.error("Error: FA2 tokens require having a tokenId set, \(name ?? tokenContractAddress ?? "")")
}
}

Expand All @@ -139,7 +139,8 @@ public class Token: Codable, CustomStringConvertible {

// TODO: make failable init
if let faVersion = faVersion, faVersion == .fa2 && tokenId == nil {
os_log("Error: FA2 tokens require having a tokenId set, %@", log: .kukaiCoreSwift, type: .error, name ?? tokenContractAddress ?? "")
weak var weakSelf = self
Logger.kukaiCoreSwift.error("Error: FA2 tokens require having a tokenId set, \(weakSelf?.name ?? weakSelf?.tokenContractAddress ?? "")")
}
}

Expand All @@ -164,7 +165,8 @@ public class Token: Codable, CustomStringConvertible {

// TODO: make failable init
if let faVersion = faVersion, faVersion == .fa2 && tokenId == nil {
os_log("Error: FA2 tokens require having a tokenId set, %@", log: .kukaiCoreSwift, type: .error, name ?? tokenContractAddress ?? "")
weak var weakSelf = self
Logger.kukaiCoreSwift.error("Error: FA2 tokens require having a tokenId set, \(weakSelf?.name ?? weakSelf?.tokenContractAddress ?? "")")
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/KukaiCoreSwift/Models/TokenAmount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class TokenAmount: Codable {
*/
public init?(fromRpcAmount rpcAmount: String, decimalPlaces: Int) {
guard CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: rpcAmount)) else {
os_log(.error, log: .kukaiCoreSwift, "Can't set balance on a Token of tokenType.empty, or pass in a string with non digit characters. Entered: %@", rpcAmount)
Logger.kukaiCoreSwift.error("Can't set balance on a Token of tokenType.empty, or pass in a string with non digit characters. Entered: \(rpcAmount)")
return nil
}

Expand Down Expand Up @@ -126,7 +126,7 @@ public class TokenAmount: Codable {
*/
public convenience init?(fromNormalisedAmount normalisedAmount: String, decimalPlaces: Int) {
guard let decimal = Decimal(string: normalisedAmount.replacingOccurrences(of: (Locale.current.decimalSeparator ?? "."), with: ".")) else {
os_log(.error, log: .kukaiCoreSwift, "Can't set balance as can't parse string")
Logger.kukaiCoreSwift.error("Can't set balance as can't parse string")
return nil
}

Expand Down Expand Up @@ -277,7 +277,7 @@ public class TokenAmount: Codable {
let result = (lhs.decimalPlaces == rhs.decimalPlaces)

if !result {
os_log(.error, log: .kukaiCoreSwift, "Arithmetic function is not possible between tokens with different decimal places. Ignoring operation.")
Logger.kukaiCoreSwift.error("Arithmetic function is not possible between tokens with different decimal places. Ignoring operation.")
}

return result
Expand Down
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Models/TorusWallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class TorusWallet: RegularWallet {
*/
public init?(authProvider: TorusAuthProvider, username: String?, userId: String?, profilePicture: String?, torusPrivateKey: String) {
guard let bytes = Sodium.shared.utils.hex2bin(torusPrivateKey) else {
os_log("Unable to convert hex to binary", log: .torus, type: .error)
Logger.torus.error("Unable to convert hex to binary")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/KukaiCoreSwift/Models/XTZAmount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class XTZAmount: TokenAmount {
*/
public convenience init?(fromNormalisedAmount normalisedAmount: String, decimalPlaces: Int) {
guard let decimal = Decimal(string: normalisedAmount.replacingOccurrences(of: (Locale.current.decimalSeparator ?? "."), with: ".")) else {
os_log(.error, log: .kukaiCoreSwift, "Can't set balance as can't parse string")
Logger.kukaiCoreSwift.error("Can't set balance as can't parse string")
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions Sources/KukaiCoreSwift/Services/DexCalculationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DexCalculationService {
private init() {
jsContext = JSContext()
jsContext.exceptionHandler = { context, exception in
os_log("Dex calculation JSContext exception: %@", log: .kukaiCoreSwift, type: .error, exception?.toString() ?? "")
Logger.kukaiCoreSwift.error("Dex calculation JSContext exception: \(exception?.toString() ?? "")")
}

if let jsSourcePath = Bundle.module.url(forResource: "kukai-dex-calculations", withExtension: "js") {
Expand All @@ -80,7 +80,7 @@ public class DexCalculationService {
self.jsContext.evaluateScript(jsSourceContents)

} catch (let error) {
os_log("Error parsing Dex calculation javascript file: %@", log: .kukaiCoreSwift, type: .error, "\(error)")
Logger.kukaiCoreSwift.error("Error parsing Dex calculation javascript file: \(error)")
}
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ public class DexCalculationService {
let token = tokenAmount.rpcRepresentation

guard slippage >= 0, slippage <= 1 else {
os_log("slippage value supplied to `xtzToTokenMinimumReturn` was not between 0 and 1: %@", log: .kukaiCoreSwift, type: .error, slippage)
Logger.kukaiCoreSwift.error("slippage value supplied to `xtzToTokenMinimumReturn` was not between 0 and 1: \(slippage)")
return nil
}

Expand Down Expand Up @@ -403,7 +403,7 @@ public class DexCalculationService {
let xtz = xtzAmount.rpcRepresentation

guard slippage >= 0, slippage <= 1 else {
os_log("slippage value supplied to `tokenToXtzMinimumReturn` was not between 0 and 1: %@", log: .kukaiCoreSwift, type: .error, slippage)
Logger.kukaiCoreSwift.error("slippage value supplied to `tokenToXtzMinimumReturn` was not between 0 and 1: \(slippage)")
return nil
}

Expand Down Expand Up @@ -545,7 +545,7 @@ public class DexCalculationService {
*/
public func addLiquidityReturn(xtzToDeposit: XTZAmount, xtzPool: XTZAmount, totalLiquidity: TokenAmount, slippage: Double, dex: DipDupExchangeName) -> (expected: TokenAmount, minimum: TokenAmount)? {
guard slippage >= 0, slippage <= 1 else {
os_log("slippage value supplied to `addLiquidityReturn` was not between 0 and 1: %@", log: .kukaiCoreSwift, type: .error, slippage)
Logger.kukaiCoreSwift.error("slippage value supplied to `addLiquidityReturn` was not between 0 and 1: \(slippage)")
return nil
}

Expand Down Expand Up @@ -629,7 +629,7 @@ public class DexCalculationService {
*/
public func removeLiquidityTokenReceived(liquidityBurned: TokenAmount, totalLiquidity: TokenAmount, tokenPool: TokenAmount, slippage: Double) -> (expected: TokenAmount, minimum: TokenAmount)? {
guard slippage >= 0, slippage <= 1 else {
os_log("slippage value supplied to `removeLiquidityTokenReceived` was not between 0 and 1: %@", log: .kukaiCoreSwift, type: .error, slippage)
Logger.kukaiCoreSwift.error("slippage value supplied to `removeLiquidityTokenReceived` was not between 0 and 1: \(slippage)")
return nil
}

Expand Down Expand Up @@ -664,7 +664,7 @@ public class DexCalculationService {
*/
public func removeLiquidityXtzReceived(liquidityBurned: TokenAmount, totalLiquidity: TokenAmount, xtzPool: XTZAmount, slippage: Double, dex: DipDupExchangeName) -> (expected: XTZAmount, minimum: XTZAmount)? {
guard slippage >= 0, slippage <= 1 else {
os_log("slippage value supplied to `removeLiquidityXtzReceived` was not between 0 and 1: %@", log: .kukaiCoreSwift, type: .error, slippage)
Logger.kukaiCoreSwift.error("slippage value supplied to `removeLiquidityXtzReceived` was not between 0 and 1: \(slippage)")
return nil
}

Expand Down
Loading

0 comments on commit d145094

Please sign in to comment.