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
Changes from 1 commit
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
29 changes: 25 additions & 4 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,41 @@ import WASILibc
/// logger.info("Hello World!")
/// ```
public struct Logger {
@usableFromInline
var handler: LogHandler
/// A private property to hold the boxed `LogHandler`.
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
private var _handler: Box<LogHandler>

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

/// A computed property to access the `LogHandler`.
public var handler: LogHandler {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
get {
return _handler.value
}
set {
if !(isKnownUniquelyReferenced(&_handler)) {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
_handler = Box(value: newValue)
} else {
_handler.value = newValue
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// The metadata provider this logger was created with.
public var metadataProvider: Logger.MetadataProvider? {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
return self.handler.metadataProvider
return handler.metadataProvider
}

internal init(label: String, _ handler: LogHandler) {
self.label = label
self.handler = handler
self._handler = Box(value: handler)
}
}

private final class Box<T> {
ayushi2103 marked this conversation as resolved.
Show resolved Hide resolved
var value: T
init(value: T) {
self.value = value
}
}

Expand Down