Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Copy-On-Write (CoW) behavior for Logger struct #297

Merged
merged 21 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved

/// An identifier of the creator of this `Logger`.
public let label: String
@usableFromInline
var handler: LogHandler
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved

@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 {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
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? {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
return self.handler.metadataProvider
}

@usableFromInline
internal init(label: String, _ handler: LogHandler) {
self.label = label
self.handler = handler
self._storage = Storage(label: label, handler: handler)
}
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved

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 {
Expand Down