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 the argument named asynchronous #59

Merged
merged 1 commit into from
Oct 6, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Update to Xcode 14.0.1 and Swift 5.6 or later. [#56](https://github.com/sushichop/Puppy/pull/56)
- Still use Xcode 13.4.1 for executing `pod lib lint`. [#57](https://github.com/sushichop/Puppy/pull/57)
- Remove a property of type FileHandle. [#58](https://github.com/sushichop/Puppy/pull/58)
- Remove the argument named `asynchronous`. [#59](https://github.com/sushichop/Puppy/pull/59)

## [0.5.1](https://github.com/sushichop/Puppy/releases/tag/0.5.1) (2022-09-24)

Expand Down
30 changes: 12 additions & 18 deletions Sources/Puppy/BaseLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import Foundation

public protocol Loggerable: Hashable {
var label: String { get }
var queue: DispatchQueue? { get }
var queue: DispatchQueue { get }

func log(_ level: LogLevel, string: String)
}

extension Loggerable {
public func hash(into hasher: inout Hasher) {
public extension Loggerable {
func hash(into hasher: inout Hasher) {
hasher.combine(label)
}

public static func == (lhs: Self, rhs: Self) -> Bool {
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.label == rhs.label
}
}
Expand All @@ -31,27 +31,21 @@ open class BaseLogger: Loggerable {
func formatMessage(_ level: LogLevel, message: String, tag: String, function: String, file: String, line: UInt, swiftLogInfo: [String: String], label: String, date: Date, threadID: UInt64) {
if !enabled { return }

var tmpFormattedMessage = message
if let format = format {
tmpFormattedMessage = format.formatMessage(level, message: message, tag: tag, function: function, file: file, line: line, swiftLogInfo: swiftLogInfo, label: label, date: date, threadID: threadID)
}

let formattedMessage = tmpFormattedMessage
if let queue = queue {
queue.async {
self.log(level, string: formattedMessage)
queue.async {
var formattedMessage = message
if let format = self.format {
formattedMessage = format.formatMessage(level, message: message, tag: tag, function: function, file: file, line: line, swiftLogInfo: swiftLogInfo, label: label, date: date, threadID: threadID)
}
} else {
log(level, string: formattedMessage)
self.log(level, string: formattedMessage)
}
}

public let label: String
public let queue: DispatchQueue?
public let queue: DispatchQueue

public init(_ label: String, asynchronous: Bool = true) {
public init(_ label: String) {
self.label = label
self.queue = asynchronous ? DispatchQueue(label: label) : nil
self.queue = DispatchQueue(label: label)
}

/// Needs to override this method in the inherited class.
Expand Down
8 changes: 4 additions & 4 deletions Sources/Puppy/FileLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class FileLogger: BaseLogger {
}

public func delete(_ url: URL) -> Result<URL, FileError> {
queue!.sync {
queue.sync {
Result { try FileManager.default.removeItem(at: url) }
.map { url }
.mapError { _ in
Expand All @@ -60,7 +60,7 @@ public class FileLogger: BaseLogger {
}

public func delete(_ url: URL, completion: @escaping (Result<URL, FileError>) -> Void) {
queue!.async {
queue.async {
let result = Result { try FileManager.default.removeItem(at: url) }
.map { url }
.mapError { _ in
Expand All @@ -71,15 +71,15 @@ public class FileLogger: BaseLogger {
}

public func flush(_ url: URL) {
queue!.sync {
queue.sync {
let handle = try? FileHandle(forWritingTo: url)
try? handle?.synchronize()
try? handle?.close()
}
}

public func flush(_ url: URL, completion: @escaping () -> Void) {
queue!.async {
queue.async {
let handle = try? FileHandle(forWritingTo: url)
try? handle?.synchronize()
try? handle?.close()
Expand Down
2 changes: 1 addition & 1 deletion Sources/Puppy/OSLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class OSLogger: BaseLogger {

public init(_ label: String, asynchronous: Bool = true, category: String = "Puppy") {
self.osLog = OSLog(subsystem: label, category: category)
super.init(label, asynchronous: asynchronous)
super.init(label)
}

public override func log(_ level: LogLevel, string: String) {
Expand Down
34 changes: 3 additions & 31 deletions Tests/PuppyTests/BaseLoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,16 @@ final class BaseLoggerTests: XCTestCase {
log.remove(baseLogger)
}

func testMockLoggerSynchronous() throws {
let mockLogger = MockLogger("com.example.yourapp.mocklogger.sync", asynchronous: false)
XCTAssertNil(mockLogger.queue)

let log = Puppy()
log.add(mockLogger, withLevel: .debug)
log.trace("TRACE message")
log.verbose("VERBOSE message")
log.debug("DEBUG message")
log.info("INFO message")

mockLogger.enabled = false
log.notice("NOTICE message")
log.warning("WARNING message")
mockLogger.enabled = true
log.error("ERROR message")
log.critical("CRITICAL message")

let expectedLogLevels: [LogLevel] = [.debug, .info, .error, .critical]
let expectedLogStrings: [String] = ["DEBUG message", "INFO message", "ERROR message", "CRITICAL message"]
XCTAssertEqual(mockLogger.invokedLogCount, 4)
XCTAssertEqual(mockLogger.invokedLogLevels, expectedLogLevels)
XCTAssertEqual(mockLogger.invokedLogStrings, expectedLogStrings)

log.remove(mockLogger)
}

func testMockLoggerAsynchronous() throws {
func testMockLogger() throws {
let mockLogger = MockLogger("com.example.yourapp.mocklogger.async")
XCTAssertNotNil(mockLogger.queue)

let log = Puppy()
log.add(mockLogger, withLevel: .debug)
log.debug("DEBUG message")
log.warning("WARNING message")

let exp = XCTestExpectation(description: "MockLogger Asynchronous")
mockLogger.queue!.async {
mockLogger.queue.async {
XCTAssertEqual(mockLogger.invokedLogCount, 2)
XCTAssertEqual(mockLogger.invokedLogLevels, [.debug, .warning])
XCTAssertEqual(mockLogger.invokedLogStrings, ["DEBUG message", "WARNING message"])
Expand All @@ -87,7 +59,7 @@ final class BaseLoggerTests: XCTestCase {
log.error("ERROR message", tag: "error-tag")

let exp = XCTestExpectation(description: "MockLogger LogFormatter")
mockLogger.queue!.async {
mockLogger.queue.async {
XCTAssertEqual(mockLogger.invokedLogCount, 2)
XCTAssertEqual(mockLogger.invokedLogLevels, [.warning, .error])
XCTAssertEqual(mockLogger.invokedLogStrings, [
Expand Down