Skip to content

Commit 4791ea7

Browse files
committed
Log messages from TaskScheduler to a different subsystem
1 parent d326f08 commit 4791ea7

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

Sources/SKCore/TaskScheduler.swift

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public enum TaskDependencyAction<TaskDescription: TaskDescriptionProtocol> {
2020
case cancelAndRescheduleDependency(TaskDescription)
2121
}
2222

23+
private let taskSchedulerSubsystem = "org.swift.sourcekit-lsp.task-scheduler"
24+
2325
public protocol TaskDescriptionProtocol: Identifiable, Sendable, CustomLogStringConvertible {
2426
/// Execute the task.
2527
///
@@ -232,9 +234,11 @@ fileprivate actor QueuedTask<TaskDescription: TaskDescriptionProtocol> {
232234
/// a new task that depends on it. Otherwise a no-op.
233235
nonisolated func elevatePriority(to targetPriority: TaskPriority) {
234236
if priority < targetPriority {
235-
logger.debug(
236-
"Elevating priority of \(self.description.forLogging) from \(self.priority.rawValue) to \(targetPriority.rawValue)"
237-
)
237+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
238+
logger.debug(
239+
"Elevating priority of \(self.description.forLogging) from \(self.priority.rawValue) to \(targetPriority.rawValue)"
240+
)
241+
}
238242
Task(priority: targetPriority) {
239243
await self.resultTask.value
240244
}
@@ -306,7 +310,9 @@ public actor TaskScheduler<TaskDescription: TaskDescriptionProtocol> {
306310
priority: TaskPriority? = nil,
307311
_ taskDescription: TaskDescription
308312
) async -> Task<Void, Never> {
309-
logger.debug("Scheduling \(taskDescription.forLogging)")
313+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
314+
logger.debug("Scheduling \(taskDescription.forLogging)")
315+
}
310316
let queuedTask = await QueuedTask(priority: priority, description: taskDescription)
311317
pendingTasks.append(queuedTask)
312318
Task.detached(priority: priority ?? Task.currentPriority) {
@@ -361,13 +367,17 @@ public actor TaskScheduler<TaskDescription: TaskDescriptionProtocol> {
361367
case .cancelAndRescheduleDependency(let taskDescription):
362368
guard let dependency = self.currentlyExecutingTasks.first(where: { $0.description.id == taskDescription.id })
363369
else {
364-
logger.fault(
365-
"Cannot find task to wait for \(taskDescription.forLogging) in list of currently executing tasks"
366-
)
370+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
371+
logger.fault(
372+
"Cannot find task to wait for \(taskDescription.forLogging) in list of currently executing tasks"
373+
)
374+
}
367375
return nil
368376
}
369377
if !taskDescription.isIdempotent {
370-
logger.fault("Cannot reschedule task '\(taskDescription.forLogging)' since it is not idempotent")
378+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
379+
logger.fault("Cannot reschedule task '\(taskDescription.forLogging)' since it is not idempotent")
380+
}
371381
return dependency
372382
}
373383
if dependency.priority > task.priority {
@@ -378,9 +388,11 @@ public actor TaskScheduler<TaskDescription: TaskDescriptionProtocol> {
378388
case .waitAndElevatePriorityOfDependency(let taskDescription):
379389
guard let dependency = self.currentlyExecutingTasks.first(where: { $0.description.id == taskDescription.id })
380390
else {
381-
logger.fault(
382-
"Cannot find task to wait for '\(taskDescription.forLogging)' in list of currently executing tasks"
383-
)
391+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
392+
logger.fault(
393+
"Cannot find task to wait for '\(taskDescription.forLogging)' in list of currently executing tasks"
394+
)
395+
}
384396
return nil
385397
}
386398
return dependency
@@ -398,9 +410,11 @@ public actor TaskScheduler<TaskDescription: TaskDescriptionProtocol> {
398410
switch taskDependency {
399411
case .cancelAndRescheduleDependency(let taskDescription):
400412
guard let task = self.currentlyExecutingTasks.first(where: { $0.description.id == taskDescription.id }) else {
401-
logger.fault(
402-
"Cannot find task to reschedule \(taskDescription.forLogging) in list of currently executing tasks"
403-
)
413+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
414+
logger.fault(
415+
"Cannot find task to reschedule \(taskDescription.forLogging) in list of currently executing tasks"
416+
)
417+
}
404418
return nil
405419
}
406420
return task
@@ -411,7 +425,9 @@ public actor TaskScheduler<TaskDescription: TaskDescriptionProtocol> {
411425
if !rescheduleTasks.isEmpty {
412426
Task.detached(priority: task.priority) {
413427
for task in rescheduleTasks {
414-
logger.debug("Suspending \(task.description.forLogging)")
428+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
429+
logger.debug("Suspending \(task.description.forLogging)")
430+
}
415431
await task.cancelToBeRescheduled()
416432
}
417433
}
@@ -422,19 +438,25 @@ public actor TaskScheduler<TaskDescription: TaskDescriptionProtocol> {
422438
return
423439
}
424440

425-
logger.debug("Executing \(task.description.forLogging) with priority \(task.priority.rawValue)")
441+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
442+
logger.debug("Executing \(task.description.forLogging) with priority \(task.priority.rawValue)")
443+
}
426444
currentlyExecutingTasks.append(task)
427445
pendingTasks.removeAll(where: { $0 === task })
428446
Task.detached(priority: task.priority) {
429-
logger.debug(
430-
"Execution of \(task.description.forLogging) started with priority \(Task.currentPriority.rawValue)"
431-
)
447+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
448+
logger.debug(
449+
"Execution of \(task.description.forLogging) started with priority \(Task.currentPriority.rawValue)"
450+
)
451+
}
432452
// Await the task's return in a task so that this poker can continue checking if there are more execution
433453
// slots that can be filled with queued tasks.
434454
let finishStatus = await task.execute()
435-
logger.debug(
436-
"Execution of \(task.description.forLogging) finished with priority \(Task.currentPriority.rawValue)"
437-
)
455+
withLoggingSubsystemAndScope(subsystem: taskSchedulerSubsystem, scope: nil) {
456+
logger.debug(
457+
"Execution of \(task.description.forLogging) finished with priority \(Task.currentPriority.rawValue)"
458+
)
459+
}
438460
await self.finalizeTaskExecution(task: task, finishStatus: finishStatus)
439461
}
440462
}

0 commit comments

Comments
 (0)