Skip to content

Commit

Permalink
Hide IOSWriter & IConsoleWriter from the public interface (#2)
Browse files Browse the repository at this point in the history
* 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
ns-vasilev committed Jan 19, 2024
1 parent 87fa168 commit 9ada899
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 59 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
#### Added
- Update GitHub Actions workflow
- Added in Pull Request [#3](https://github.com/space-code/log/pull/3).
- Hide `IOSWriter` & `IConsoleWriter` from the public interface
- Added in Pull Request [#2](https://github.com/space-code/log/pull/2).

#### 1.x Releases
- `1.0.x` Releases - [1.0.0](#100)
Expand Down
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(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)
}
}
}
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)")
}
}
70 changes: 70 additions & 0 deletions Tests/LogTests/IntegrationTests/LogIntegrationTests.swift
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"
}
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))
}
}
Loading

0 comments on commit 9ada899

Please sign in to comment.