Skip to content
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

Implement Logging #17

Merged
merged 6 commits into from
Jan 21, 2024
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
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ excluded:
- Package.swift
- Package@swift-5.7.swift
- Package@swift-5.8.swift
- Sources/Flare/Classes/Generated/Strings.swift
- .build

# Rules
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

## Added
- Implement Logging Functionality
- Added in Pull Request [#17](https://github.com/space-code/flare/pull/17).

- Implement Support for Promotional Offers
- Added in Pull Request [#16](https://github.com/space-code/flare/pull/16).

Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ lint:
fmt:
mint run swiftformat Sources Tests

strings:
swiftgen

generate:
xcodegen generate

setup_build_tools:
sh scripts/setup_build_tools.sh

.PHONY: all bootstrap hook mint lint fmt generate setup_build_tools
build:
swift build -c release --target Flare

test:
xcodebuild test -scheme "Flare" -destination "$(destination)" -testPlan AllTests clean -enableCodeCoverage YES

.PHONY: all bootstrap hook mint lint fmt generate setup_build_tools strings build test
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "0.0.1"
}
},
{
"identity" : "log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/space-code/log",
"state" : {
"revision" : "d99fff5656c31ef7e604965b90a50ec10539c98f",
"version" : "1.1.0"
}
},
{
"identity" : "swift-docc-plugin",
"kind" : "remoteSourceControl",
Expand Down
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/space-code/concurrency.git", .upToNextMajor(from: "0.0.1")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
.package(url: "https://github.com/space-code/log.git", .upToNextMajor(from: "1.1.0")),
],
targets: [
.target(
name: "Flare",
dependencies: [
.product(name: "Concurrency", package: "concurrency"),
.product(name: "Log", package: "log"),
],
resources: [.process("Resources")],
swiftSettings: [visionOSSetting]
),
.testTarget(
Expand Down
5 changes: 4 additions & 1 deletion Package@swift-5.7.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/space-code/concurrency.git", .upToNextMajor(from: "0.0.1")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
.package(url: "https://github.com/space-code/log.git", .upToNextMajor(from: "1.1.0")),
],
targets: [
.target(
name: "Flare",
dependencies: [
.product(name: "Concurrency", package: "concurrency"),
]
.product(name: "Log", package: "log"),
],
resources: [.process("Resources")]
),
.testTarget(
name: "FlareTests",
Expand Down
5 changes: 4 additions & 1 deletion Package@swift-5.8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/space-code/concurrency.git", .upToNextMajor(from: "0.0.1")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.1.0"),
.package(url: "https://github.com/space-code/log.git", .upToNextMajor(from: "1.1.0")),
],
targets: [
.target(
name: "Flare",
dependencies: [
.product(name: "Concurrency", package: "concurrency"),
]
.product(name: "Log", package: "log"),
],
resources: [.process("Resources")]
),
.testTarget(
name: "FlareTests",
Expand Down
78 changes: 78 additions & 0 deletions Sources/Flare/Classes/Common/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Flare
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation
import Log

// MARK: - Logger

enum Logger {
// MARK: Properties

private static var defaultLogLevel: LogLevel {
#if DEBUG
return .debug
#endif
return .info
}

private static let `default`: Log.Logger = .init(
printers: [
ConsolePrinter(formatters: Self.formatters),
OSPrinter(subsystem: .subsystem, category: .category, formatters: Self.formatters),
],
logLevel: Self.defaultLogLevel
)

private static var formatters: [ILogFormatter] {
[
PrefixLogFormatter(name: .subsystem),
TimestampLogFormatter(dateFormat: "dd.MM.yyyy hh:mm:ss"),
]
}

static var logLevel: LogLevel {
get { Self.default.logLevel }
set { Self.default.logLevel = newValue }

Check warning on line 38 in Sources/Flare/Classes/Common/Logger.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Flare/Classes/Common/Logger.swift#L37-L38

Added lines #L37 - L38 were not covered by tests
}

// MARK: Static Public Methods

static func debug(message: @autoclosure () -> String) {
log(level: .debug, message: message())
}

static func info(message: @autoclosure () -> String) {
log(level: .info, message: message())
}

static func error(message: @autoclosure () -> String) {
log(level: .fault, message: message())
}

// MARK: Private

private static func log(level: LogLevel, message: @autoclosure () -> String) {
switch level {
case .debug:
Self.default.debug(message: message())
case .info:
Self.default.info(message: message())
case .fault:
Self.default.fault(message: message())
case .error:
Self.default.error(message: message())

Check warning on line 66 in Sources/Flare/Classes/Common/Logger.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Flare/Classes/Common/Logger.swift#L66

Added line #L66 was not covered by tests
default:
break

Check warning on line 68 in Sources/Flare/Classes/Common/Logger.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Flare/Classes/Common/Logger.swift#L68

Added line #L68 was not covered by tests
}
}
}

// MARK: - Constants

private extension String {
static let subsystem = "Flare"
static let category = "iap"
}
25 changes: 21 additions & 4 deletions Sources/Flare/Classes/Flare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright © 2024 Space Code. All rights reserved.
//

import struct Log.LogLevel
import StoreKit

#if os(iOS) || VISION_OS
Expand All @@ -27,6 +28,12 @@
/// Returns a shared `Flare` object.
public static var shared: IFlare { flare }

/// The log level.
public var logLevel: LogLevel {
get { Logger.logLevel }
set { Logger.logLevel = newValue }

Check warning on line 34 in Sources/Flare/Classes/Flare.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Flare/Classes/Flare.swift#L33-L34

Added lines #L33 - L34 were not covered by tests
}

// MARK: Initialization

/// Creates a new `Flare` instance.
Expand Down Expand Up @@ -66,7 +73,7 @@
promotionalOffer: PromotionalOffer?,
completion: @escaping Closure<Result<StoreTransaction, IAPError>>
) {
guard iapProvider.canMakePayments else {
guard checkIfUserCanMakePayments() else {
completion(.failure(.paymentNotAllowed))
return
}
Expand All @@ -82,7 +89,7 @@
}

public func purchase(product: StoreProduct, promotionalOffer: PromotionalOffer?) async throws -> StoreTransaction {
guard iapProvider.canMakePayments else { throw IAPError.paymentNotAllowed }
guard checkIfUserCanMakePayments() else { throw IAPError.paymentNotAllowed }
return try await iapProvider.purchase(product: product, promotionalOffer: promotionalOffer)
}

Expand All @@ -93,7 +100,7 @@
promotionalOffer: PromotionalOffer?,
completion: @escaping SendableClosure<Result<StoreTransaction, IAPError>>
) {
guard iapProvider.canMakePayments else {
guard checkIfUserCanMakePayments() else {
completion(.failure(.paymentNotAllowed))
return
}
Expand All @@ -106,7 +113,7 @@
options: Set<StoreKit.Product.PurchaseOption>,
promotionalOffer: PromotionalOffer?
) async throws -> StoreTransaction {
guard iapProvider.canMakePayments else { throw IAPError.paymentNotAllowed }
guard checkIfUserCanMakePayments() else { throw IAPError.paymentNotAllowed }
return try await iapProvider.purchase(product: product, options: options, promotionalOffer: promotionalOffer)
}

Expand Down Expand Up @@ -167,4 +174,14 @@
try await iapProvider.presentOfferCodeRedeemSheet()
}
#endif

// MARK: Private

private func checkIfUserCanMakePayments() -> Bool {
guard iapProvider.canMakePayments else {
Logger.error(message: L10n.Purchase.cannotPurcaseProduct)
return false
}
return true
}
}
Loading
Loading