From 00e51ec89571a76d11d08a384e454cd4ae286096 Mon Sep 17 00:00:00 2001 From: bullinnyc Date: Sun, 11 Feb 2024 10:44:57 +0700 Subject: [PATCH] Add logger. --- .../Extensions/Extension + Logger.swift | 22 +++++++++++++++ Sources/CachedAsyncImage/Services/Emoji.swift | 27 +++++++++++++++++++ .../Services/ImageLoader.swift | 25 ++++++++++------- .../Services/NetworkManager.swift | 6 +++-- 4 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 Sources/CachedAsyncImage/Extensions/Extension + Logger.swift create mode 100644 Sources/CachedAsyncImage/Services/Emoji.swift diff --git a/Sources/CachedAsyncImage/Extensions/Extension + Logger.swift b/Sources/CachedAsyncImage/Extensions/Extension + Logger.swift new file mode 100644 index 0000000..b576171 --- /dev/null +++ b/Sources/CachedAsyncImage/Extensions/Extension + Logger.swift @@ -0,0 +1,22 @@ +// +// Extension + Logger.swift +// CachedAsyncImage +// +// Created by Dmitry Kononchuk on 24.01.2024. +// Copyright © 2024 Dmitry Kononchuk. All rights reserved. +// + +import OSLog + +typealias Log = Logger + +extension Logger { + // MARK: - Public Properties + + /// Log of failure. + static let failure = Logger(subsystem: subsystem, category: "failure") + + // MARK: - Private Properties + + private static let subsystem = Bundle.module.bundleIdentifier ?? "" +} diff --git a/Sources/CachedAsyncImage/Services/Emoji.swift b/Sources/CachedAsyncImage/Services/Emoji.swift new file mode 100644 index 0000000..478f510 --- /dev/null +++ b/Sources/CachedAsyncImage/Services/Emoji.swift @@ -0,0 +1,27 @@ +// +// Emoji.swift +// CachedAsyncImage +// +// Created by Dmitry Kononchuk on 25.01.2024. +// Copyright © 2024 Dmitry Kononchuk. All rights reserved. +// + +import Foundation + +final class Emoji { + // MARK: - Public Enums + + enum Code: String { + case hammer = "1f528" + } + + // MARK: - Public Methods + + static func getEmoji(from code: Code) -> String? { + guard let number = Int(code.rawValue, radix: 16), + let unicodeScalar = UnicodeScalar(number) + else { return nil } + + return String(unicodeScalar) + } +} diff --git a/Sources/CachedAsyncImage/Services/ImageLoader.swift b/Sources/CachedAsyncImage/Services/ImageLoader.swift index 22169a9..c4a2067 100644 --- a/Sources/CachedAsyncImage/Services/ImageLoader.swift +++ b/Sources/CachedAsyncImage/Services/ImageLoader.swift @@ -52,14 +52,12 @@ final class ImageLoader: ObservableObject { func fetchImage(from url: String) { if case .loading = state { return } - let url = URL(string: url) - - if let url = url, let cachedImage = imageCache[url] { + if let url = URL(string: url), let cachedImage = imageCache[url] { state = .loaded(cachedImage) return } - let (progress, data) = networkManager.fetchImage(from: url) + let (progress, data) = networkManager.fetchImage(from: URL(string: url)) progress? .publisher(for: \.fractionCompleted) @@ -71,15 +69,13 @@ final class ImageLoader: ObservableObject { data .map { CPImage(data: $0) } - .catch { error -> AnyPublisher in + .catch { [weak self] error -> AnyPublisher in if let error = error as? NetworkError { Task { @MainActor [weak self] in self?.state = .failed(error.rawValue) } - #if DEBUG - print("**** CachedAsyncImage error: \(error.rawValue)") - #endif + self?.log(error.rawValue, url: url) } return Just(nil).eraseToAnyPublisher() @@ -91,7 +87,7 @@ final class ImageLoader: ObservableObject { } }, receiveOutput: { [weak self] in - self?.cache(url: url, image: $0) + self?.cache(url: URL(string: url), image: $0) } ) .subscribe(on: Self.imageProcessing) @@ -113,4 +109,15 @@ final class ImageLoader: ObservableObject { private func cancel() { cancellables.forEach { $0.cancel() } } + + private func log(_ error: String, url: String) { + guard let emoji = Emoji.getEmoji(from: .hammer) else { return } + + let errorMessage = "Error: \(error)" + let urlMessage = "URL: \(url)" + + Log.failure.error( + "\(emoji) CachedAsyncImage\n\(errorMessage)\n\(urlMessage)" + ) + } } diff --git a/Sources/CachedAsyncImage/Services/NetworkManager.swift b/Sources/CachedAsyncImage/Services/NetworkManager.swift index a235319..8b900e0 100644 --- a/Sources/CachedAsyncImage/Services/NetworkManager.swift +++ b/Sources/CachedAsyncImage/Services/NetworkManager.swift @@ -47,8 +47,10 @@ struct NetworkManager: NetworkProtocol { publisher: AnyPublisher ) { guard let url = url else { - return (nil, Fail(error: NetworkError.badURL()) - .eraseToAnyPublisher()) + return ( + nil, + Fail(error: NetworkError.badURL()).eraseToAnyPublisher() + ) } let sharedPublisher = URLSession.shared