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

Add method to support flushing log message #79

Merged
merged 4 commits into from
Mar 1, 2023
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 @@ -2,6 +2,7 @@

- Add different error handling behaviors for disk writing errors. [#75](https://github.com/sushichop/Puppy/pull/75)
- Make `logMessage` method public. [#77](https://github.com/sushichop/Puppy/pull/77)
- Add method to support flushing log message. [#79](https://github.com/sushichop/Puppy/pull/79)

## [0.6.0](https://github.com/sushichop/Puppy/releases/tag/0.6.0) (2022-11-29)

Expand Down
8 changes: 8 additions & 0 deletions Sources/Puppy/Loggerable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public protocol Loggerable: Sendable {
func pickMessage(_ level: LogLevel, message: String, tag: String, function: String, file: String, line: UInt, swiftLogInfo: [String: String], label: String, date: Date, threadID: UInt64)

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

func flush(completion: @escaping @Sendable () -> Void)
}

extension Loggerable {
Expand All @@ -24,4 +26,10 @@ extension Loggerable {
log(level, string: formattedMessage)
}
}

public func flush(completion: @escaping @Sendable () -> Void) {
queue.async {
completion()
}
}
}
29 changes: 29 additions & 0 deletions Sources/Puppy/Puppy.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@preconcurrency import Dispatch
import Foundation
#if canImport(Darwin)
#elseif os(Linux)
Expand Down Expand Up @@ -78,6 +79,29 @@ public struct Puppy: Sendable {
}
}

public func flush(_ timeout: Double = 3.0) -> WaitingResult {
let date = Date()
let threadID = currentThreadID()

let group = DispatchGroup()
for logger in loggers {
group.enter()
logger.flush {
puppyDebug("before group.leave, date: \(date), threadID: \(threadID)")
group.leave()
puppyDebug("after group.leave, date: \(date), threadID: \(threadID)")
}
}

let result = group.wait(timeout: .now() + .seconds(Int(timeout)))
switch result {
case .success:
return .success
case .timedOut:
return .timeout
}
}

@usableFromInline
func currentThreadID() -> UInt64 {
var threadID: UInt64 = 0
Expand All @@ -93,6 +117,11 @@ public struct Puppy: Sendable {
}
}

public enum WaitingResult {
case success
case timeout
}

@inlinable
func puppyDebug(_ items: Any) {
#if DEBUG && PUPPY_DEBUG
Expand Down
19 changes: 19 additions & 0 deletions Tests/PuppyTests/PuppyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,23 @@ final class PuppyTests: XCTestCase {
"42".colorize(.colorNumber(42))
)
}

func testFlush() {
let consoleLogger: ConsoleLogger = .init("com.example.yourapp.consolelogger.wait", logLevel: .info)
var log = Puppy()
log.add(consoleLogger)

log.info("INFO message for testFlush")
log.notice("NOTICE message for testFlush")
XCTAssertEqual(log.flush(3.0), .success)

log.warning("WARNING message for testFlush")
log.error("ERROR message for testFlush")
consoleLogger.flush {
Thread.sleep(forTimeInterval: 1.0)
}
XCTAssertEqual(log.flush(0.5), .timeout)

log.removeAll()
}
}