Skip to content

Commit a6d03a5

Browse files
authored
Merge pull request #53 from cwakamo/PlaygroundLogger-override-maximum-depth-env-var
[PlaygroundLogger] Added support for an environment variable which co…
2 parents 88043d7 + 469b06e commit a6d03a5

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

PlaygroundLogger/PlaygroundLogger/LogPolicy.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2018-2021 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -35,14 +35,23 @@ struct LogPolicy {
3535

3636
/// Initializes a new `LogPolicy`.
3737
///
38-
/// - parameter maximumDepth: The maximum depth level for logging children of children. Defaults to 2.
38+
/// - parameter maximumDepth: The maximum depth level for logging children of children. Defaults to 2, but the default can be overridden with the `LOGGER_DEPTH` environment variable.
3939
/// - parameter aggregateChildPolicy: The policy to use for logging children of aggregates. Defaults to logging no more than the first 10,000 children.
4040
/// - parameter containerChildPolicy: The policy to use for logging children of collections. Defaults to logging no more than the first 80 children plus the last 20 children.
41-
init(maximumDepth: Int = 2,
41+
init(maximumDepth: Int = (LogPolicy.environmentMaxDepth ?? 2),
4242
aggregateChildPolicy: ChildPolicy = .head(count: 10_000),
4343
containerChildPolicy: ChildPolicy = .headTail(headCount: 80, tailCount: 20)) {
4444
self.maximumDepth = maximumDepth
4545
self.aggregateChildPolicy = aggregateChildPolicy
4646
self.containerChildPolicy = containerChildPolicy
4747
}
48+
49+
/// Read and return the `LOGGER_DEPTH` from the environment.
50+
static let loggerDepthEnvironmentKey = "LOGGER_DEPTH"
51+
private static var environmentMaxDepth: Int? {
52+
guard let envDepth = ProcessInfo.processInfo.environment[loggerDepthEnvironmentKey] else {
53+
return nil
54+
}
55+
return Int(envDepth)
56+
}
4857
}

PlaygroundLogger/PlaygroundLoggerTests/LogPolicyTests.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2018-2021 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See http://swift.org/LICENSE.txt for license information
@@ -39,6 +39,14 @@ fileprivate class TestSubsubclass: TestSubclass {
3939
}
4040

4141
class LogPolicyTests: XCTestCase {
42+
func testMaximumDepthEnvironmentOverride() {
43+
setenv("LOGGER_DEPTH", "4", 0)
44+
let testPolicy = LogPolicy()
45+
46+
XCTAssertEqual(testPolicy.maximumDepth, 4)
47+
unsetenv("LOGGER_DEPTH")
48+
}
49+
4250
func testMaximumDepthLimitZero() throws {
4351
let testPolicy = LogPolicy(maximumDepth: 0)
4452

0 commit comments

Comments
 (0)