-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWorkflowLogger.swift
213 lines (181 loc) · 7.09 KB
/
WorkflowLogger.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright 2020 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import os.signpost
private extension OSLog {
/// Logging will use this log handle when enabled
static let workflow = OSLog(subsystem: "com.squareup.Workflow", category: "Workflow")
/// The active log handle to use when logging. If `WorkflowLogging.osLoggingSupported` is
/// `true`, defaults to the `workflow` handle, otherwise defaults to the shared `.disabled`
/// handle.
static var active: OSLog = {
WorkflowLogging.isOSLoggingAllowed ? .workflow : .disabled
}()
}
// MARK: -
/// Namespace for specifying logging configuration data.
public enum WorkflowLogging {}
extension WorkflowLogging {
/// Flag indicating whether `OSLog` logs may be recorded. Note, actual emission of
/// log statements in specific cases may depend on additional configuration options, so
/// this being `true` does not necessarily imply logging will occur.
@_spi(Logging)
public static let isOSLoggingAllowed: Bool = {
let env = ProcessInfo.processInfo.environment
guard let value = env["com.squareup.workflow.allowOSLogging"] else {
return false
}
return (value as NSString).boolValue
}()
}
extension WorkflowLogging {
public struct Config {
/// Configuration options to control logging during a render pass.
public enum RenderLoggingMode {
/// No data will be recorded for WorkflowNode render timings.
case none
/// Render timings will only be recorded for root nodes in a Workflow tree.
case rootsOnly
/// Render timings will be recorded for all nodes in a Workflow tree.
/// N.B. performance may be noticeably impacted when using this option.
case allNodes
}
public var renderLoggingMode: RenderLoggingMode = .allNodes
/// When `true`, the interval spanning a WorkflowNode's lifetime will be recorded.
public var logLifetimes = true
/// When `true`, action events will be recorded.
public var logActions = true
}
/// Global setting to enable or disable logging.
/// Note, this is independent of the specified `config` value, and simply governs whether
/// the runtime should emit any logs.
///
/// To enable logging, at a minimum you must set:
/// `WorkflowLogging.enabled = true`
///
/// Additionally, ``isOSLoggingAllowed`` must also be configured to be `true`.
///
/// If you wish for more control over what the runtime will log, you may additionally specify
/// a custom value for `WorkflowLogging.config`.
public static var enabled: Bool {
get { OSLog.active === OSLog.workflow }
set {
guard isOSLoggingAllowed else { return }
OSLog.active = newValue ? .workflow : .disabled
}
}
/// Configuration options used to determine which activities are logged.
public static var config: Config = .rootRendersAndActions
}
extension WorkflowLogging.Config {
/// Logging config that will output the most information.
/// Will also have the most noticeable effect on performance.
public static let debug: Self = .init(renderLoggingMode: .allNodes, logLifetimes: true, logActions: true)
/// Logging config that will record render timings for root nodes as well as action events.
/// This provides a reasonable performance tradeoff if you're interested in the runtime's behavior
/// but don't wan to pay the price of logging everything.
public static let rootRendersAndActions: Self = .init(renderLoggingMode: .rootsOnly, logLifetimes: false, logActions: true)
}
// MARK: -
/// Simple class that can be used to create signpost IDs based on an object pointer.
final class SignpostRef {
init() {}
}
final class WorkflowLogger {
// MARK: Workflows
static func logWorkflowStarted<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
guard
WorkflowLogging.isOSLoggingAllowed,
WorkflowLogging.config.logLifetimes
else { return }
let signpostID = OSSignpostID(log: .active, object: ref)
os_signpost(
.begin,
log: .active,
name: "Alive",
signpostID: signpostID,
"Workflow: %{public}@",
String(describing: WorkflowType.self)
)
}
static func logWorkflowFinished<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
guard
WorkflowLogging.isOSLoggingAllowed,
WorkflowLogging.config.logLifetimes
else { return }
let signpostID = OSSignpostID(log: .active, object: ref)
os_signpost(.end, log: .active, name: "Alive", signpostID: signpostID)
}
static func logSinkEvent<Action: WorkflowAction>(ref: AnyObject, action: Action) {
guard
WorkflowLogging.isOSLoggingAllowed,
WorkflowLogging.config.logActions
else { return }
let signpostID = OSSignpostID(log: .active, object: ref)
os_signpost(
.event,
log: .active,
name: "Sink Event",
signpostID: signpostID,
"Event for workflow: %{public}@",
String(describing: Action.WorkflowType.self)
)
}
// MARK: Rendering
static func logWorkflowStartedRendering<WorkflowType>(
ref: WorkflowNode<WorkflowType>,
isRootNode: Bool
) {
guard shouldLogRenderTimings(
isRootNode: isRootNode
) else { return }
let signpostID = OSSignpostID(log: .active, object: ref)
os_signpost(
.begin,
log: .active,
name: "Render",
signpostID: signpostID,
"Render Workflow: %{public}@",
String(describing: WorkflowType.self)
)
}
static func logWorkflowFinishedRendering<WorkflowType>(
ref: WorkflowNode<WorkflowType>,
isRootNode: Bool
) {
guard shouldLogRenderTimings(
isRootNode: isRootNode
) else { return }
let signpostID = OSSignpostID(log: .active, object: ref)
os_signpost(.end, log: .active, name: "Render", signpostID: signpostID)
}
// MARK: - Utilities
private static func shouldLogRenderTimings(
isRootNode: Bool
) -> Bool {
guard WorkflowLogging.isOSLoggingAllowed else {
return false
}
switch WorkflowLogging.config.renderLoggingMode {
case .none:
return false
case .rootsOnly:
return isRootNode
case .allNodes:
return true
}
}
}