-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Logger typealias and Logging wrapper (#11)
- Loading branch information
Showing
5 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,6 @@ | |
cache.set(value: newValue, forKey: key) | ||
} | ||
} | ||
|
||
|
||
|
||
#if !os(Windows) | ||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |