Skip to content

Commit d9be5df

Browse files
authored
Add Logger typealias and Logging wrapper (#11)
1 parent a3f81b1 commit d9be5df

File tree

5 files changed

+114
-3
lines changed

5 files changed

+114
-3
lines changed

Sources/Cache/Cache/ExpiringCache.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
*/
1212
public class ExpiringCache<Key: Hashable, Value>: Cacheable {
1313
/// `Error` that reports expired values
14-
public struct ExpiriedValueError<Key: Hashable>: LocalizedError {
14+
public struct ExpiriedValueError: LocalizedError {
1515
/// Expired key
1616
public let key: Key
1717

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#if canImport(OSLog)
2+
import OSLog
3+
4+
/// Typealias for `os.Logger`
5+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
6+
public typealias Logger = os.Logger
7+
8+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
9+
extension Global {
10+
/// The global cache for Loggers
11+
public static var loggers: RequiredKeysCache<AnyHashable, Logger> = RequiredKeysCache()
12+
}
13+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if canImport(OSLog)
2+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
3+
@propertyWrapper public struct Logging<Key: Hashable> {
4+
/// The key associated with the Logger in the cache.
5+
public let key: Key
6+
7+
/// The `RequiredKeysCache` instance to resolve the dependency from.
8+
public let cache: RequiredKeysCache<Key, Logger>
9+
10+
/// The wrapped value that can be accessed and mutated by the property wrapper.
11+
public var wrappedValue: Logger {
12+
get {
13+
cache.resolve(requiredKey: key, as: Logger.self)
14+
}
15+
set {
16+
cache.set(value: newValue, forKey: key)
17+
}
18+
}
19+
20+
#if !os(Windows)
21+
/// Initializes the `Logging` property wrapper.
22+
///
23+
/// - Parameters:
24+
/// - key: The key associated with the Logger in the cache.
25+
/// - cache: The `RequiredKeysCache` instance to resolve the dependency from.
26+
public init(
27+
key: Key,
28+
using cache: RequiredKeysCache<Key, Logger> = Global.loggers
29+
) {
30+
self.key = key
31+
self.cache = cache
32+
33+
_ = self.cache.requiredKeys.insert(key)
34+
}
35+
#else
36+
/// Initializes the `Logging` property wrapper.
37+
///
38+
/// - Parameters:
39+
/// - key: The key associated with the Logger in the cache.
40+
/// - cache: The `RequiredKeysCache` instance to resolve the dependency from.
41+
public init(
42+
key: Key,
43+
using cache: RequiredKeysCache<Key, Any>
44+
) {
45+
self.key = key
46+
self.cache = cache
47+
48+
_ = self.cache.requiredKeys.insert(key)
49+
}
50+
#endif
51+
}
52+
#endif

Sources/Cache/PropertyWrappers/Resolved.swift

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
cache.set(value: newValue, forKey: key)
3232
}
3333
}
34-
35-
3634

3735
#if !os(Windows)
3836
/**

Tests/CacheTests/LoggingTests.swift

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import XCTest
2+
@testable import Cache
3+
4+
#if canImport(OSLog)
5+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
6+
final class LoggingTests: XCTestCase {
7+
func testLogging() {
8+
struct CachedValueObject {
9+
enum Key {
10+
case pi
11+
case value
12+
}
13+
14+
static let cache = RequiredKeysCache<Key, Logger>()
15+
16+
@Logging(key: Key.value, using: cache)
17+
var someValue: Logger
18+
}
19+
20+
CachedValueObject.cache.set(value: Logger(subsystem: "subsystem", category: "category"), forKey: .value)
21+
22+
let object = CachedValueObject()
23+
24+
object.someValue.log("Success")
25+
}
26+
27+
func testGloballyLogger() {
28+
struct CachedValueObject {
29+
enum Key {
30+
case pi
31+
case value
32+
}
33+
34+
@Logging(key: Key.value, using: Global.loggers)
35+
var someValue: Logger
36+
}
37+
38+
Global.loggers.set(
39+
value: Logger(subsystem: "subsystem", category: "category"),
40+
forKey: CachedValueObject.Key.value
41+
)
42+
43+
let object = CachedValueObject()
44+
45+
object.someValue.log("Success")
46+
}
47+
}
48+
#endif

0 commit comments

Comments
 (0)