Skip to content

Commit

Permalink
streams
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Sep 27, 2024
1 parent 19b6138 commit bbdc399
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
41 changes: 28 additions & 13 deletions Sources/Logger/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public actor Channel {

let manager: Manager
var handler: Handler
let sequence: LogSequence

static let defaultSubsystem = "com.elegantchaos.logger"

public init(
_ name: String, handler: Handler? = nil,
alwaysEnabled: Bool = false, manager: Manager? = nil
alwaysEnabled: Bool = false, manager: Manager? = nil, autoRun: Bool = true
) {
let manager = manager ?? Manager.shared
let components = name.split(separator: ".")
Expand All @@ -62,8 +63,14 @@ public actor Channel {
self.manager = manager
self.enabled = isEnabled
self.handler = handler ?? Manager.defaultHandler
let s = LogSequence()
self.sequence = s

Task {
await manager.register(channel: self)
if autoRun {
await run()
}
}
}

Expand All @@ -90,10 +97,7 @@ public actor Channel {
channel: self,
file: file, line: line, column: column, function: function, dso: dso)
let value = asSendable(logged)
Task {
let handler = await self.handler
await handler.log(value, context: context)
}
sequence.log(LoggedItem(value: value, context: context))
}
}

Expand All @@ -107,10 +111,7 @@ public actor Channel {
channel: self,
file: file, line: line, column: column, function: function, dso: dso)
let value = asSendable(logged)
Task {
let handler = await self.handler
await handler.log(value, context: context)
}
sequence.log(LoggedItem(value: value, context: context))
}
#endif
}
Expand All @@ -124,10 +125,24 @@ public actor Channel {
manager.fatalHandler(value, self, file, line)
}

/// Flush the channel.
/// When this returns, all output should have been logged by the handler.
public func flush() async {
await handler.flush()
/// Kick off the handler. It will pull items from the
/// stream until it is finished.
/// NB: Under normal conditions this function is called automatically
/// when the channel is created. For testing purposes it's useful to
/// be able to call it manually, in order to wait for all logged items
/// to be processed.
public func run() async {
for await item in sequence {
await handler.log(item.value, context: item.context)
}
}

/// Shut down the channel.
/// Logging items to the channel after this call
/// will do nothing.
/// NB: Under normal conditions this function does not need to be called.
public func shutdown() async {
sequence.continuation.finish()
}
}

Expand Down
5 changes: 3 additions & 2 deletions Tests/LoggerTests/LoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ func withTestChannel(action: @escaping @Sendable (Channel, StreamHandler) async
let handler = StreamHandler("test", continuation: continuation)
let manager = Manager(settings: TestSettings())
let channel = Channel(
"test", handler: handler, alwaysEnabled: true, manager: manager)
"test", handler: handler, alwaysEnabled: true, manager: manager, autoRun: false)
print("running action")
try await action(channel, handler)
print("done")
await channel.flush()
await channel.shutdown()
await channel.run()
print("flushed")
await handler.finish()
} catch {
Expand Down

0 comments on commit bbdc399

Please sign in to comment.