Skip to content

Commit

Permalink
feat: Implement Copy-On-Write (CoW) behavior for Logger struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushi2103 committed May 9, 2024
1 parent c5991d7 commit a215ff0
Showing 1 changed file with 25 additions and 4 deletions.
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`.
private var _handler: Box<LogHandler>

/// An identifier of the creator of this `Logger`.
public let label: String

/// A computed property to access the `LogHandler`.
public var handler: LogHandler {
get {
return _handler.value
}
set {
if !(isKnownUniquelyReferenced(&_handler)) {
_handler = Box(value: newValue)
} else {
_handler.value = newValue
}
}
}

/// The metadata provider this logger was created with.
public var metadataProvider: Logger.MetadataProvider? {
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> {
var value: T
init(value: T) {
self.value = value
}
}

Expand Down

0 comments on commit a215ff0

Please sign in to comment.