Skip to content

Commit

Permalink
Hide some implementation details
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-vasilev committed Jan 18, 2024
1 parent 1a163f6 commit b441f43
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 58 deletions.
15 changes: 12 additions & 3 deletions Sources/Log/Classes/Core/Printers/ConsolePrinter.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation
Expand All @@ -15,16 +15,25 @@ public final class ConsolePrinter {
public let formatters: [ILogFormatter]

/// The console writer.
public let consoleWriter: IConsoleWriter
private let consoleWriter: IConsoleWriter

// MARK: Initialization

/// Creates a new `ConsolePrinter` instance.
///
/// - Parameters:
/// - formatters: An array of log formatters for customizing log messages.
public init(formatters: [ILogFormatter]) {
self.formatters = formatters
consoleWriter = ConsoleWriter()
}

/// Creates a new `ConsolePrinter` instance.
///
/// - Parameters:
/// - formatters: An array of log formatters for customizing log messages.
/// - consoleWriter: The console writer.
public init(formatters: [ILogFormatter], consoleWriter: IConsoleWriter = ConsoleWriter()) {
init(formatters: [ILogFormatter], consoleWriter: IConsoleWriter) {
self.formatters = formatters
self.consoleWriter = consoleWriter
}
Expand Down
37 changes: 20 additions & 17 deletions Sources/Log/Classes/Core/Printers/OSPrinter.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation
Expand All @@ -14,15 +14,9 @@ public final class OSPrinter {

/// An array of log formatters used to customize log message output.
public let formatters: [ILogFormatter]
/// The optional subsystem for categorizing log messages.
public let subsystem: String
/// The optional category for categorizing log messages.
public let category: String
/// The os writer.
public let osWriter: IOSWriter

/// An internal lazy property for initializing the OSLog instance.
private lazy var osLog: OSLog = .init(subsystem: subsystem, category: category)
/// An os writer.
private let osWriter: IOSWriter

// MARK: Initialization

Expand All @@ -32,15 +26,24 @@ public final class OSPrinter {
/// - subsystem: An optional subsystem for categorizing log messages.
/// - category: An optional category for categorizing log messages.
/// - formatters: An array of log formatters for customizing log messages.
/// - osWriter: An os writer.
public init(
subsystem: String = "os_printer",
category: String = "",
formatters: [ILogFormatter],
osWriter: IOSWriter = OSWriter()
subsystem: String,
category: String,
formatters: [ILogFormatter]
) {
self.subsystem = subsystem
self.category = category
self.formatters = formatters
osWriter = OSWriter(
subsystem: subsystem,
category: category
)
}

/// Creates a new `OSPrinter` instance.
///
/// - Parameters:
/// - formatters: An array of log formatters for customizing log messages.
/// - osWriter: An os writer.
init(formatters: [ILogFormatter], osWriter: IOSWriter) {
self.formatters = formatters
self.osWriter = osWriter
}
Expand All @@ -52,7 +55,7 @@ extension OSPrinter: IStyleLogStrategy {
public func log(_ message: String, logLevel: LogLevel) {
let message = formatMessage(message, logLevel: logLevel)
let type = sysLogPriority(logLevel)
osWriter.log("%s", log: osLog, type: type, message)
osWriter.log(type: type, message)
}
}

Expand Down
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation

/// A protocol for writing messages to the console.
public protocol IConsoleWriter {
protocol IConsoleWriter {
/// Prints a message to the console output.
///
/// - Parameter message: The message to be printed as a String.
Expand Down
8 changes: 3 additions & 5 deletions Sources/Log/Classes/Helpers/Writers/OSWriter/IOSWriter.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation
Expand All @@ -11,9 +11,7 @@ public protocol IOSWriter {
/// Writes a log message to the specified OSLog.
///
/// - Parameters:
/// - message: A StaticString containing the log message format.
/// - log: An OSLog object representing the log subsystem and category.
/// - type: An OSLogType indicating the log message type.
/// - args: A variadic list of CVarArg values to fill in the message format.
func log(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg...)
/// - message: A variadic list of String values to fill in the message format.
func log(type: OSLogType, _ message: String)
}
61 changes: 54 additions & 7 deletions Sources/Log/Classes/Helpers/Writers/OSWriter/OSWriter.swift
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(iOS 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)
}
}
}
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)")
}
}
4 changes: 2 additions & 2 deletions Tests/LogTests/Mocks/ConsoleWriterMock.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Foundation
import Log
@testable import Log

final class ConsoleWriterMock: IConsoleWriter {
var invokedPrint = false
Expand Down
14 changes: 7 additions & 7 deletions Tests/LogTests/Mocks/OSWriterMock.swift
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))
}
}
4 changes: 2 additions & 2 deletions Tests/LogTests/UnitTests/ConsolePrinterTests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Log
@testable import Log
import XCTest

// MARK: - ConsolePrinterTests
Expand Down
16 changes: 7 additions & 9 deletions Tests/LogTests/UnitTests/OSPrinterTests.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// log
// Copyright © 2023 Space Code. All rights reserved.
// Copyright © 2024 Space Code. All rights reserved.
//

import Log
@testable import Log
import XCTest

// MARK: - OSPrinterTests
Expand All @@ -23,8 +23,6 @@ final class OSPrinterTests: XCTestCase {
formatterMock = LogFormatterMock()
osWriterMock = OSWriterMock()
sut = OSPrinter(
subsystem: .subsystem,
category: .category,
formatters: [formatterMock],
osWriter: osWriterMock
)
Expand All @@ -47,7 +45,7 @@ final class OSPrinterTests: XCTestCase {
sut.log(.message, logLevel: .all)

// then
XCTAssertEqual((osWriterMock.invokedLogParameters?.args as? [String])?.first, .message)
XCTAssertEqual(osWriterMock.invokedLogParameters?.message, .message)
}

func test_thatConsolePrinterLogsMessage_whenLogLevelIsDebug() {
Expand All @@ -58,7 +56,7 @@ final class OSPrinterTests: XCTestCase {
sut.log(.message, logLevel: .debug)

// then
XCTAssertEqual((osWriterMock.invokedLogParameters?.args as? [String])?.first, .message)
XCTAssertEqual(osWriterMock.invokedLogParameters?.message, .message)
}

func test_thatConsolePrinterLogsMessage_whenLogLevelIsInfo() {
Expand All @@ -69,7 +67,7 @@ final class OSPrinterTests: XCTestCase {
sut.log(.message, logLevel: .info)

// then
XCTAssertEqual((osWriterMock.invokedLogParameters?.args as? [String])?.first, .message)
XCTAssertEqual(osWriterMock.invokedLogParameters?.message, .message)
}

func test_thatConsolePrinterLogsMessage_whenLogLevelIsError() {
Expand All @@ -80,7 +78,7 @@ final class OSPrinterTests: XCTestCase {
sut.log(.message, logLevel: .error)

// then
XCTAssertEqual((osWriterMock.invokedLogParameters?.args as? [String])?.first, .message)
XCTAssertEqual(osWriterMock.invokedLogParameters?.message, .message)
}

func test_thatConsolePrinterLogsMessage_whenLogLevelIsFault() {
Expand All @@ -91,7 +89,7 @@ final class OSPrinterTests: XCTestCase {
sut.log(.message, logLevel: .fault)

// then
XCTAssertEqual((osWriterMock.invokedLogParameters?.args as? [String])?.first, .message)
XCTAssertEqual(osWriterMock.invokedLogParameters?.message, .message)
}
}

Expand Down

0 comments on commit b441f43

Please sign in to comment.