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:
authored and GitHub committed 2024-05-30 11:18:30 +01:00
1 parent c7dcb55fb9
commit 23517234d5
2 files changed
+55 -5

No files matched your search

+41 -5
View File
@@ -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)
}
}
+14
View File
@@ -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 {