Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions AmplifyFoundation/Credentials/AWSCredentials.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation

public protocol FoundationAWSCredentials {
var accessKeyId: String { get }
var secretAccessKey: String { get }
}

public protocol FoundationAWSTemporaryCredentials: FoundationAWSCredentials {
var sessionToken: String { get }
var expiration: Date { get }
}

public protocol FoundationAWSCredentialsProvider {
func resolve() async throws -> FoundationAWSCredentials
}
26 changes: 26 additions & 0 deletions AmplifyFoundation/Exception/AmplifyException.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation

public typealias FoundationErrorDescription = String
public typealias FoundationRecoverySuggestion = String

public struct FoundationAmplifyException: Error {
var errorDescription: FoundationErrorDescription
var recoverySuggestion: FoundationRecoverySuggestion
var underlyingError: Error?

init(
errorDescription: FoundationErrorDescription,
recoverySuggestion: FoundationRecoverySuggestion,
error: Error
) {
self.errorDescription = errorDescription
self.recoverySuggestion = recoverySuggestion
self.underlyingError = error
}
}
114 changes: 114 additions & 0 deletions AmplifyFoundation/Logging/AmplifyLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import os
import Foundation

public final class FoundationAmplifyLogger: FoundationLogger {
static let lock: NSLocking = NSLock()
static var _logLevel = FoundationLogLevel.error

private let osLog: OSLog
private let subsystem: String

public init(namespace: String) {
self.subsystem = Bundle.main.bundleIdentifier ?? "com.amazonaws.amplify.logger"
self.namespace = namespace
self.osLog = OSLog(subsystem: subsystem, category: namespace)
}

public var namespace: String

public var logLevel: FoundationLogLevel {
get {
FoundationAmplifyLogger.lock.lock()
defer {
FoundationAmplifyLogger.lock.unlock()
}

return FoundationAmplifyLogger._logLevel
}
set {
FoundationAmplifyLogger.lock.lock()
defer {
FoundationAmplifyLogger.lock.unlock()
}

FoundationAmplifyLogger._logLevel = newValue
}
}

public func error(_ message: @autoclosure () -> String) {
guard logLevel.rawValue >= FoundationLogLevel.error.rawValue else { return }
os_log(
"%@",
log: osLog,
type: OSLogType.error,
message()
)
}

public func error(_ error: @autoclosure () -> Error) {
guard logLevel.rawValue >= FoundationLogLevel.error.rawValue else { return }
os_log(
"%@",
log: osLog,
type: OSLogType.error,
error().localizedDescription
)
}

public func warn(_ message: @autoclosure () -> String) {
guard logLevel.rawValue >= FoundationLogLevel.warn.rawValue else { return }

os_log(
"%@",
log: osLog,
type: OSLogType.info,
message()
)
}

public func info(_ message: @autoclosure () -> String) {
guard logLevel.rawValue >= FoundationLogLevel.info.rawValue else { return }

os_log(
"%@",
log: osLog,
type: OSLogType.info,
message()
)
}

public func debug(_ message: @autoclosure () -> String) {
guard logLevel.rawValue >= FoundationLogLevel.debug.rawValue else { return }

os_log(
"%@",
log: osLog,
type: OSLogType.debug,
message()
)
}

public func verbose(_ message: @autoclosure () -> String) {
guard logLevel.rawValue >= FoundationLogLevel.verbose.rawValue else { return }

os_log(
"%@",
log: osLog,
type: OSLogType.debug,
message()
)
}
}

public final class FoundationAmplifyLoggerProvider: FoundationLoggerProvider {
public func resolve(forNamespace namespace: String) -> any FoundationLogger {
FoundationAmplifyLogger(namespace: namespace)
}
}
48 changes: 48 additions & 0 deletions AmplifyFoundation/Logging/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

public protocol FoundationLogger {
/// namespace
var namespace: String {get set}

/// The log level of the logger.
var logLevel: FoundationLogLevel { get set }

/// Logs a message at `error` level
func error(_ message: @autoclosure () -> String)

/// Logs the error at `error` level
func error(_ error: @autoclosure () -> Error)

/// Logs a message at `warn` level
func warn(_ message: @autoclosure () -> String)

/// Logs a message at `info` level
func info(_ message: @autoclosure () -> String)

/// Logs a message at `debug` level
func debug(_ message: @autoclosure () -> String)

/// Logs a message at `verbose` level
func verbose(_ message: @autoclosure () -> String)
}

public protocol FoundationLoggerProvider {
func resolve(forNamespace namespace: String) -> FoundationLogger
}

/// An enumeration of the different levels of logging.
/// The levels are progressive, with lower-value items being lower priority
/// than higher-value items. For example, `info` is lower priority than `warn`
/// or `error`.
public enum FoundationLogLevel: Int {
case error
case warn
case info
case debug
case verbose
}
10 changes: 9 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,13 @@ let loggingTargets: [Target] = [
)
]

let targets: [Target] = amplifyTargets
let foundationTarget: Target = .target(
name: "AmplifyFoundation",
dependencies: [],
path: "AmplifyFoundation"
)

var targets: [Target] = amplifyTargets
+ apiTargets
+ authTargets
+ dataStoreTargets
Expand All @@ -482,6 +488,8 @@ let targets: [Target] = amplifyTargets
+ predictionsTargets
+ loggingTargets

targets.append(foundationTarget)

let package = Package(
name: "Amplify",
platforms: platforms,
Expand Down
Loading