Skip to content

Commit

Permalink
fix logger
Browse files Browse the repository at this point in the history
  • Loading branch information
waahm7 committed Sep 9, 2024
1 parent 26be737 commit 60a8abe
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 38 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ awsCIoPlatformExcludes.append("source/s2n")
/// aws-c-checksums
//////////////////////////////////////////////////////////////////////
var awsCChecksumsExcludes = [
"bin",
"CMakeLists.txt",
"LICENSE",
"builder.json",
Expand Down
8 changes: 6 additions & 2 deletions Source/AwsCommonRuntimeKit/crt/CommonRuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ public struct CRTError: Equatable {
public let message: String
public let name: String

public init<T: BinaryInteger>(code: T) {
public init<T: BinaryInteger>(code: T, context: String? = nil) {
if code > INT32_MAX || code <= 0 {
self.code = Int32(AWS_ERROR_UNKNOWN.rawValue)
} else {
self.code = Int32(code)
}
self.message = String(cString: aws_error_str(self.code))
var message = String(cString: aws_error_str(self.code))
if let context {
message += ". " + context;

Check failure on line 24 in Source/AwsCommonRuntimeKit/crt/CommonRuntimeError.swift

View workflow job for this annotation

GitHub Actions / lint

Trailing Semicolon Violation: Lines should not have trailing semicolons (trailing_semicolon)
}
self.message = message
self.name = String(cString: aws_error_name(self.code))
}

Expand Down
62 changes: 38 additions & 24 deletions Source/AwsCommonRuntimeKit/crt/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,52 @@
// SPDX-License-Identifier: Apache-2.0.

import AwsCCommon
var logger: aws_logger?
import Foundation

public enum LogTarget {
case standardOutput
case standardError
case filePath(String)
}

public struct Logger {
public static func initialize(pipe: UnsafeMutablePointer<FILE>?, level: LogLevel) {
// Clean up the logger if it was previously initialized
if var logger = logger {
aws_logger_clean_up(&logger)
aws_logger_set(nil)
private static var logger: aws_logger? = nil

Check warning on line 14 in Source/AwsCommonRuntimeKit/crt/Logger.swift

View workflow job for this annotation

GitHub Actions / lint

Redundant Optional Initialization Violation: Initializing an optional variable with nil is redundant (redundant_optional_initialization)
private static let lock = NSLock()

/// Initializes the CRT logger based on the specified log target and log level. The CRT logger must be only initialized once in your application. Initializing the logger multiple times is not supported.
/// - Parameters:
/// - target: The logging target, which can be standard output, standard error, or a custom file path.
/// - level: The logging level, represented by the `LogLevel` enum.
/// - Throws: CommonRunTimeError.crtException
public static func initialize(target: LogTarget, level: LogLevel) throws {
lock.lock()
defer { lock.unlock() }

// Check if the logger is already initialized
guard logger == nil else {
throw CommonRunTimeError.crtError(CRTError(code: AWS_ERROR_UNSUPPORTED_OPERATION.rawValue, context: "Initializing the CRT Logger multiple times is not supported."))

Check failure on line 28 in Source/AwsCommonRuntimeKit/crt/Logger.swift

View workflow job for this annotation

GitHub Actions / lint

Line Length Violation: Line should be 160 characters or less; currently it has 176 characters (line_length)
}

// Initialize the logger
logger = aws_logger()
var options = aws_logger_standard_options()
options.level = level.rawValue
options.file = pipe
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
aws_logger_set(&logger!)
}

public static func initilize(filePath: String, level: LogLevel) {
// Clean up the logger if it was previously initialized
if var logger = logger {
aws_logger_clean_up(&logger)
aws_logger_set(nil)
}
logger = aws_logger()

filePath.withCString { cFilePath in
var options = aws_logger_standard_options()
options.level = level.rawValue
options.filename = cFilePath
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
aws_logger_set(&logger!)
// Set options based on the logging target
switch target {
case .standardOutput:
options.file = stdout
case .standardError:
options.file = stderr
case .filePath(let filePath):
filePath.withCString { cFilePath in
options.filename = cFilePath
}
}

// Initialize and set the logger
aws_logger_init_standard(&logger!, allocator.rawValue, &options)
aws_logger_set(&logger!)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Elasticurl/Elasticurl.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.elastic
// SPDX-License-Identifier: Apache-2.0.

import AwsCommonRuntimeKit
Expand Down Expand Up @@ -222,10 +222,10 @@ struct Elasticurl {
createOutputFile()
if let traceFile = context.traceFile {
print("enable logging with trace file")
Logger.initilize(filePath: traceFile, level: context.logLevel)
try! Logger.initialize(target: .filePath(traceFile), level: context.logLevel)

Check failure on line 225 in Source/Elasticurl/Elasticurl.swift

View workflow job for this annotation

GitHub Actions / lint

Force Try Violation: Force tries should be avoided (force_try)
} else {
print("enable logging with stdout")
Logger.initialize(pipe: stdout, level: context.logLevel)
try! Logger.initialize(target: .standardOutput, level: context.logLevel)

Check failure on line 228 in Source/Elasticurl/Elasticurl.swift

View workflow job for this annotation

GitHub Actions / lint

Force Try Violation: Force tries should be avoided (force_try)
}

await run()
Expand Down
2 changes: 1 addition & 1 deletion Test/AwsCommonRuntimeKitTests/XCBaseTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class XCBaseTestCase: XCTestCase {
// XCode currently lacks a way to enable logs exclusively for failed tests only.
// To prevent log spamming, we use `error` log level to only print error message.
// We should update this once a more efficient log processing method becomes available.
Logger.initialize(pipe: stdout, level: .error)
try! Logger.initialize(target: .standardOutput, level: .error)

// Override the allocator with tracing allocator
allocator = tracingAllocator.rawValue
Expand Down
2 changes: 1 addition & 1 deletion aws-common-runtime/aws-c-cal
2 changes: 1 addition & 1 deletion aws-common-runtime/s2n
Submodule s2n updated 430 files

0 comments on commit 60a8abe

Please sign in to comment.