Skip to content

Commit 432eb43

Browse files
committed
[fix]: short circuit worker logging & require explicit opt-in
1 parent 15d7d29 commit 432eb43

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

Workflow/Sources/WorkflowLogger.swift

+35-5
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,39 @@
1414
* limitations under the License.
1515
*/
1616

17+
import Foundation
1718
import os.signpost
1819

1920
private extension OSLog {
2021
/// Logging will use this log handle when enabled
2122
static let workflow = OSLog(subsystem: "com.squareup.Workflow", category: "Workflow")
2223

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+
}()
2530
}
2631

2732
// MARK: -
2833

2934
/// Namespace for specifying logging configuration data.
3035
public enum WorkflowLogging {}
3136

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+
public static let isOSLoggingAllowed: Bool = {
42+
let env = ProcessInfo.processInfo.environment
43+
guard let value = env["com.squareup.workflow.allowOSLogging"] else {
44+
return false
45+
}
46+
return (value as NSString).boolValue
47+
}()
48+
}
49+
3250
extension WorkflowLogging {
3351
public struct Config {
3452
/// Configuration options to control logging during a render pass.
@@ -93,7 +111,10 @@ final class WorkflowLogger {
93111
// MARK: Workflows
94112

95113
static func logWorkflowStarted<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
96-
guard WorkflowLogging.config.logLifetimes else { return }
114+
guard
115+
WorkflowLogging.isOSLoggingAllowed,
116+
WorkflowLogging.config.logLifetimes
117+
else { return }
97118

98119
let signpostID = OSSignpostID(log: .active, object: ref)
99120
os_signpost(
@@ -107,14 +128,20 @@ final class WorkflowLogger {
107128
}
108129

109130
static func logWorkflowFinished<WorkflowType>(ref: WorkflowNode<WorkflowType>) {
110-
guard WorkflowLogging.config.logLifetimes else { return }
131+
guard
132+
WorkflowLogging.isOSLoggingAllowed,
133+
WorkflowLogging.config.logLifetimes
134+
else { return }
111135

112136
let signpostID = OSSignpostID(log: .active, object: ref)
113137
os_signpost(.end, log: .active, name: "Alive", signpostID: signpostID)
114138
}
115139

116140
static func logSinkEvent<Action: WorkflowAction>(ref: AnyObject, action: Action) {
117-
guard WorkflowLogging.config.logActions else { return }
141+
guard
142+
WorkflowLogging.isOSLoggingAllowed,
143+
WorkflowLogging.config.logActions
144+
else { return }
118145

119146
let signpostID = OSSignpostID(log: .active, object: ref)
120147
os_signpost(
@@ -165,6 +192,9 @@ final class WorkflowLogger {
165192
private static func shouldLogRenderTimings(
166193
isRootNode: Bool
167194
) -> Bool {
195+
guard WorkflowLogging.isOSLoggingAllowed else {
196+
return false
197+
}
168198
switch WorkflowLogging.config.renderLoggingMode {
169199
case .none:
170200
return false

WorkflowCombine/Sources/Logger.swift

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import os.signpost
18+
import Workflow
1819

1920
private extension OSLog {
2021
static let worker = OSLog(subsystem: "com.squareup.WorkflowCombine", category: "Worker")
@@ -29,6 +30,8 @@ final class WorkerLogger<WorkerType: Worker> {
2930
// MARK: - Workers
3031

3132
func logStarted() {
33+
guard WorkflowLogging.isOSLoggingAllowed else { return }
34+
3235
os_signpost(
3336
.begin,
3437
log: .worker,
@@ -40,6 +43,8 @@ final class WorkerLogger<WorkerType: Worker> {
4043
}
4144

4245
func logFinished(status: StaticString) {
46+
guard WorkflowLogging.isOSLoggingAllowed else { return }
47+
4348
os_signpost(
4449
.end,
4550
log: .worker,
@@ -50,6 +55,8 @@ final class WorkerLogger<WorkerType: Worker> {
5055
}
5156

5257
func logOutput() {
58+
guard WorkflowLogging.isOSLoggingAllowed else { return }
59+
5360
os_signpost(
5461
.event,
5562
log: .worker,

WorkflowConcurrency/Sources/Logger.swift

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import os.signpost
18+
import Workflow
1819

1920
private extension OSLog {
2021
static let worker = OSLog(subsystem: "com.squareup.WorkflowConcurrency", category: "Worker")
@@ -29,6 +30,8 @@ final class WorkerLogger<WorkerType: Worker> {
2930
// MARK: - Workers
3031

3132
func logStarted() {
33+
guard WorkflowLogging.isOSLoggingAllowed else { return }
34+
3235
os_signpost(
3336
.begin,
3437
log: .worker,
@@ -40,6 +43,8 @@ final class WorkerLogger<WorkerType: Worker> {
4043
}
4144

4245
func logFinished(status: StaticString) {
46+
guard WorkflowLogging.isOSLoggingAllowed else { return }
47+
4348
os_signpost(
4449
.end,
4550
log: .worker,
@@ -50,6 +55,8 @@ final class WorkerLogger<WorkerType: Worker> {
5055
}
5156

5257
func logOutput() {
58+
guard WorkflowLogging.isOSLoggingAllowed else { return }
59+
5360
os_signpost(
5461
.event,
5562
log: .worker,

WorkflowReactiveSwift/Sources/Logger.swift

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@
1515
*/
1616

1717
import os.signpost
18+
import Workflow
1819

1920
// Namespace for Worker logging
2021
public enum WorkerLogging {}
2122

2223
extension WorkerLogging {
2324
public static var enabled: Bool {
2425
get { OSLog.active === OSLog.worker }
25-
set { OSLog.active = newValue ? .worker : .disabled }
26+
set {
27+
guard WorkflowLogging.isOSLoggingAllowed else { return }
28+
OSLog.active = newValue ? .worker : .disabled
29+
}
2630
}
2731
}
2832

2933
private extension OSLog {
3034
static let worker = OSLog(subsystem: "com.squareup.WorkflowReactiveSwift", category: "Worker")
3135

32-
static var active: OSLog = .disabled
36+
static var active: OSLog = {
37+
WorkflowLogging.isOSLoggingAllowed ? .worker : .disabled
38+
}()
3339
}
3440

3541
// MARK: -
@@ -43,6 +49,8 @@ final class WorkerLogger<WorkerType: Worker> {
4349
// MARK: - Workers
4450

4551
func logStarted() {
52+
guard WorkerLogging.enabled else { return }
53+
4654
os_signpost(
4755
.begin,
4856
log: .active,
@@ -54,10 +62,14 @@ final class WorkerLogger<WorkerType: Worker> {
5462
}
5563

5664
func logFinished(status: StaticString) {
65+
guard WorkerLogging.enabled else { return }
66+
5767
os_signpost(.end, log: .active, name: "Running", signpostID: signpostID, status)
5868
}
5969

6070
func logOutput() {
71+
guard WorkerLogging.enabled else { return }
72+
6173
os_signpost(
6274
.event,
6375
log: .active,

WorkflowRxSwift/Sources/Logger.swift

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,27 @@
1515
*/
1616

1717
import os.signpost
18+
import Workflow
1819

1920
// Namespace for Worker logging
2021
public enum WorkerLogging {}
2122

2223
extension WorkerLogging {
2324
public static var enabled: Bool {
2425
get { OSLog.active === OSLog.worker }
25-
set { OSLog.active = newValue ? .worker : .disabled }
26+
set {
27+
guard WorkflowLogging.isOSLoggingAllowed else { return }
28+
OSLog.active = newValue ? .worker : .disabled
29+
}
2630
}
2731
}
2832

2933
private extension OSLog {
3034
static let worker = OSLog(subsystem: "com.squareup.WorkflowRxSwift", category: "Worker")
3135

32-
static var active: OSLog = .disabled
36+
static var active: OSLog = {
37+
WorkflowLogging.isOSLoggingAllowed ? .worker : .disabled
38+
}()
3339
}
3440

3541
// MARK: -
@@ -43,6 +49,8 @@ final class WorkerLogger<WorkerType: Worker> {
4349
// MARK: - Workers
4450

4551
func logStarted() {
52+
guard WorkerLogging.enabled else { return }
53+
4654
os_signpost(
4755
.begin,
4856
log: .active,
@@ -54,10 +62,14 @@ final class WorkerLogger<WorkerType: Worker> {
5462
}
5563

5664
func logFinished(status: StaticString) {
65+
guard WorkerLogging.enabled else { return }
66+
5767
os_signpost(.end, log: .active, name: "Running", signpostID: signpostID, status)
5868
}
5969

6070
func logOutput() {
71+
guard WorkerLogging.enabled else { return }
72+
6173
os_signpost(
6274
.event,
6375
log: .active,

0 commit comments

Comments
 (0)