From cbd70ea7ca8c41e2a68475db0fbe0daa72406668 Mon Sep 17 00:00:00 2001 From: Eric Rabil Date: Wed, 3 Aug 2022 18:40:47 -0400 Subject: [PATCH] Report to unified logging when a promise violation is detected --- Sources/Pwomise/Pwomise.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/Pwomise/Pwomise.swift b/Sources/Pwomise/Pwomise.swift index 6e9503d..65c46c7 100644 --- a/Sources/Pwomise/Pwomise.swift +++ b/Sources/Pwomise/Pwomise.swift @@ -1,4 +1,5 @@ import Foundation +import Swog public protocol PromiseConvertible { associatedtype Output @@ -16,6 +17,7 @@ public protocol PromiseAbuseDelegate { func promise(_ promise: Promise, doubleResolvedWithResult result: PendingPromise, resolutionStackTrace: [String], abusingStackTrace: [String]) } +private let abuseLog = Logger(category: "Abuse", subsystem: "com.ericrabil.pwomise") public var SharedPromiseAbuseDelegate: PromiseAbuseDelegate? typealias dispatch_get_current_queue_t = @convention(c) () -> Unmanaged @@ -100,6 +102,8 @@ public class Promise: CustomDebugStringConvertible { willSet { guard result == .pending, newValue != .pending else { /// Result can only be set once – its a promise of a result, not a publisher + let resolutionStackTrace = resolutionStackTrace, callStackSymbols = Thread.callStackSymbols + abuseLog.fault("Double-completion of \(String(describing: type(of: self))) \n- original stack trace: \n\(resolutionStackTrace.joined(separator: "\n"))\n- violating stack trace: \(callStackSymbols)") if let abuseDelegate = SharedPromiseAbuseDelegate { abuseDelegate.promise(self, doubleResolvedWithResult: newValue, resolutionStackTrace: resolutionStackTrace, abusingStackTrace: Thread.callStackSymbols) } else {