Skip to content

Commit

Permalink
feat: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Jul 14, 2024
1 parent dc6bf8e commit baf835d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 0 additions & 2 deletions Sources/Commands/Run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ extension RunOn {
let runner = ActionRunner(with: handler)
let observer = EventObserver(activeSources)
observer.listener = runner
let sourceNames = activeSources.map { source in source.name.cyan }
logger.info("observed sources: \(sourceNames.joined(separator: ", "))")
observer.runLoop()
}
}
Expand Down
24 changes: 24 additions & 0 deletions Sources/Events/EventLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
struct EventLogger {
private let logger: Logger

init(with logger: Logger) {
self.logger = logger
}

func eventReceived(_ event: Event) {
self.logger.info("received event \(event.source.cyan):\(event.kind.blue) with \(event.target.yellow)")
}

func sourcesSubscribed(_ sources: [EventSource]) {
let names = sources
.map { source in source.name.cyan }
.joined(separator: ", ")
self.logger.info("subscribed to: \(names)")
}

func listenerIsNil() {
self.logger.error("event skipped, because listener is not set")
}
}

let kEventLogger = EventLogger(with: logger.child(prefix: "Events"))
4 changes: 3 additions & 1 deletion Sources/Events/EventObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ class EventObserver: EventListener, EventProvider {
}

func handle(_ event: Event) {
kEventLogger.eventReceived(event)
guard let listener else {
kEventLogger.listenerIsNil()
return
}
listener.handle(event)
}

func subscribeSources() {
for source in sources {
logger.debug("subscribing to \(source.name.cyan)")
source.subscribe()
}
kEventLogger.sourcesSubscribed(sources)
}

func runLoop() {
Expand Down
4 changes: 0 additions & 4 deletions Sources/Runner/ActionLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ struct ActionRunnerLogger {
self.logger.error(String(describing: error))
}
}

func eventReceived(_ event: Event) {
self.logger.info("received event \(event.source.cyan):\(event.kind.blue) with \(event.target.yellow)")
}
}

let kActionLogger = ActionRunnerLogger(with: logger.child(prefix: "Runner"))
1 change: 0 additions & 1 deletion Sources/Runner/ActionRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class ActionRunner: EventListener {
}

func handle(_ event: Event) {
kActionLogger.eventReceived(event)
guard let action = handler.findAction(
source: event.source,
kind: event.kind,
Expand Down

0 comments on commit baf835d

Please sign in to comment.