14
14
* limitations under the License.
15
15
*/
16
16
17
+ import Foundation
17
18
import os. signpost
18
19
19
20
private extension OSLog {
20
21
/// Logging will use this log handle when enabled
21
22
static let workflow = OSLog ( subsystem: " com.squareup.Workflow " , category: " Workflow " )
22
23
23
- /// The active log handle to use when logging. Defaults to the shared `.disabled` handle.
24
- static var active : OSLog = . disabled
24
+ /// The active log handle to use when logging. If `WorkflowLogging.osLoggingSupported` is
25
+ /// `true`, defaults to the `workflow` handle, otherwise defaults to the shared `.disabled`
26
+ /// handle.
27
+ static var active : OSLog = {
28
+ WorkflowLogging . isOSLoggingAllowed ? . workflow : . disabled
29
+ } ( )
25
30
}
26
31
27
32
// MARK: -
28
33
29
34
/// Namespace for specifying logging configuration data.
30
35
public enum WorkflowLogging { }
31
36
37
+ extension WorkflowLogging {
38
+ /// Flag indicating whether `OSLog` logs may be recorded. Note, actual emission of
39
+ /// log statements in specific cases may depend on additional configuration options, so
40
+ /// this being `true` does not necessarily imply logging will occur.
41
+ @_spi ( Logging)
42
+ public static let isOSLoggingAllowed : Bool = {
43
+ let env = ProcessInfo . processInfo. environment
44
+ guard let value = env [ " com.squareup.workflow.allowOSLogging " ] else {
45
+ return false
46
+ }
47
+ return ( value as NSString ) . boolValue
48
+ } ( )
49
+ }
50
+
32
51
extension WorkflowLogging {
33
52
public struct Config {
34
53
/// Configuration options to control logging during a render pass.
@@ -60,11 +79,16 @@ extension WorkflowLogging {
60
79
/// To enable logging, at a minimum you must set:
61
80
/// `WorkflowLogging.enabled = true`
62
81
///
82
+ /// Additionally, ``isOSLoggingAllowed`` must also be configured to be `true`.
83
+ ///
63
84
/// If you wish for more control over what the runtime will log, you may additionally specify
64
85
/// a custom value for `WorkflowLogging.config`.
65
86
public static var enabled : Bool {
66
87
get { OSLog . active === OSLog . workflow }
67
- set { OSLog . active = newValue ? . workflow : . disabled }
88
+ set {
89
+ guard isOSLoggingAllowed else { return }
90
+ OSLog . active = newValue ? . workflow : . disabled
91
+ }
68
92
}
69
93
70
94
/// Configuration options used to determine which activities are logged.
@@ -93,7 +117,10 @@ final class WorkflowLogger {
93
117
// MARK: Workflows
94
118
95
119
static func logWorkflowStarted< WorkflowType> ( ref: WorkflowNode < WorkflowType > ) {
96
- guard WorkflowLogging . config. logLifetimes else { return }
120
+ guard
121
+ WorkflowLogging . isOSLoggingAllowed,
122
+ WorkflowLogging . config. logLifetimes
123
+ else { return }
97
124
98
125
let signpostID = OSSignpostID ( log: . active, object: ref)
99
126
os_signpost (
@@ -107,14 +134,20 @@ final class WorkflowLogger {
107
134
}
108
135
109
136
static func logWorkflowFinished< WorkflowType> ( ref: WorkflowNode < WorkflowType > ) {
110
- guard WorkflowLogging . config. logLifetimes else { return }
137
+ guard
138
+ WorkflowLogging . isOSLoggingAllowed,
139
+ WorkflowLogging . config. logLifetimes
140
+ else { return }
111
141
112
142
let signpostID = OSSignpostID ( log: . active, object: ref)
113
143
os_signpost ( . end, log: . active, name: " Alive " , signpostID: signpostID)
114
144
}
115
145
116
146
static func logSinkEvent< Action: WorkflowAction > ( ref: AnyObject , action: Action ) {
117
- guard WorkflowLogging . config. logActions else { return }
147
+ guard
148
+ WorkflowLogging . isOSLoggingAllowed,
149
+ WorkflowLogging . config. logActions
150
+ else { return }
118
151
119
152
let signpostID = OSSignpostID ( log: . active, object: ref)
120
153
os_signpost (
@@ -165,6 +198,9 @@ final class WorkflowLogger {
165
198
private static func shouldLogRenderTimings(
166
199
isRootNode: Bool
167
200
) -> Bool {
201
+ guard WorkflowLogging . isOSLoggingAllowed else {
202
+ return false
203
+ }
168
204
switch WorkflowLogging . config. renderLoggingMode {
169
205
case . none:
170
206
return false
0 commit comments