-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hide some implementation details * Update `CHANGELOG.md` * Add a test The test just checks that the methods don't cause a crash when printing a message to different outputs. * Update `codecov.yml`
- Loading branch information
1 parent
87fa168
commit 9ada899
Showing
14 changed files
with
213 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
Sources/Log/Classes/Helpers/Writers/ConsoleWriter/ConsoleWriter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
// | ||
// log | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - ConsoleWriter | ||
|
||
/// A class that conforms to the IConsoleWriter protocol and writes messages to the console output. | ||
public final class ConsoleWriter: IConsoleWriter { | ||
final class ConsoleWriter: IConsoleWriter { | ||
// MARK: Initialization | ||
|
||
/// Initializes a new ConsoleWriter instance. | ||
public init() {} | ||
init() {} | ||
|
||
// MARK: IConsoleWriter | ||
|
||
public func print(_ message: String) { | ||
func print(_ message: String) { | ||
Swift.print(message) | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
Sources/Log/Classes/Helpers/Writers/ConsoleWriter/IConsoleWriter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 54 additions & 7 deletions
61
Sources/Log/Classes/Helpers/Writers/OSWriter/OSWriter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,70 @@ | ||
// | ||
// log | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import OSLog | ||
|
||
// MARK: - OSWriter | ||
|
||
/// A class that conforms to the IOSWriter protocol and writes log messages to the Apple OSLog system. | ||
public final class OSWriter: IOSWriter { | ||
final class OSWriter: IOSWriter { | ||
// MARK: Properties | ||
|
||
/// The optional subsystem for categorizing log messages. | ||
private let subsystem: String | ||
/// The optional category for categorizing log messages. | ||
private let category: String | ||
|
||
/// An internal lazy property for initializing the OSLog instance. | ||
private var osLog: OSLog { OSLog(subsystem: subsystem, category: category) } | ||
|
||
/// An internal lazy property for initializing WriteStrategy instance. | ||
private lazy var writerStrategy: IOSWriterStrategy = { | ||
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) { | ||
return os.Logger(osLog) | ||
} else { | ||
return LegacyOSLogger(osLog: osLog) | ||
} | ||
}() | ||
|
||
// MARK: Initialization | ||
|
||
/// Creates a new `OSWriter` instance. | ||
public init() {} | ||
/// Creates a `OSWriter` instance. | ||
/// | ||
/// - Parameters: | ||
/// - subsystem: An optional subsystem for categorizing log messages. | ||
/// - category: An optional category for categorizing log messages. | ||
init(subsystem: String, category: String) { | ||
self.subsystem = subsystem | ||
self.category = category | ||
} | ||
|
||
// MARK: IOSWriter | ||
|
||
public func log(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg...) { | ||
os_log(message, log: log, type: type, args) | ||
func log(type: OSLogType, _ message: String) { | ||
writerStrategy.log(type: type, message) | ||
} | ||
} | ||
|
||
// MARK: OSWriter.LegacyOSLogger | ||
|
||
private extension OSWriter { | ||
struct LegacyOSLogger: IOSWriterStrategy { | ||
// MARK: Private | ||
|
||
private let osLog: OSLog | ||
|
||
// MARK: Initialization | ||
|
||
init(osLog: OSLog) { | ||
self.osLog = osLog | ||
} | ||
|
||
// MARK: IOSWriterStrategy | ||
|
||
func log(type: OSLogType, _ message: String) { | ||
os_log("%s", log: osLog, type: type, message) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
Sources/Log/Classes/Helpers/Writers/OSWriter/Strategies/IOSWriterStrategy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// log | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import os | ||
|
||
// MARK: - IOSWriterStrategy | ||
|
||
/// Protocol defining the contract for a logger strategy that writes logs to the iOS system logs using OSLog. | ||
protocol IOSWriterStrategy { | ||
/// Writes a log message to the iOS system logs with the specified log type. | ||
/// | ||
/// - Parameters: | ||
/// - type: The type of the log message (debug, info, error, etc.). | ||
/// - message: The message to be logged. | ||
func log(type: OSLogType, _ message: String) | ||
} | ||
|
||
// MARK: - os.Logger + IOSWriterStrategy | ||
|
||
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) | ||
extension os.Logger: IOSWriterStrategy { | ||
func log(type: OSLogType, _ message: String) { | ||
log(level: type, "\(message)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// log | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Log | ||
import XCTest | ||
|
||
// MARK: - LogIntegrationTests | ||
|
||
final class LogIntegrationTests: XCTestCase { | ||
// MARK: Properties | ||
|
||
private var sut: Logger! | ||
|
||
// MARK: XCTestCase | ||
|
||
override func setUp() { | ||
super.setUp() | ||
let formatters: [ILogFormatter] = [ | ||
TimestampLogFormatter(dateFormat: "dd/MM/yyyy"), | ||
PrefixLogFormatter(name: "LogIntegrationTests"), | ||
] | ||
|
||
sut = Logger( | ||
printers: [ | ||
ConsolePrinter( | ||
formatters: formatters | ||
), | ||
OSPrinter( | ||
subsystem: .subsystem, | ||
category: .category, | ||
formatters: formatters | ||
), | ||
], | ||
logLevel: .all | ||
) | ||
} | ||
|
||
override func tearDown() { | ||
sut = nil | ||
super.tearDown() | ||
} | ||
|
||
// MARK: Tests | ||
|
||
// The test just checks that the methods don't cause a crash | ||
// when printing a message to different outputs. | ||
func test_logDoesNotThrowUnexpectedBehavior_whenLogMessages() { | ||
// 1. Print an info message | ||
sut.info(message: .message) | ||
|
||
// 2. Print a debug message | ||
sut.debug(message: .message) | ||
|
||
// 3. Print an error message | ||
sut.error(message: .message) | ||
|
||
// 4. Print a fault message | ||
sut.fault(message: .message) | ||
} | ||
} | ||
|
||
// MARK: - Constants | ||
|
||
private extension String { | ||
static let subsystem = "subsystem" | ||
static let category = "category" | ||
static let message = "text" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
// | ||
// log | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// Copyright © 2024 Space Code. All rights reserved. | ||
// | ||
|
||
import Log | ||
@testable import Log | ||
import OSLog | ||
|
||
final class OSWriterMock: IOSWriter { | ||
var invokedLog = false | ||
var invokedLogCount = 0 | ||
var invokedLogParameters: (message: StaticString, log: OSLog, type: OSLogType, args: CVarArg)? | ||
var invokedLogParametersList = [(message: StaticString, log: OSLog, type: OSLogType, args: CVarArg)]() | ||
var invokedLogParameters: (type: OSLogType, message: String)? | ||
var invokedLogParametersList = [(type: OSLogType, message: String)]() | ||
|
||
func log(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg...) { | ||
func log(type: OSLogType, _ message: String) { | ||
invokedLog = true | ||
invokedLogCount += 1 | ||
invokedLogParameters = (message, log, type, args) | ||
invokedLogParametersList.append((message, log, type, args)) | ||
invokedLogParameters = (type, message) | ||
invokedLogParametersList.append((type, message)) | ||
} | ||
} |
Oops, something went wrong.