Skip to content

Commit

Permalink
Merge pull request #156 from fwcd/async-await
Browse files Browse the repository at this point in the history
Begin migrating to Swift Concurrency by introducing async/await at the top-level
  • Loading branch information
fwcd authored Jul 1, 2024
2 parents 4677649 + 3ea4513 commit a80d052
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 32 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser.git",
"state" : {
"revision" : "9564d61b08a5335ae0a36f789a7d71493eacadfc",
"version" : "0.3.2"
"revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b",
"version" : "1.4.0"
}
},
{
Expand Down Expand Up @@ -184,8 +184,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/fwcd/swift-gif.git",
"state" : {
"revision" : "0257a56fdab50ae0e661e5ece226d8215cbe28f4",
"version" : "3.0.2"
"revision" : "7b194cddaac97cc90d8fb192e0db6cfc99f11bf6",
"version" : "3.1.0"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ let package = Package(
.package(url: "https://github.com/attaswift/BigInt.git", from: "5.3.0"),
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.4.3"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.4.2"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.3.2"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
.package(url: "https://github.com/fwcd/swift-qrcode-generator.git", from: "1.0.0"),
.package(url: "https://github.com/fwcd/swift-prolog.git", from: "0.1.0"),
.package(url: "https://github.com/fwcd/swift-utils.git", from: "3.0.4"),
.package(url: "https://github.com/fwcd/swift-graphics.git", from: "3.0.1"),
.package(url: "https://github.com/fwcd/swift-gif.git", from: "3.0.2"),
.package(url: "https://github.com/fwcd/swift-gif.git", from: "3.1.0"),
.package(url: "https://github.com/fwcd/swift-mensa.git", from: "0.1.10"),
.package(url: "https://github.com/fwcd/swift-music-theory.git", from: "0.1.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.59.0"),
Expand Down
10 changes: 5 additions & 5 deletions Sources/D2/D2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Backtrace
#endif

@main
struct D2: ParsableCommand {
struct D2: AsyncParsableCommand {
@Option(name: .shortAndLong, help: "The logging level")
var logLevel: Logger.Level?

Expand All @@ -25,7 +25,7 @@ struct D2: ParsableCommand {
@Option(name: .shortAndLong, help: "The initial activity message")
var initialPresence: String?

func run() throws {
func run() async throws {
#if DEBUG && swift(<5.9)
Backtrace.install()
#endif
Expand All @@ -41,12 +41,12 @@ struct D2: ParsableCommand {
let logOutput = LogOutput()

if printToStdout {
logOutput.registerAsync {
await logOutput.register {
print($0)
}
}

logOutput.registerAsync {
await logOutput.register {
logBuffer.push($0)
}

Expand Down Expand Up @@ -136,7 +136,7 @@ struct D2: ParsableCommand {

// Register channel log output if needed
if let logChannel = config?.log?.channel {
logOutput.registerAsync {
await logOutput.register {
combinedSink.sendMessage($0, to: logChannel)
}
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/D2/D2LogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public struct D2LogHandler: LogHandler {
let mergedMetadata = self.metadata.merging(metadata ?? [:], uniquingKeysWith: { _, newKey in newKey })
let output = "\(timestamp(using: mergedMetadata)) [\(level)] \(label): \(message)"

logOutput.publishAsync(output)
Task {
await logOutput.publish(output)
}
}

private func timestamp(using metadata: Logger.Metadata?) -> String {
Expand Down
24 changes: 4 additions & 20 deletions Sources/D2/LogOutput.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
import Dispatch

/// A simple pub/sub facility for publishing log messages.
class LogOutput {
private static let dispatchQueue = DispatchQueue(label: "D2.LogOutput")

actor LogOutput {
private var isSilenced: Bool = false
private var consumers: [(String) -> Void] = []

/// Registers a consumer for log messages.
private func register(_ consumer: @escaping (String) -> Void) {
func register(_ consumer: @escaping (String) -> Void) {
consumers.append(consumer)
}

/// Publishes a log message.
private func publish(_ message: String) {
func publish(_ message: String) {
guard !isSilenced else { return }
isSilenced = true
for consumer in consumers {
consumer(message)
}
isSilenced = false
}

/// Registers a consumer for log messages asynchronously.
func registerAsync(_ consumer: @escaping (String) -> Void) {
Self.dispatchQueue.async {
self.consumers.append(consumer)
}
}

/// Publishes a log message asynchronously.
func publishAsync(_ message: String) {
guard !isSilenced else { return }
Self.dispatchQueue.async {
self.publish(message)
}
}
}

0 comments on commit a80d052

Please sign in to comment.