Skip to content

Commit

Permalink
fix: set CRT log level without fatalError
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelkins committed Sep 18, 2024
1 parent 7b75b71 commit cda5ad6
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Sources/ClientRuntime/Networking/Http/CRT/SDKDefaultIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
//

import AwsCommonRuntimeKit
import class Foundation.ProcessInfo
#if os(Linux)
import Glibc
#else
import Darwin
#endif

public final class SDKDefaultIO: @unchecked Sendable {
public let eventLoopGroup: EventLoopGroup
Expand All @@ -25,13 +19,24 @@ public final class SDKDefaultIO: @unchecked Sendable {
/// The setter for changing log level of SDKDefaultIO logger.
/// If any log level other than the default log level of `.none` is desired,
/// this setter needs to be called as the first thing in the program.
public func setLogLevel(level: LogLevel) {
Self.setupLogger(level: level)
public static func setLogLevel(level: LogLevel) {
do {
try Logger.initialize(target: .standardOutput, level: level)
} catch {
failOnLogger()
}
}

private init() {
CommonRuntimeKit.initialize()
Self.setupLogger(level: .none)
do {
try Logger.initialize(target: .standardOutput, level: .none)
} catch CommonRunTimeError.crtError(let error)
where error.code == 6 && error.name == "AWS_ERROR_UNSUPPORTED_OPERATION" {
// logger was already initialized, no need to initialize it
} catch {
Self.failOnLogger()
}

do {
self.eventLoopGroup = try EventLoopGroup(threadCount: 0)
Expand Down Expand Up @@ -90,4 +95,11 @@ public final class SDKDefaultIO: @unchecked Sendable {
""")
}
}

private static func failOnLogger() -> Never {
fatalError("""
Logger failed to create. This should never happen. Please open a
Github issue with us at https://github.com/awslabs/aws-sdk-swift.
""")
}
}

0 comments on commit cda5ad6

Please sign in to comment.