mirror of
https://github.com/apple/swift-log
synced 2026-11-30 04:18:44 +00:00
feat: Implement Copy-On-Write (CoW) behavior for Logger struct (#297)
* feat: Implement Copy-On-Write (CoW) behavior for Logger struct * fix: refactored the struct and added test * fix: refactored the struct * fix: refactored the struct * fix:Added compiler directive and @usableFromInline/@inlinable * fix: Add endif label * fix: Remove colon causing expected type * fix: Add inlinable and moving compiler derective * nit * fix: add inlinable * Update Sources/Logging/Logging.swift Co-authored-by: Johannes Weiss <johannesweiss@apple.com> * Update Sources/Logging/Logging.swift Co-authored-by: Johannes Weiss <johannesweiss@apple.com> * Update Sources/Logging/Logging.swift Co-authored-by: Franz Busch <privat@franz-busch.de> * Update Sources/Logging/Logging.swift Co-authored-by: Franz Busch <privat@franz-busch.de> * fix: add self to class declaration and fixed formatting * fix: make label public * fix: format * fix: remove setter * test: added a test case --------- Co-authored-by: Johannes Weiss <johannesweiss@apple.com> Co-authored-by: Franz Busch <privat@franz-busch.de>
This commit is contained in:
2 files changed
+55
-5
No files matched your search
@@ -39,20 +39,56 @@ import WASILibc
|
||||
/// logger.info("Hello World!")
|
||||
/// ```
|
||||
public struct Logger {
|
||||
/// Storage class to hold the label and log handler
|
||||
@usableFromInline
|
||||
var handler: LogHandler
|
||||
internal final class Storage: @unchecked /* and not actually */ Sendable /* but safe if only used with CoW */ {
|
||||
@usableFromInline
|
||||
var label: String
|
||||
|
||||
/// An identifier of the creator of this `Logger`.
|
||||
public let label: String
|
||||
@usableFromInline
|
||||
var handler: LogHandler
|
||||
|
||||
@inlinable
|
||||
init(label: String, handler: LogHandler) {
|
||||
self.label = label
|
||||
self.handler = handler
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func copy() -> Storage {
|
||||
return Storage(label: self.label, handler: self.handler)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal var _storage: Storage
|
||||
public var label: String {
|
||||
return self._storage.label
|
||||
}
|
||||
|
||||
/// A computed property to access the `LogHandler`.
|
||||
@inlinable
|
||||
public var handler: LogHandler {
|
||||
get {
|
||||
return self._storage.handler
|
||||
}
|
||||
set {
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.handler = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The metadata provider this logger was created with.
|
||||
@inlinable
|
||||
public var metadataProvider: Logger.MetadataProvider? {
|
||||
return self.handler.metadataProvider
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal init(label: String, _ handler: LogHandler) {
|
||||
self.label = label
|
||||
self.handler = handler
|
||||
self._storage = Storage(label: label, handler: handler)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1039,6 +1039,20 @@ class LoggingTest: XCTestCase {
|
||||
LoggingSystem.bootstrap(StreamLogHandler.standardOutput, metadataProvider: .exampleMetadataProvider)
|
||||
LoggingSystem.bootstrap(StreamLogHandler.standardError, metadataProvider: .exampleMetadataProvider)
|
||||
}
|
||||
|
||||
func testLoggerIsJustHoldingASinglePointer() {
|
||||
let expectedSize = MemoryLayout<UnsafeRawPointer>.size
|
||||
XCTAssertEqual(MemoryLayout<Logger>.size, expectedSize)
|
||||
}
|
||||
|
||||
func testLoggerCopyOnWrite() {
|
||||
var logger1 = Logger(label: "foo")
|
||||
logger1.logLevel = .error
|
||||
var logger2 = logger1
|
||||
logger2.logLevel = .trace
|
||||
XCTAssertEqual(.error, logger1.logLevel)
|
||||
XCTAssertEqual(.trace, logger2.logLevel)
|
||||
}
|
||||
}
|
||||
|
||||
extension Logger {
|
||||
|
||||
Reference in new issue
Block a user