Skip to content

Commit

Permalink
Merge branch 'os_signpost-static-funcs' into os_signpost
Browse files Browse the repository at this point in the history
  • Loading branch information
AquaGeek committed May 6, 2020
2 parents 4752fd5 + d39bae5 commit 8a73d32
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 44 deletions.
77 changes: 76 additions & 1 deletion swift/Workflow/Sources/OSLog+Workflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,82 @@

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))
}
}
}
32 changes: 6 additions & 26 deletions swift/Workflow/Sources/SubtreeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
import Dispatch
import ReactiveSwift
import os.signpost

extension WorkflowNode {

Expand Down Expand Up @@ -217,10 +216,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)
}
Expand Down Expand Up @@ -440,39 +436,23 @@ 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)
.observe(on: QueueScheduler.workflowExecution)
.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")
}
}
}
Expand Down
21 changes: 4 additions & 17 deletions swift/Workflow/Sources/WorkflowNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
import ReactiveSwift
import os.signpost

/// Manages a running workflow.
final class WorkflowNode<WorkflowType: Workflow> {
Expand All @@ -35,21 +34,15 @@ 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)
}
}

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
Expand Down Expand Up @@ -81,16 +74,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
Expand Down

0 comments on commit 8a73d32

Please sign in to comment.