Skip to content

Commit 3ce3479

Browse files
committed
[feat]: add swift-log dep & some more logging
1 parent 0061902 commit 3ce3479

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

Package.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ let package = Package(
6262
.package(url: "https://github.com/pointfreeco/swift-perception", "1.5.0" ..< "3.0.0"),
6363
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
6464
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"),
65+
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
6566
],
6667
targets: [
6768
// MARK: Workflow
6869

6970
.target(
7071
name: "Workflow",
71-
dependencies: ["ReactiveSwift"],
72+
dependencies: [
73+
.product(name: "Logging", package: "swift-log"),
74+
"ReactiveSwift",
75+
],
7276
path: "Workflow/Sources"
7377
),
7478
.target(

Samples/Tuist/Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Workflow/Sources/RenderContext.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public class RenderContext<WorkflowType: Workflow>: RenderContextType {
153153
}
154154

155155
private func assertStillValid() {
156-
assert(isValid, "A `RenderContext` instance was used outside of the workflow's `render` method. It is a programmer error to capture a context in a closure or otherwise cause it to be used outside of the `render` method.")
156+
if !isValid {
157+
WorkflowLogger.logExternal(as: .error, "A `RenderContext` instance was used outside of the workflow's `render` method. It is a programmer error to capture a context in a closure or otherwise cause it to be used outside of the `render` method.")
158+
assertionFailure("A `RenderContext` instance was used outside of the workflow's `render` method. It is a programmer error to capture a context in a closure or otherwise cause it to be used outside of the `render` method.")
159+
}
157160
}
158161
}
159162
}

Workflow/Sources/SubtreeManager.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -409,27 +409,25 @@ extension WorkflowNode.SubtreeManager {
409409
case .valid(let handler):
410410
handler(event)
411411

412-
case .invalid:
413-
#if DEBUG
412+
#if DEBUG
413+
case .invalid where isReentrantCall:
414414
// Reentrancy seems to often be due to UIKit behaviors over
415415
// which we have little control (e.g. synchronous resignation
416416
// of first responder after a new Rendering is assigned). Emit
417417
// some debug info in these cases.
418-
if isReentrantCall {
419-
print("[\(WorkflowType.self)]: ℹ️ Sink sent another action after it was invalidated but before its original action handling was resolved. This new action will be ignored. If this is unexpected, set a Swift error breakpoint on `\(InvalidSinkSentAction.self)` to debug.")
420-
}
418+
WorkflowLogger.logExternal(as: .info, "[\(WorkflowType.self)]: ℹ️ Sink sent another action after it was invalidated but before its original action handling was resolved. This new action will be ignored. If this is unexpected, set a Swift error breakpoint on `\(InvalidSinkSentAction.self)` to debug.")
419+
do { throw InvalidSinkSentAction() } catch {}
420+
#endif
421421

422-
do {
423-
throw InvalidSinkSentAction()
424-
} catch {}
425-
#endif
426-
427-
// If we're invalid and this is the first time `handle()` has
428-
// been called, then it's likely we've somehow been inadvertently
429-
// retained from the 'outside world'. Fail more loudly in this case.
430-
assert(isReentrantCall, """
431-
[\(WorkflowType.self)]: Sink sent an action after it was invalidated. This action will be ignored.
432-
""")
422+
case .invalid where !isReentrantCall:
423+
// If we've already been invalidated when `handle()` is initially
424+
// called, then it's likely we've somehow been inadvertently retained
425+
// from the 'outside world'. Fail more loudly in this case.
426+
WorkflowLogger.logExternal(as: .warning, "[\(WorkflowType.self)]: Sink sent an action after it was invalidated. This action will be ignored.")
427+
assertionFailure("[\(WorkflowType.self)]: Sink sent an action after it was invalidated. This action will be ignored.")
428+
429+
case .invalid:
430+
break
433431
}
434432
}
435433

Workflow/Sources/WorkflowLogger.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import Foundation
18+
import Logging
1819
import os.signpost
1920

2021
extension OSLog {
@@ -207,3 +208,33 @@ enum WorkflowLogger {
207208
}
208209
}
209210
}
211+
212+
// MARK: - External Logging
213+
214+
typealias ExternalLogger = Logging.Logger
215+
216+
extension WorkflowLogger {
217+
static func logExternal(
218+
as level: ExternalLogger.Level,
219+
_ message: @autoclosure () -> ExternalLogger.Message,
220+
metadata: @autoclosure () -> ExternalLogger.Metadata? = nil,
221+
source: @autoclosure () -> String? = nil,
222+
file: String = #fileID,
223+
function: String = #function,
224+
line: UInt = #line
225+
) {
226+
ExternalLogger.workflow.log(
227+
level: level,
228+
message(),
229+
metadata: metadata(),
230+
source: source(),
231+
file: file,
232+
function: function,
233+
line: line
234+
)
235+
}
236+
}
237+
238+
extension ExternalLogger {
239+
static let workflow = Logger(label: "com.squareup.workflow")
240+
}

0 commit comments

Comments
 (0)