Skip to content

Commit

Permalink
Merge pull request #9 from bullinnyc/add-logger
Browse files Browse the repository at this point in the history
Add logger.
  • Loading branch information
bullinnyc authored Feb 11, 2024
2 parents 0fc8873 + 00e51ec commit 43f008c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
22 changes: 22 additions & 0 deletions Sources/CachedAsyncImage/Extensions/Extension + Logger.swift
Original file line number Diff line number Diff line change
@@ -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 ?? ""
}
27 changes: 27 additions & 0 deletions Sources/CachedAsyncImage/Services/Emoji.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
25 changes: 16 additions & 9 deletions Sources/CachedAsyncImage/Services/ImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -71,15 +69,13 @@ final class ImageLoader: ObservableObject {

data
.map { CPImage(data: $0) }
.catch { error -> AnyPublisher<CPImage?, Never> in
.catch { [weak self] error -> AnyPublisher<CPImage?, Never> 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()
Expand All @@ -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)
Expand All @@ -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)"
)
}
}
6 changes: 4 additions & 2 deletions Sources/CachedAsyncImage/Services/NetworkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ struct NetworkManager: NetworkProtocol {
publisher: AnyPublisher<Data, Error>
) {
guard let url = url else {
return (nil, Fail(error: NetworkError.badURL())
.eraseToAnyPublisher())
return (
nil,
Fail(error: NetworkError.badURL()).eraseToAnyPublisher()
)
}

let sharedPublisher = URLSession.shared
Expand Down

0 comments on commit 43f008c

Please sign in to comment.