Consolidate all the os_signpost calls
MODIFY
swift/Workflow/Sources/OSLog+Workflow.swift
swift/Workflow/Sources/OSLog+Workflow.swift
import os.signpost
-extension OSLog {
+fileprivate extension OSLog {
static let workflow = OSLog(subsystem: "com.squareup.Workflow", category: "Workflow")
static let worker = OSLog(subsystem: "com.squareup.Workflow", category: "Worker")
}
+
+// MARK: -
+
+final class WorkflowLogger {
+ // MARK: Workflows
+
+ static func logWorkflowStarted<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .workflow, object: ref)
+ os_signpost(.begin, log: .workflow, name: "Alive", signpostID: signpostID,
+ "Workflow: %{public}@", String(describing: WorkflowType.self))
+ }
+ }
+
+ static func logWorkflowFinished<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .workflow, object: ref)
+ os_signpost(.end, log: .workflow, name: "Alive", signpostID: signpostID)
+ }
+ }
+
+ static func logSinkEvent<Action>(ref: AnyObject, action: Action) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .workflow, object: ref)
+ os_signpost(.event, log: .workflow, name: "Sink Event", signpostID: signpostID,
+ "Event: %@", String(describing: action))
+ }
+ }
+
+ // MARK: Rendering
+
+ static func logWorkflowStartedRendering<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .workflow, object: ref)
+ os_signpost(.begin, log: .workflow, name: "Render", signpostID: signpostID,
+ "Render Workflow: %{public}@", String(describing: WorkflowType.self))
+ }
+ }
+
+ static func logWorkflowFinishedRendering<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .workflow, object: ref)
+ os_signpost(.end, log: .workflow, name: "Render", signpostID: signpostID)
+ }
+ }
+
+ // MARK: - Workers
+
+ static func logWorkerStartedRunning<WorkerType>(ref: AnyObject, workerType: WorkerType.Type) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .worker, object: ref)
+ os_signpost(.begin, log: .worker, name: "Running", signpostID: signpostID,
+ "Worker: %{public}@", String(describing: WorkerType.self))
+ }
+ }
+
+ static func logWorkerFinishedRunning(ref: AnyObject, status: StaticString? = nil) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .worker, object: ref)
+ if let status = status {
+ os_signpost(.end, log: .worker, name: "Running", signpostID: signpostID, status)
+ } else {
+ os_signpost(.end, log: .worker, name: "Running", signpostID: signpostID)
+ }
+ }
+ }
+
+ static func logWorkerOutput<Output>(ref: AnyObject, output: Output) {
+ if #available(iOS 12.0, macOS 10.14, *) {
+ let signpostID = OSSignpostID(log: .worker, object: ref)
+ os_signpost(.event, log: .worker, name: "Worker Event", signpostID: signpostID,
+ "Event: %@", String(describing: output))
+ }
+ }
+}

MODIFY
swift/Workflow/Sources/SubtreeManager.swift
swift/Workflow/Sources/SubtreeManager.swift
import Dispatch
import ReactiveSwift
-import os.signpost
extension WorkflowNode {
/// Manages the subtree of a workflow. Specifically, this type encapsulates the logic required to update and manage
@@ -208,10 +207,7 @@ extension WorkflowNode.SubtreeManager {
let signpostRef = NSObject()
let sink = Sink<Action> { action in
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .workflow, object: signpostRef)
- os_signpost(.event, log: .workflow, name: "Sink Event", signpostID: signpostID, "Event: %@", String(describing: action))
- }
+ WorkflowLogger.logSinkEvent(ref: signpostRef, action: action)
reusableSink.handle(action: action)
}
@@ -416,11 +412,7 @@ extension WorkflowNode.SubtreeManager {
super.init(eventPipe: eventPipe)
let signpostRef = NSObject()
-
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .worker, object: signpostRef)
- os_signpost(.begin, log: .worker, name: "Running", signpostID: signpostID, "Worker: %{public}@", String(describing: W.self))
- }
+ WorkflowLogger.logWorkerStartedRunning(ref: signpostRef, workerType: W.self)
signalProducer
.take(during: lifetime)
@@ -428,27 +420,15 @@ extension WorkflowNode.SubtreeManager {
.start { [weak self] event in
switch event {
case .value(let output):
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .worker, object: signpostRef)
- os_signpost(.event, log: .worker, name: "Worker Event", signpostID: signpostID, "Event: %@", String(describing: output))
- }
+ WorkflowLogger.logWorkerOutput(ref: signpostRef, output: output)
self?.handle(output: output)
case .completed:
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .worker, object: signpostRef)
- os_signpost(.end, log: .worker, name: "Running", signpostID: signpostID)
- }
+ WorkflowLogger.logWorkerFinishedRunning(ref: signpostRef)
case .interrupted:
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .worker, object: signpostRef)
- os_signpost(.end, log: .worker, name: "Running", signpostID: signpostID, "Interrupted")
- }
+ WorkflowLogger.logWorkerFinishedRunning(ref: signpostRef, status: "Interrupted")
case .failed:
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .worker, object: signpostRef)
- os_signpost(.end, log: .worker, name: "Running", signpostID: signpostID, "Failed")
- }
+ WorkflowLogger.logWorkerFinishedRunning(ref: signpostRef, status: "Failed")
}
}
}

MODIFY
swift/Workflow/Sources/WorkflowNode.swift
swift/Workflow/Sources/WorkflowNode.swift
*/
import ReactiveSwift
-import os.signpost
/// Manages a running workflow.
final class WorkflowNode<WorkflowType: Workflow> {
@@ -35,10 +34,7 @@ final class WorkflowNode<WorkflowType: Workflow> {
self.workflow = workflow
self.state = workflow.makeInitialState()
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .workflow, object: self)
- os_signpost(.begin, log: .workflow, name: "Alive", signpostID: signpostID, "Workflow: %{public}@", String(describing: WorkflowType.self))
- }
+ WorkflowLogger.logWorkflowStarted(ref: self)
subtreeManager.onUpdate = { [weak self] output in
self?.handle(subtreeOutput: output)
@@ -46,10 +42,7 @@ final class WorkflowNode<WorkflowType: Workflow> {
}
deinit {
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .workflow, object: self)
- os_signpost(.end, log: .workflow, name: "Alive", signpostID: signpostID)
- }
+ WorkflowLogger.logWorkflowFinished(ref: self)
}
/// Handles an event produced by the subtree manager
@@ -84,16 +77,10 @@ final class WorkflowNode<WorkflowType: Workflow> {
}
func render() -> WorkflowType.Rendering {
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .workflow, object: self)
- os_signpost(.begin, log: .workflow, name: "Render", signpostID: signpostID, "Render Workflow: %{public}@", String(describing: WorkflowType.self))
- }
+ WorkflowLogger.logWorkflowStartedRendering(ref: self)
defer {
- if #available(iOS 12.0, *) {
- let signpostID = OSSignpostID(log: .workflow, object: self)
- os_signpost(.end, log: .workflow, name: "Render", signpostID: signpostID)
- }
+ WorkflowLogger.logWorkflowFinishedRendering(ref: self)
}
return subtreeManager.render { context in

Consolidate all the os_signpost calls