Skip to content

Commit 9ada899

Browse files
committed
Hide IOSWriter & IConsoleWriter from the public interface (#2)
* 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`
1 parent 87fa168 commit 9ada899

14 files changed

Lines changed: 213 additions & 59 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
55
#### Added
66
- Update GitHub Actions workflow
77
- Added in Pull Request [#3](https://github.com/space-code/log/pull/3).
8+
- Hide `IOSWriter` & `IConsoleWriter` from the public interface
9+
- Added in Pull Request [#2](https://github.com/space-code/log/pull/2).
810

911
#### 1.x Releases
1012
- `1.0.x` Releases - [1.0.0](#100)

Sources/Log/Classes/Core/Printers/ConsolePrinter.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

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

1717
/// The console writer.
18-
public let consoleWriter: IConsoleWriter
18+
private let consoleWriter: IConsoleWriter
1919

2020
// MARK: Initialization
2121

22+
/// Creates a new `ConsolePrinter` instance.
23+
///
24+
/// - Parameters:
25+
/// - formatters: An array of log formatters for customizing log messages.
26+
public init(formatters: [ILogFormatter]) {
27+
self.formatters = formatters
28+
consoleWriter = ConsoleWriter()
29+
}
30+
2231
/// Creates a new `ConsolePrinter` instance.
2332
///
2433
/// - Parameters:
2534
/// - formatters: An array of log formatters for customizing log messages.
2635
/// - consoleWriter: The console writer.
27-
public init(formatters: [ILogFormatter], consoleWriter: IConsoleWriter = ConsoleWriter()) {
36+
init(formatters: [ILogFormatter], consoleWriter: IConsoleWriter) {
2837
self.formatters = formatters
2938
self.consoleWriter = consoleWriter
3039
}

Sources/Log/Classes/Core/Printers/OSPrinter.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Foundation
@@ -14,15 +14,9 @@ public final class OSPrinter {
1414

1515
/// An array of log formatters used to customize log message output.
1616
public let formatters: [ILogFormatter]
17-
/// The optional subsystem for categorizing log messages.
18-
public let subsystem: String
19-
/// The optional category for categorizing log messages.
20-
public let category: String
21-
/// The os writer.
22-
public let osWriter: IOSWriter
2317

24-
/// An internal lazy property for initializing the OSLog instance.
25-
private lazy var osLog: OSLog = .init(subsystem: subsystem, category: category)
18+
/// An os writer.
19+
private let osWriter: IOSWriter
2620

2721
// MARK: Initialization
2822

@@ -32,15 +26,24 @@ public final class OSPrinter {
3226
/// - subsystem: An optional subsystem for categorizing log messages.
3327
/// - category: An optional category for categorizing log messages.
3428
/// - formatters: An array of log formatters for customizing log messages.
35-
/// - osWriter: An os writer.
3629
public init(
37-
subsystem: String = "os_printer",
38-
category: String = "",
39-
formatters: [ILogFormatter],
40-
osWriter: IOSWriter = OSWriter()
30+
subsystem: String,
31+
category: String,
32+
formatters: [ILogFormatter]
4133
) {
42-
self.subsystem = subsystem
43-
self.category = category
34+
self.formatters = formatters
35+
osWriter = OSWriter(
36+
subsystem: subsystem,
37+
category: category
38+
)
39+
}
40+
41+
/// Creates a new `OSPrinter` instance.
42+
///
43+
/// - Parameters:
44+
/// - formatters: An array of log formatters for customizing log messages.
45+
/// - osWriter: An os writer.
46+
init(formatters: [ILogFormatter], osWriter: IOSWriter) {
4447
self.formatters = formatters
4548
self.osWriter = osWriter
4649
}
@@ -52,7 +55,7 @@ extension OSPrinter: IStyleLogStrategy {
5255
public func log(_ message: String, logLevel: LogLevel) {
5356
let message = formatMessage(message, logLevel: logLevel)
5457
let type = sysLogPriority(logLevel)
55-
osWriter.log("%s", log: osLog, type: type, message)
58+
osWriter.log(type: type, message)
5659
}
5760
}
5861

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Foundation
77

88
// MARK: - ConsoleWriter
99

1010
/// A class that conforms to the IConsoleWriter protocol and writes messages to the console output.
11-
public final class ConsoleWriter: IConsoleWriter {
11+
final class ConsoleWriter: IConsoleWriter {
1212
// MARK: Initialization
1313

1414
/// Initializes a new ConsoleWriter instance.
15-
public init() {}
15+
init() {}
1616

1717
// MARK: IConsoleWriter
1818

19-
public func print(_ message: String) {
19+
func print(_ message: String) {
2020
Swift.print(message)
2121
}
2222
}

Sources/Log/Classes/Helpers/Writers/ConsoleWriter/IConsoleWriter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Foundation
77

88
/// A protocol for writing messages to the console.
9-
public protocol IConsoleWriter {
9+
protocol IConsoleWriter {
1010
/// Prints a message to the console output.
1111
///
1212
/// - Parameter message: The message to be printed as a String.
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Foundation
@@ -11,9 +11,7 @@ public protocol IOSWriter {
1111
/// Writes a log message to the specified OSLog.
1212
///
1313
/// - Parameters:
14-
/// - message: A StaticString containing the log message format.
15-
/// - log: An OSLog object representing the log subsystem and category.
1614
/// - type: An OSLogType indicating the log message type.
17-
/// - args: A variadic list of CVarArg values to fill in the message format.
18-
func log(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg...)
15+
/// - message: A variadic list of String values to fill in the message format.
16+
func log(type: OSLogType, _ message: String)
1917
}
Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Foundation
77
import OSLog
88

99
// MARK: - OSWriter
1010

11-
/// A class that conforms to the IOSWriter protocol and writes log messages to the Apple OSLog system.
12-
public final class OSWriter: IOSWriter {
11+
final class OSWriter: IOSWriter {
12+
// MARK: Properties
13+
14+
/// The optional subsystem for categorizing log messages.
15+
private let subsystem: String
16+
/// The optional category for categorizing log messages.
17+
private let category: String
18+
19+
/// An internal lazy property for initializing the OSLog instance.
20+
private var osLog: OSLog { OSLog(subsystem: subsystem, category: category) }
21+
22+
/// An internal lazy property for initializing WriteStrategy instance.
23+
private lazy var writerStrategy: IOSWriterStrategy = {
24+
if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
25+
return os.Logger(osLog)
26+
} else {
27+
return LegacyOSLogger(osLog: osLog)
28+
}
29+
}()
30+
1331
// MARK: Initialization
1432

15-
/// Creates a new `OSWriter` instance.
16-
public init() {}
33+
/// Creates a `OSWriter` instance.
34+
///
35+
/// - Parameters:
36+
/// - subsystem: An optional subsystem for categorizing log messages.
37+
/// - category: An optional category for categorizing log messages.
38+
init(subsystem: String, category: String) {
39+
self.subsystem = subsystem
40+
self.category = category
41+
}
1742

1843
// MARK: IOSWriter
1944

20-
public func log(_ message: StaticString, log: OSLog, type: OSLogType, _ args: CVarArg...) {
21-
os_log(message, log: log, type: type, args)
45+
func log(type: OSLogType, _ message: String) {
46+
writerStrategy.log(type: type, message)
47+
}
48+
}
49+
50+
// MARK: OSWriter.LegacyOSLogger
51+
52+
private extension OSWriter {
53+
struct LegacyOSLogger: IOSWriterStrategy {
54+
// MARK: Private
55+
56+
private let osLog: OSLog
57+
58+
// MARK: Initialization
59+
60+
init(osLog: OSLog) {
61+
self.osLog = osLog
62+
}
63+
64+
// MARK: IOSWriterStrategy
65+
66+
func log(type: OSLogType, _ message: String) {
67+
os_log("%s", log: osLog, type: type, message)
68+
}
2269
}
2370
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// log
3+
// Copyright © 2024 Space Code. All rights reserved.
4+
//
5+
6+
import os
7+
8+
// MARK: - IOSWriterStrategy
9+
10+
/// Protocol defining the contract for a logger strategy that writes logs to the iOS system logs using OSLog.
11+
protocol IOSWriterStrategy {
12+
/// Writes a log message to the iOS system logs with the specified log type.
13+
///
14+
/// - Parameters:
15+
/// - type: The type of the log message (debug, info, error, etc.).
16+
/// - message: The message to be logged.
17+
func log(type: OSLogType, _ message: String)
18+
}
19+
20+
// MARK: - os.Logger + IOSWriterStrategy
21+
22+
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
23+
extension os.Logger: IOSWriterStrategy {
24+
func log(type: OSLogType, _ message: String) {
25+
log(level: type, "\(message)")
26+
}
27+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// log
3+
// Copyright © 2024 Space Code. All rights reserved.
4+
//
5+
6+
import Log
7+
import XCTest
8+
9+
// MARK: - LogIntegrationTests
10+
11+
final class LogIntegrationTests: XCTestCase {
12+
// MARK: Properties
13+
14+
private var sut: Logger!
15+
16+
// MARK: XCTestCase
17+
18+
override func setUp() {
19+
super.setUp()
20+
let formatters: [ILogFormatter] = [
21+
TimestampLogFormatter(dateFormat: "dd/MM/yyyy"),
22+
PrefixLogFormatter(name: "LogIntegrationTests"),
23+
]
24+
25+
sut = Logger(
26+
printers: [
27+
ConsolePrinter(
28+
formatters: formatters
29+
),
30+
OSPrinter(
31+
subsystem: .subsystem,
32+
category: .category,
33+
formatters: formatters
34+
),
35+
],
36+
logLevel: .all
37+
)
38+
}
39+
40+
override func tearDown() {
41+
sut = nil
42+
super.tearDown()
43+
}
44+
45+
// MARK: Tests
46+
47+
// The test just checks that the methods don't cause a crash
48+
// when printing a message to different outputs.
49+
func test_logDoesNotThrowUnexpectedBehavior_whenLogMessages() {
50+
// 1. Print an info message
51+
sut.info(message: .message)
52+
53+
// 2. Print a debug message
54+
sut.debug(message: .message)
55+
56+
// 3. Print an error message
57+
sut.error(message: .message)
58+
59+
// 4. Print a fault message
60+
sut.fault(message: .message)
61+
}
62+
}
63+
64+
// MARK: - Constants
65+
66+
private extension String {
67+
static let subsystem = "subsystem"
68+
static let category = "category"
69+
static let message = "text"
70+
}

Tests/LogTests/Mocks/ConsoleWriterMock.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//
22
// log
3-
// Copyright © 2023 Space Code. All rights reserved.
3+
// Copyright © 2024 Space Code. All rights reserved.
44
//
55

66
import Foundation
7-
import Log
7+
@testable import Log
88

99
final class ConsoleWriterMock: IConsoleWriter {
1010
var invokedPrint = false

0 commit comments

Comments
 (0)