Skip to content

Commit

Permalink
Minor format/consistency updates to logging. (#5)
Browse files Browse the repository at this point in the history
Minor format/consistency updates to logging.
  • Loading branch information
jakehawken committed Jul 29, 2022
1 parent 1b65460 commit 0eefaa9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
50 changes: 29 additions & 21 deletions Sources/Propagate/PublisherAndSubscriber/Publisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Publisher<T, E: Error> {
fileprivate var subscribers = WeakBag<Subscriber<T, E>>()
private(set) public var isCancelled = false

internal var loggingCombo: (LoggingCombo)?
internal var loggingCombo: LoggingCombo?

public init() {
}
Expand All @@ -35,7 +35,11 @@ public class Publisher<T, E: Error> {
}

deinit {
safePrint("Releasing \(self) from memory.", logType: .lifeCycle, loggingCombo: loggingCombo)
safePrint(
"Releasing \(self) from memory.",
logType: .lifeCycle,
loggingCombo: loggingCombo
)
// The asynchronous cancelAll() can't be called from deinit
// because it results in a bad access crash.
handleCancellation()
Expand All @@ -48,17 +52,19 @@ public class Publisher<T, E: Error> {
/// `cancelAll()`, which will remove all subscribers and emit the `.cancelled`
/// state on each of them.
public func subscriber() -> Subscriber<T, E> {
/*
Any changes made to this function's implementation will
also need to be made to the same method on StatefulPublisher.
*/
let newSub = Subscriber(canceller: .init { [weak self] in
self?.removeSubscriber($0)
})
/// Any changes made in this method that need to shared between
/// this and any subclasses should be added to the below helper method.
return addNewSubscriberAndLog(newSub)
}

fileprivate func addNewSubscriberAndLog(_ newSub: Subscriber<T,E>) -> Subscriber<T,E> {
lockQueue.async { [weak self] in
self?.subscribers.insert(newSub)
}
safePrint("Generating new subscriber: \(newSub) from \(self)", logType: .lifeCycle, loggingCombo: loggingCombo)
logPublisher(self, generatingNewSubscriber: newSub, loggingCombo: loggingCombo)
return newSub
}

Expand Down Expand Up @@ -122,7 +128,7 @@ extension Publisher: PropagateDebuggable {
@discardableResult public func enableLogging(
logLevel: DebugLogLevel = .all,
_ additionalMessage: String = "",
_ logMethod: LoggingMethod = .debugPrint
_ logMethod: LoggingMethod
) -> Self {
self.loggingCombo = (logLevel, additionalMessage, logMethod)
return self
Expand Down Expand Up @@ -219,24 +225,14 @@ public class StatefulPublisher<T,E: Error>: Publisher<T, E> {
}

public override func subscriber() -> Subscriber<T, E> {
/*
Any changes made to this function's implementation will
also need to be made to the same method on Publisher.
*/
let newSub = OnSubscribeCallbackSubscriber(canceller: .init { [weak self] in
self?.removeSubscriber($0)
})
newSub.lastStateCallback = { [weak self] in self?.lastState }

lockQueue.async { [weak self] in
self?.subscribers.insert(newSub)
}
safePrint(
"Generating new subscriber. -- \(self) --> \(newSub)",
logType: .lifeCycle,
loggingCombo: loggingCombo
)
return newSub
/// Any changes made in this method that need to shared between
/// this and the superclass should be added to the below helper method.
return addNewSubscriberAndLog(newSub)
}

}
Expand All @@ -248,3 +244,15 @@ public extension Publisher where T == Void {
}

}

private func logPublisher<T,E:Error>(
_ pub: Publisher<T,E>,
generatingNewSubscriber sub: Subscriber<T,E>,
loggingCombo: LoggingCombo?
) {
safePrint(
"Instance \(memoryAddressStringFor(pub)) generating new subscriber --> \(sub)",
logType: .lifeCycle,
loggingCombo: loggingCombo
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public extension Subscriber {
boundStatefulPublisher()
.subscriber()
.onCancelled {
_ = self // Capturing self to keep subscriber alive for easier chaining.
// Capturing self to keep subscriber alive for easier chaining.
_ = self
}
}

Expand All @@ -21,7 +22,8 @@ public extension Subscriber {
publisher.publish(value)
return publisher.subscriber()
.onCancelled {
_ = self // Capturing self to keep subscriber alive for easier chaining.
// Capturing self to keep subscriber alive for easier chaining.
_ = self
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Propagate/PublisherAndSubscriber/Subscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extension Subscriber: PropagateDebuggable {
@discardableResult public func enableLogging(
logLevel: DebugLogLevel = .all,
_ additionalMessage: String = "",
_ logMethod: LoggingMethod = .debugPrint
_ logMethod: LoggingMethod
) -> Self {
self.loggingCombo = (logLevel, additionalMessage, logMethod)
return self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ extension ValueOnlySubscriber: PropagateDebuggable, CustomStringConvertible {
@discardableResult public func enableLogging(
logLevel: DebugLogLevel = .all,
_ additionalMessage: String = "",
_ logMethod: LoggingMethod = .debugPrint
_ logMethod: LoggingMethod
) -> Self {
self.loggingCombo = (logLevel, additionalMessage, logMethod)
return self
Expand Down

0 comments on commit 0eefaa9

Please sign in to comment.