|
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 | | - @available(iOS 12.0, *) |
25 | | - public func signpost( |
26 | | - _ prefix: String = "", |
27 | | - log: OSLog = OSLog( |
28 | | - subsystem: "co.pointfree.composable-architecture", |
29 | | - category: "Reducer Instrumentation" |
30 | | - ) |
31 | | - ) -> Self { |
32 | | - 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 | + @available(iOS 12.0, *) |
| 25 | + public func signpost( |
| 26 | + _ prefix: String = "", |
| 27 | + log: OSLog = OSLog( |
| 28 | + subsystem: "co.pointfree.composable-architecture", |
| 29 | + category: "Reducer Instrumentation" |
| 30 | + ) |
| 31 | + ) -> Self { |
| 32 | + guard log.signpostsEnabled else { return self } |
33 | 33 |
|
34 | | - // NB: Prevent rendering as "N/A" in Instruments |
35 | | - let zeroWidthSpace = "\u{200B}" |
| 34 | + // NB: Prevent rendering as "N/A" in Instruments |
| 35 | + let zeroWidthSpace = "\u{200B}" |
36 | 36 |
|
37 | | - let prefix = prefix.isEmpty ? zeroWidthSpace : "[\(prefix)] " |
| 37 | + let prefix = prefix.isEmpty ? zeroWidthSpace : "[\(prefix)] " |
38 | 38 |
|
39 | | - return Self { state, action, environment in |
40 | | - var actionOutput: String! |
41 | | - if log.signpostsEnabled { |
42 | | - actionOutput = debugCaseOutput(action) |
43 | | - os_signpost(.begin, log: log, name: "Action", "%s%s", prefix, actionOutput) |
| 39 | + return Self { state, action, environment in |
| 40 | + var actionOutput: String! |
| 41 | + if log.signpostsEnabled { |
| 42 | + actionOutput = debugCaseOutput(action) |
| 43 | + os_signpost(.begin, log: log, name: "Action", "%s%s", prefix, actionOutput) |
| 44 | + } |
| 45 | + let effects = self.run(&state, action, environment) |
| 46 | + if log.signpostsEnabled { |
| 47 | + os_signpost(.end, log: log, name: "Action") |
| 48 | + return |
| 49 | + effects |
| 50 | + .effectSignpost(prefix, log: log, actionOutput: actionOutput) |
| 51 | + } |
| 52 | + return effects |
44 | 53 | } |
45 | | - let effects = self.run(&state, action, environment) |
46 | | - if log.signpostsEnabled { |
47 | | - os_signpost(.end, log: log, name: "Action") |
48 | | - return |
49 | | - effects |
50 | | - .effectSignpost(prefix, log: log, actionOutput: actionOutput) |
51 | | - } |
52 | | - return effects |
53 | 54 | } |
54 | 55 | } |
55 | | -} |
56 | 56 |
|
57 | | -extension Effect where Error == Never { |
58 | | - @available(iOS 12.0, *) |
59 | | - func effectSignpost( |
60 | | - _ prefix: String, |
61 | | - log: OSLog, |
62 | | - actionOutput: String |
63 | | - ) -> Effect<Value, Error> { |
64 | | - let sid = OSSignpostID(log: log) |
| 57 | + extension Effect where Error == Never { |
| 58 | + @available(iOS 12.0, *) |
| 59 | + func effectSignpost( |
| 60 | + _ prefix: String, |
| 61 | + log: OSLog, |
| 62 | + actionOutput: String |
| 63 | + ) -> Effect<Value, Error> { |
| 64 | + let sid = OSSignpostID(log: log) |
65 | 65 |
|
66 | | - return self.on( |
67 | | - starting: { |
68 | | - os_signpost( |
69 | | - .begin, log: log, name: "Effect", signpostID: sid, "%sStarted from %s", prefix, |
70 | | - actionOutput |
71 | | - ) |
72 | | - }, |
73 | | - completed: { |
74 | | - os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sFinished", prefix) |
75 | | - }, |
76 | | - disposed: { |
77 | | - os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sCancelled", prefix) |
78 | | - }, |
79 | | - value: { value in |
80 | | - os_signpost( |
81 | | - .event, log: log, name: "Effect Output", "%sOutput from %s", prefix, actionOutput |
82 | | - ) |
83 | | - }) |
| 66 | + return self.on( |
| 67 | + starting: { |
| 68 | + os_signpost( |
| 69 | + .begin, log: log, name: "Effect", signpostID: sid, "%sStarted from %s", prefix, |
| 70 | + actionOutput |
| 71 | + ) |
| 72 | + }, |
| 73 | + completed: { |
| 74 | + os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sFinished", prefix) |
| 75 | + }, |
| 76 | + disposed: { |
| 77 | + os_signpost(.end, log: log, name: "Effect", signpostID: sid, "%sCancelled", prefix) |
| 78 | + }, |
| 79 | + value: { value in |
| 80 | + os_signpost( |
| 81 | + .event, log: log, name: "Effect Output", "%sOutput from %s", prefix, actionOutput |
| 82 | + ) |
| 83 | + }) |
| 84 | + } |
84 | 85 | } |
85 | | -} |
86 | 86 | #endif |
87 | 87 |
|
88 | 88 | func debugCaseOutput(_ value: Any) -> String { |
|
0 commit comments