Skip to content

[PlaygroundLogger] Added support for an environment variable which co… #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions PlaygroundLogger/PlaygroundLogger/LogPolicy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2018-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -35,14 +35,23 @@ struct LogPolicy {

/// Initializes a new `LogPolicy`.
///
/// - parameter maximumDepth: The maximum depth level for logging children of children. Defaults to 2.
/// - 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.
/// - parameter aggregateChildPolicy: The policy to use for logging children of aggregates. Defaults to logging no more than the first 10,000 children.
/// - 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.
init(maximumDepth: Int = 2,
init(maximumDepth: Int = (LogPolicy.environmentMaxDepth ?? 2),
aggregateChildPolicy: ChildPolicy = .head(count: 10_000),
containerChildPolicy: ChildPolicy = .headTail(headCount: 80, tailCount: 20)) {
self.maximumDepth = maximumDepth
self.aggregateChildPolicy = aggregateChildPolicy
self.containerChildPolicy = containerChildPolicy
}

/// Read and return the `LOGGER_DEPTH` from the environment.
static let loggerDepthEnvironmentKey = "LOGGER_DEPTH"
private static var environmentMaxDepth: Int? {
guard let envDepth = ProcessInfo.processInfo.environment[loggerDepthEnvironmentKey] else {
return nil
}
return Int(envDepth)
}
}
10 changes: 9 additions & 1 deletion PlaygroundLogger/PlaygroundLoggerTests/LogPolicyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2018-2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -39,6 +39,14 @@ fileprivate class TestSubsubclass: TestSubclass {
}

class LogPolicyTests: XCTestCase {
func testMaximumDepthEnvironmentOverride() {
setenv("LOGGER_DEPTH", "4", 0)
let testPolicy = LogPolicy()

XCTAssertEqual(testPolicy.maximumDepth, 4)
unsetenv("LOGGER_DEPTH")
}

func testMaximumDepthLimitZero() throws {
let testPolicy = LogPolicy(maximumDepth: 0)

Expand Down