Skip to content

Commit

Permalink
ssingle handler
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Sep 14, 2024
1 parent a3f1aac commit c78c921
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions Sources/Logger/Handler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
writing it to disk, or sending it down a pipe or network stream.
*/

public protocol Handler {
public protocol Handler: Sendable {
var name: String { get }
func log(_ value: Sendable, context: Context) async
}

public actor BasicHandler: Handler {
public typealias Logger = @Sendable (Sendable, Context, BasicHandler) async -> Void

let name: String
public let name: String
let showName: Bool
let showSubsystem: Bool
let logger: Logger
Expand Down
2 changes: 1 addition & 1 deletion Sources/Logger/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public actor Manager {

static func initDefaultHandler() -> Handler {
#if os(macOS) || os(iOS)
return OSLogHandler("default")
return OSLogHandler()
#else
return stdoutHandler // TODO: should perhaps be stderr instead?
#endif
Expand Down
17 changes: 11 additions & 6 deletions Tests/LoggerTests/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,28 @@ import Testing

/// Test item that can record when it has been logged.
/// Test log handler which remembers everything that is logged.
class TestHandler: Handler {
actor TestHandler: Handler {

var logged: [Any] = []
var continuation: AsyncStream<Any>.Continuation?

override func log(channel _: Channel, context _: Context, logged: Any) async {
self.logged.append("\(logged)")
continuation?.yield(logged)
func log(_ value: Sendable, context: Context) async {
self.logged.append("\(value)")
continuation?.yield(value)
}

func setContinuation(_ continuation: AsyncStream<Any>.Continuation) {
self.continuation = continuation
}
struct Stream: AsyncSequence {
typealias AsyncIterator = AsyncStream<Any>.Iterator
typealias Element = Any
let handler: TestHandler
func makeAsyncIterator() -> AsyncIterator {
AsyncStream<Any> { continuation in
handler.continuation = continuation
Task {
await handler.setContinuation(continuation)
}
}.makeAsyncIterator()
}

Expand All @@ -49,7 +54,7 @@ func makeTestManager() -> Manager {
struct LoggerTests {
@Test func name() async throws {
let handler = TestHandler("test")
let results = handler.stream
let results = await handler.stream
let channel = Channel("test", handler: handler, manager: makeTestManager())
channel.log("test")
handler.continuation?.finish()
Expand Down

0 comments on commit c78c921

Please sign in to comment.