|
1 | 1 | #if canImport(os) |
2 | | - import os.signpost |
| 2 | +import os.signpost |
3 | 3 |
|
4 | | - extension Reducer { |
5 | | - /// Instruments the reducer with |
6 | | - /// [signposts](https://developer.apple.com/documentation/os/logging/recording_performance_data). |
7 | | - /// Each invocation of the reducer will be measured by an interval, and the lifecycle of its |
8 | | - /// effects will be measured with interval and event signposts. |
9 | | - /// |
10 | | - /// To use, build your app for Instruments (⌘I), create a blank instrument, and then use the "+" |
11 | | - /// icon at top right to add the signpost instrument. Start recording your app (red button at top |
12 | | - /// left) and then you should see timing information for every action sent to the store and every |
13 | | - /// effect executed. |
14 | | - /// |
15 | | - /// Effect instrumentation can be particularly useful for inspecting the lifecycle of long-living |
16 | | - /// effects. For example, if you start an effect (e.g. a location manager) in `onAppear` and |
17 | | - /// forget to tear down the effect in `onDisappear`, it will clearly show in Instruments that the |
18 | | - /// effect never completed. |
19 | | - /// |
20 | | - /// - Parameters: |
21 | | - /// - prefix: A string to print at the beginning of the formatted message for the signpost. |
22 | | - /// - log: An `OSLog` to use for signposts. |
23 | | - /// - Returns: A reducer that has been enhanced with instrumentation. |
24 | | - public func signpost( |
25 | | - _ prefix: String = "", |
26 | | - log: OSLog = OSLog( |
27 | | - subsystem: "co.pointfree.composable-architecture", |
28 | | - category: "Reducer Instrumentation" |
29 | | - ) |
30 | | - ) -> Self { |
31 | | - guard log.signpostsEnabled else { return self } |
| 4 | +extension Reducer { |
| 5 | + /// Instruments the reducer with |
| 6 | + /// [signposts](https://developer.apple.com/documentation/os/logging/recording_performance_data). |
| 7 | + /// Each invocation of the reducer will be measured by an interval, and the lifecycle of its |
| 8 | + /// effects will be measured with interval and event signposts. |
| 9 | + /// |
| 10 | + /// To use, build your app for Instruments (⌘I), create a blank instrument, and then use the "+" |
| 11 | + /// icon at top right to add the signpost instrument. Start recording your app (red button at top |
| 12 | + /// left) and then you should see timing information for every action sent to the store and every |
| 13 | + /// effect executed. |
| 14 | + /// |
| 15 | + /// Effect instrumentation can be particularly useful for inspecting the lifecycle of long-living |
| 16 | + /// effects. For example, if you start an effect (e.g. a location manager) in `onAppear` and |
| 17 | + /// forget to tear down the effect in `onDisappear`, it will clearly show in Instruments that the |
| 18 | + /// effect never completed. |
| 19 | + /// |
| 20 | + /// - Parameters: |
| 21 | + /// - prefix: A string to print at the beginning of the formatted message for the signpost. |
| 22 | + /// - log: An `OSLog` to use for signposts. |
| 23 | + /// - Returns: A reducer that has been enhanced with instrumentation. |
| 24 | + public func signpost( |
| 25 | + _ prefix: String = "", |
| 26 | + log: OSLog = OSLog( |
| 27 | + subsystem: "co.pointfree.composable-architecture", |
| 28 | + category: "Reducer Instrumentation" |
| 29 | + ) |
| 30 | + ) -> Self { |
| 31 | + guard log.signpostsEnabled else { return self } |
32 | 32 |
|
33 | | - // NB: Prevent rendering as "N/A" in Instruments |
34 | | - let zeroWidthSpace = "\u{200B}" |
| 33 | + // NB: Prevent rendering as "N/A" in Instruments |
| 34 | + let zeroWidthSpace = "\u{200B}" |
35 | 35 |
|
36 | | - let prefix = prefix.isEmpty ? zeroWidthSpace : "[\(prefix)] " |
| 36 | + let prefix = prefix.isEmpty ? zeroWidthSpace : "[\(prefix)] " |
37 | 37 |
|
38 | | - return Self { state, action, environment in |
39 | | - var actionOutput: String! |
40 | | - if log.signpostsEnabled { |
41 | | - actionOutput = debugCaseOutput(action) |
42 | | - os_signpost(.begin, log: log, name: "Action", "%s%s", prefix, actionOutput) |
43 | | - } |
44 | | - let effects = self.run(&state, action, environment) |
45 | | - if log.signpostsEnabled { |
46 | | - os_signpost(.end, log: log, name: "Action") |
47 | | - return |
48 | | - effects |
49 | | - .effectSignpost(prefix, log: log, actionOutput: actionOutput) |
50 | | - } |
51 | | - return effects |
| 38 | + return Self { state, action, environment in |
| 39 | + var actionOutput: String! |
| 40 | + if log.signpostsEnabled { |
| 41 | + actionOutput = debugCaseOutput(action) |
| 42 | + os_signpost(.begin, log: log, name: "Action", "%s%s", prefix, actionOutput) |
52 | 43 | } |
| 44 | + let effects = self.run(&state, action, environment) |
| 45 | + if log.signpostsEnabled { |
| 46 | + os_signpost(.end, log: log, name: "Action") |
| 47 | + return |
| 48 | + effects |
| 49 | + .effectSignpost(prefix, log: log, actionOutput: actionOutput) |
| 50 | + } |
| 51 | + return effects |
53 | 52 | } |
54 | 53 | } |
| 54 | +} |
55 | 55 |
|
56 | | - extension Effect where Failure == Never { |
57 | | - func effectSignpost( |
58 | | - _ prefix: String, |
59 | | - log: OSLog, |
60 | | - actionOutput: String |
61 | | - ) -> Self { |
62 | | - let sid = OSSignpostID(log: log) |
| 56 | +extension Effect where Failure == Never { |
| 57 | + func effectSignpost( |
| 58 | + _ prefix: String, |
| 59 | + log: OSLog, |
| 60 | + actionOutput: String |
| 61 | + ) -> Self { |
| 62 | + let sid = OSSignpostID(log: log) |
63 | 63 |
|
64 | | - switch self.operation { |
65 | | - case .none: |
66 | | - return self |
| 64 | + switch self.operation { |
| 65 | + case .none: |
| 66 | + return self |
67 | 67 | case let .producer(producer): |
68 | | - return .init( |
| 68 | + return .init( |
69 | 69 | operation: .producer( |
70 | 70 | producer |
71 | 71 | .on( |
72 | 72 | starting: { |
73 | | - os_signpost( |
74 | | - .begin, log: log, name: "Effect", signpostID: sid, "%sStarted from %s", prefix, |
| 73 | + os_signpost( |
| 74 | + .begin, log: log, name: "Effect", signpostID: sid, "%sStarted from %s", prefix, |
75 | 75 | actionOutput |
76 | 76 | ) |
77 | 77 | }, |
78 | 78 | completed: { |
79 | 79 | os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sFinished", prefix) |
80 | | - }, |
| 80 | + }, |
81 | 81 | disposed: { |
82 | | - os_signpost( |
| 82 | + os_signpost( |
83 | 83 | .end, log: log, name: "Effect", signpostID: sid, "%sCancelled", prefix) |
84 | | - }, |
| 84 | + }, |
85 | 85 | value: { value in |
86 | 86 | os_signpost( |
87 | 87 | .event, log: log, name: "Effect Output", "%sOutput from %s", prefix, |
88 | 88 | actionOutput |
89 | 89 | ) |
90 | | - } |
91 | | - ) |
| 90 | + } |
92 | 91 | ) |
93 | 92 | ) |
94 | | - case let .run(priority, operation): |
95 | | - return .init( |
96 | | - operation: .run(priority) { send in |
97 | | - os_signpost( |
98 | | - .begin, log: log, name: "Effect", signpostID: sid, "%sStarted from %s", prefix, |
99 | | - actionOutput |
100 | | - ) |
101 | | - await operation( |
102 | | - Send { output in |
103 | | - os_signpost( |
104 | | - .event, log: log, name: "Effect Output", "%sOutput from %s", prefix, actionOutput |
105 | | - ) |
106 | | - send(output) |
107 | | - } |
108 | | - ) |
109 | | - if Task.isCancelled { |
110 | | - os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sCancelled", prefix) |
| 93 | + ) |
| 94 | + case let .run(priority, operation): |
| 95 | + return .init( |
| 96 | + operation: .run(priority) { send in |
| 97 | + os_signpost( |
| 98 | + .begin, log: log, name: "Effect", signpostID: sid, "%sStarted from %s", prefix, |
| 99 | + actionOutput |
| 100 | + ) |
| 101 | + await operation( |
| 102 | + Send { action in |
| 103 | + os_signpost( |
| 104 | + .event, log: log, name: "Effect Output", "%sOutput from %s", prefix, actionOutput |
| 105 | + ) |
| 106 | + send(action) |
111 | 107 | } |
112 | | - os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sFinished", prefix) |
| 108 | + ) |
| 109 | + if Task.isCancelled { |
| 110 | + os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sCancelled", prefix) |
113 | 111 | } |
114 | | - ) |
115 | | - } |
| 112 | + os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sFinished", prefix) |
| 113 | + } |
| 114 | + ) |
116 | 115 | } |
117 | 116 | } |
| 117 | +} |
118 | 118 | #endif |
119 | 119 |
|
120 | 120 | func debugCaseOutput(_ value: Any) -> String { |
|
0 commit comments