Skip to content

Commit

Permalink
Add Logger typealias and Logging wrapper (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLeif authored Jul 7, 2023
1 parent a3f81b1 commit d9be5df
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/Cache/Cache/ExpiringCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
*/
public class ExpiringCache<Key: Hashable, Value>: Cacheable {
/// `Error` that reports expired values
public struct ExpiriedValueError<Key: Hashable>: LocalizedError {
public struct ExpiriedValueError: LocalizedError {
/// Expired key
public let key: Key

Expand Down
13 changes: 13 additions & 0 deletions Sources/Cache/Global/Global+loggers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if canImport(OSLog)
import OSLog

/// Typealias for `os.Logger`
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
public typealias Logger = os.Logger

@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension Global {
/// The global cache for Loggers
public static var loggers: RequiredKeysCache<AnyHashable, Logger> = RequiredKeysCache()
}
#endif
52 changes: 52 additions & 0 deletions Sources/Cache/PropertyWrappers/Logging.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#if canImport(OSLog)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@propertyWrapper public struct Logging<Key: Hashable> {
/// The key associated with the Logger in the cache.
public let key: Key

/// The `RequiredKeysCache` instance to resolve the dependency from.
public let cache: RequiredKeysCache<Key, Logger>

/// The wrapped value that can be accessed and mutated by the property wrapper.
public var wrappedValue: Logger {
get {
cache.resolve(requiredKey: key, as: Logger.self)
}
set {
cache.set(value: newValue, forKey: key)
}
}

#if !os(Windows)
/// Initializes the `Logging` property wrapper.
///
/// - Parameters:
/// - key: The key associated with the Logger in the cache.
/// - cache: The `RequiredKeysCache` instance to resolve the dependency from.
public init(
key: Key,
using cache: RequiredKeysCache<Key, Logger> = Global.loggers
) {
self.key = key
self.cache = cache

_ = self.cache.requiredKeys.insert(key)
}
#else
/// Initializes the `Logging` property wrapper.
///
/// - Parameters:
/// - key: The key associated with the Logger in the cache.
/// - cache: The `RequiredKeysCache` instance to resolve the dependency from.
public init(
key: Key,
using cache: RequiredKeysCache<Key, Any>
) {
self.key = key
self.cache = cache

_ = self.cache.requiredKeys.insert(key)
}
#endif
}
#endif
2 changes: 0 additions & 2 deletions Sources/Cache/PropertyWrappers/Resolved.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
cache.set(value: newValue, forKey: key)
}
}



#if !os(Windows)
/**
Expand Down
48 changes: 48 additions & 0 deletions Tests/CacheTests/LoggingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import XCTest
@testable import Cache

#if canImport(OSLog)
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
final class LoggingTests: XCTestCase {
func testLogging() {
struct CachedValueObject {
enum Key {
case pi
case value
}

static let cache = RequiredKeysCache<Key, Logger>()

@Logging(key: Key.value, using: cache)
var someValue: Logger
}

CachedValueObject.cache.set(value: Logger(subsystem: "subsystem", category: "category"), forKey: .value)

let object = CachedValueObject()

object.someValue.log("Success")
}

func testGloballyLogger() {
struct CachedValueObject {
enum Key {
case pi
case value
}

@Logging(key: Key.value, using: Global.loggers)
var someValue: Logger
}

Global.loggers.set(
value: Logger(subsystem: "subsystem", category: "category"),
forKey: CachedValueObject.Key.value
)

let object = CachedValueObject()

object.someValue.log("Success")
}
}
#endif

0 comments on commit d9be5df

Please sign in to comment.