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 12 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
55 changes: 48 additions & 7 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,61 @@ import WASILibc
/// ```swift
/// logger.info("Hello World!")
/// ```

ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
public struct Logger {
/// Storage class to hold the label and log handler
@usableFromInline
internal final class Storage {
@usableFromInline
var label: String
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
@usableFromInline
var handler: LogHandler
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
@usableFromInline
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
init(label: String, handler: LogHandler) {
self.label = label
self.handler = handler
}
}

@usableFromInline
internal var _storage: Storage
@usableFromInline
var handler: LogHandler
internal var label: String {
FranzBusch marked this conversation as resolved.
Show resolved Hide resolved
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
get {
return _storage.label
}
set {
if !isKnownUniquelyReferenced(&_storage) {
_storage = Storage(label: newValue, handler: _storage.handler)
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
} else {
_storage.label = newValue
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// An identifier of the creator of this `Logger`.
public let label: String
/// A computed property to access the `LogHandler`.
@inlinable
public var handler: LogHandler {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
get {
return _storage.handler
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
}
set {
if !isKnownUniquelyReferenced(&_storage) {
_storage = Storage(label: label, handler: newValue)
} else {
_storage.handler = newValue
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// 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
return handler.metadataProvider
}

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

Expand Down Expand Up @@ -1330,6 +1370,7 @@ private final class WarnOnceBox {
// MARK: - Sendable support helpers

extension Logger.MetadataValue: Sendable {}
extension Logger.Storage: @unchecked Sendable {}
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
extension Logger: Sendable {}
extension Logger.Level: Sendable {}
extension Logger.Message: Sendable {}
5 changes: 5 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,11 @@ 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
}

extension Logger {
Expand Down