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

Remove swift logging when #16

Merged
merged 3 commits into from
Oct 17, 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
14 changes: 0 additions & 14 deletions Package.resolved

This file was deleted.

10 changes: 0 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,9 @@ let package = Package(
targets: ["FelinePine"]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0")
],
targets: [
.target(
name: "FelinePine",
dependencies: [
.product(
name: "Logging",
package: "swift-log",
condition: .when(platforms: [.linux, .android, .wasi, .windows])
)
],
swiftSettings: [
SwiftSetting.enableUpcomingFeature("BareSlashRegexLiterals"),
SwiftSetting.enableUpcomingFeature("ConciseMagicFile"),
Expand Down
15 changes: 9 additions & 6 deletions Sources/FelinePine/Feline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
//

import Foundation

#if canImport(os)
import os
#else
#elseif canImport(Logging)
import Logging
#endif

Expand All @@ -45,9 +46,11 @@ public protocol Feline {
}
}

extension Feline where Self: Pine {
/// Use the ``loggingCategory`` to define the shared logger for type.
public static var logger: Logger {
LoggingSystemType.logger(forCategory: loggingCategory)
#if canImport(os) || canImport(Logging)
extension Feline where Self: Pine {
/// Use the ``loggingCategory`` to define the shared logger for type.
public static var logger: Logger {
LoggingSystemType.logger(forCategory: loggingCategory)
}
}
}
#endif
9 changes: 8 additions & 1 deletion Sources/FelinePine/Loggable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@

import Foundation

#if canImport(os) || canImport(Logging)
public typealias FelinePineProtocol = Feline & Pine
#else
public typealias FelinePineProtocol = Feline
#endif

/// Loggable type for a ``LoggingSystem``.
public protocol Loggable<LoggingSystemType>: Feline, Pine
public protocol Loggable<LoggingSystemType>: FelinePineProtocol

where LoggingSystemType: LoggingSystem {}
28 changes: 15 additions & 13 deletions Sources/FelinePine/Logger+LoggerCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@
import Foundation
#if canImport(os)
import os
#else
#elseif canImport(Logging)
import Logging
#endif

extension Logger {
internal init<Category: RawRepresentable>(
subsystem: String,
category: Category
) where Category.RawValue == String {
#if canImport(os)
self.init(subsystem: subsystem, category: category.rawValue)
#else
self.init(label: subsystem)
self[metadataKey: "category"] = "\(category)"
#endif
#if canImport(os) || canImport(Logging)
extension Logger {
internal init<Category: RawRepresentable>(
subsystem: String,
category: Category
) where Category.RawValue == String {
#if canImport(os)
self.init(subsystem: subsystem, category: category.rawValue)
#else
self.init(label: subsystem)
self[metadataKey: "category"] = "\(category)"
#endif
}
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/FelinePine/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import os.log
/// Standard os.log Logger
public typealias Logger = os.Logger
#else
#elseif canImport(Logging)
import Logging
/// swift-log Logging.Logger
public typealias Logger = Logging.Logger
Expand Down
114 changes: 114 additions & 0 deletions Sources/FelinePine/LoggingSystem+LoggingSystemRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// LoggingSystem+LoggingSystemRepository.swift
// FelinePine
//
// Created by Leo Dion.
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation
#if canImport(os)
import os
#elseif canImport(Logging)
import Logging
#endif

// swiftlint:disable strict_fileprivate
private class LoggingSystemRepository: @unchecked Sendable {
fileprivate static let shared = LoggingSystemRepository()

private let lock = NSRecursiveLock()
private var items = [String: Any]()

private init(items: [String: Any] = [String: Any]()) {
self.items = items
}

#if canImport(os) || canImport(Logging)
fileprivate func loggingSystem<LoggingSystemType: LoggingSystem>(
for system: LoggingSystemType.Type,
using value: @autoclosure () -> [LoggingSystemType.Category: Logger]
) -> [LoggingSystemType.Category: Logger] {
let anyItem = lock.withLock {
items[system.identifier]
}
if let item = anyItem as? [LoggingSystemType.Category: Logger] {
return item
} else {
assert(anyItem == nil)
return lock.withLock {
let value = value()
items[system.identifier] = value
return value
}
}
}
#endif
}

// swiftlint:enable strict_fileprivate

extension LoggingSystem {
// swiftlint:disable:next missing_docs
public static var identifier: String {
String(reflecting: Self.self)
}

/// By default, this is `Bundle.main.bundleIdentifier`.
public static var subsystem: String {
#if canImport(os)
Bundle.main.bundleIdentifier ?? identifier
#else
identifier
#endif
}
}

#if canImport(os) || canImport(Logging)
extension LoggingSystem where Category: CaseIterable {
private static var loggers: [Category: Logger] {
LoggingSystemRepository.shared.loggingSystem(
for: Self.self,
using: defaultLoggers()
)
}

/// If ``Category`` implements `CaseIterable`, ``LoggingSystem`` can automatically
/// iterate over the cases and automatically create the ``Logger`` objects needed.
public static func logger(forCategory category: Category) -> Logger {
guard let logger = loggers[category] else {
preconditionFailure("missing logger")
}
return logger
}

private static func defaultLoggers() -> [Category: Logger] {
.init(
uniqueKeysWithValues: Category.allCases.map {
($0, Logger(subsystem: Self.subsystem, category: $0))
}
)
}
}
#endif
82 changes: 5 additions & 77 deletions Sources/FelinePine/LoggingSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,10 @@
import Foundation
#if canImport(os)
import os
#else
#elseif canImport(Logging)
import Logging
#endif

// swiftlint:disable strict_fileprivate
private class LoggingSystemRepository: @unchecked Sendable {
fileprivate static let shared = LoggingSystemRepository()

private let lock = NSRecursiveLock()
private var items = [String: Any]()

private init(items: [String: Any] = [String: Any]()) {
self.items = items
}

fileprivate func loggingSystem<LoggingSystemType: LoggingSystem>(
for system: LoggingSystemType.Type,
using value: @autoclosure () -> [LoggingSystemType.Category: Logger]
) -> [LoggingSystemType.Category: Logger] {
let anyItem = lock.withLock {
items[system.identifier]
}
if let item = anyItem as? [LoggingSystemType.Category: Logger] {
return item
} else {
assert(anyItem == nil)
return lock.withLock {
let value = value()
items[system.identifier] = value
return value
}
}
}
}

// swiftlint:enable strict_fileprivate

/// Defines the logging categories for your application.
public protocol LoggingSystem {
/// Logging categories available to types in the application
Expand All @@ -79,47 +46,8 @@ public protocol LoggingSystem {
/// Subsystem to use for each ``Logger``.
/// By default, this is `Bundle.main.bundleIdentifier`.
static var subsystem: String { get }

/// Fetches the correct logger based on the category.
static func logger(forCategory category: Category) -> Logger
}

extension LoggingSystem {
// swiftlint:disable:next missing_docs
public static var identifier: String {
String(reflecting: Self.self)
}

/// By default, this is `Bundle.main.bundleIdentifier`.
public static var subsystem: String {
#if canImport(os)
// swiftlint:disable:next force_unwrapping
Bundle.main.bundleIdentifier!
#else
identifier
#endif
}
}

extension LoggingSystem where Category: CaseIterable {
private static var loggers: [Category: Logger] {
LoggingSystemRepository.shared.loggingSystem(for: Self.self, using: defaultLoggers())
}

/// If ``Category`` implements `CaseIterable`, ``LoggingSystem`` can automatically
/// iterate over the cases and automatically create the ``Logger`` objects needed.
public static func logger(forCategory category: Category) -> Logger {
guard let logger = loggers[category] else {
preconditionFailure("missing logger")
}
return logger
}

private static func defaultLoggers() -> [Category: Logger] {
.init(
uniqueKeysWithValues: Category.allCases.map {
($0, Logger(subsystem: Self.subsystem, category: $0))
}
)
}
#if canImport(os) || canImport(Logging)
/// Fetches the correct logger based on the category.
static func logger(forCategory category: Category) -> Logger
#endif
}
20 changes: 11 additions & 9 deletions Sources/FelinePine/Pine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@
import Foundation
#if canImport(os)
import os
#else
#elseif canImport(Logging)
import Logging
#endif

/// Defines a shared logger for the type.
///
/// Provides a shared ``Logger`` to use in this type.
public protocol Pine {
/// Shared logger for Type.
static var logger: Logger {
get
#if canImport(os) || canImport(Logging)
/// Defines a shared logger for the type.
///
/// Provides a shared ``Logger`` to use in this type.
public protocol Pine {
/// Shared logger for Type.
static var logger: Logger {
get
}
}
}
#endif
10 changes: 7 additions & 3 deletions Tests/FelinePineTests/FelinePineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
import XCTest

internal final class FelinePineTests: XCTestCase {
internal func testLogger() {
_ = MockType.logger
XCTAssert(true)
internal func testLogger() throws {
#if canImport(os) || canImport(Logging)
_ = MockType.logger
XCTAssert(true)
#else
throw XCTSkip("No Logger available.")
#endif
}
}
12 changes: 8 additions & 4 deletions Tests/FelinePineTests/LoggingSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ internal final class LoggingSystemTests: XCTestCase {
}
}

internal func testLogger() {
for category in MockSystem.Category.allCases {
_ = MockSystem.logger(forCategory: category)
}
internal func testLogger() throws {
#if canImport(os) || canImport(Logging)
for category in MockSystem.Category.allCases {
_ = MockSystem.logger(forCategory: category)
}
#else
throw XCTSkip("Logger not available.")
#endif
}
}
Loading
Loading