Skip to content

Commit be1165d

Browse files
authored
Forward the child process output type to the logger (#174)
1 parent 05e1ccb commit be1165d

8 files changed

+40
-12
lines changed

src/Logger/Logger.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@ public function logChildProcessFinished(int $index): void;
6969
* process expects the child to output the progress symbol to communicate its
7070
* progression. Any other sort of output is considered "unexpected".
7171
*
72-
* @param string $buffer Child process output.
7372
* @param positive-int|0 $index Index of the process amoung the list of running processes.
7473
* @param int|null $pid The child process PID. It can be null if the process is no
7574
* longer running.
75+
* @param string $type The type of output: "out" or "err".
76+
* @param string $buffer Child process output.
7677
*/
7778
public function logUnexpectedChildProcessOutput(
7879
int $index,
7980
?int $pid,
81+
string $type,
8082
string $buffer,
8183
string $progressSymbol
8284
): void;

src/Logger/NullLogger.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ public function logChildProcessFinished(int $index): void
5858
// Do nothing.
5959
}
6060

61-
public function logUnexpectedChildProcessOutput(int $index, ?int $pid, string $buffer, string $progressSymbol): void
62-
{
61+
public function logUnexpectedChildProcessOutput(
62+
int $index,
63+
?int $pid,
64+
string $type,
65+
string $buffer,
66+
string $progressSymbol
67+
): void {
6368
// Do nothing.
6469
}
6570
}

src/Logger/StandardLogger.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,13 @@ public function logChildProcessFinished(int $index): void
148148
$this->logger->debug('Command finished');
149149
}
150150

151-
public function logUnexpectedChildProcessOutput(int $index, ?int $pid, string $buffer, string $progressSymbol): void
152-
{
151+
public function logUnexpectedChildProcessOutput(
152+
int $index,
153+
?int $pid,
154+
string $type,
155+
string $buffer,
156+
string $progressSymbol
157+
): void {
153158
$this->output->writeln('');
154159
$this->output->writeln(sprintf(
155160
'<comment>%s</comment>',

src/ParallelExecutor.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ private function createProcessLauncher(
356356
fn (int $index, ?int $pid, string $type, string $buffer) => $this->processChildOutput(
357357
$index,
358358
$pid,
359+
$type,
359360
$buffer,
360361
$logger,
361362
),
@@ -364,17 +365,18 @@ private function createProcessLauncher(
364365
}
365366

366367
/**
367-
* TODO: pass the type
368368
* Called whenever data is received in the main process from a child process.
369369
*
370370
* @param positive-int|0 $index Index of the process amoung the list of running processes.
371371
* @param int|null $pid The child process PID. It can be null if the process is no
372372
* longer running.
373-
* @param string $buffer The received data
373+
* @param string $type The type of output: "out" or "err".
374+
* @param string $buffer The received data.
374375
*/
375376
private function processChildOutput(
376377
int $index,
377378
?int $pid,
379+
string $type,
378380
string $buffer,
379381
Logger $logger
380382
): void {
@@ -386,6 +388,7 @@ private function processChildOutput(
386388
$logger->logUnexpectedChildProcessOutput(
387389
$index,
388390
$pid,
391+
$type,
389392
$buffer,
390393
$progressSymbol,
391394
);

tests/Logger/DummyLogger.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ public function logChildProcessFinished(int $index): void
8282
];
8383
}
8484

85-
public function logUnexpectedChildProcessOutput(int $index, ?int $pid, string $buffer, string $progressSymbol): void
86-
{
85+
public function logUnexpectedChildProcessOutput(
86+
int $index,
87+
?int $pid,
88+
string $type,
89+
string $buffer,
90+
string $progressSymbol
91+
): void {
8792
$this->records[] = [
8893
__FUNCTION__,
8994
func_get_args(),

tests/Logger/FakeLogger.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ public function logChildProcessFinished(int $index): void
5959
throw new DomainException('Unexpected call.');
6060
}
6161

62-
public function logUnexpectedChildProcessOutput(int $index, ?int $pid, string $buffer, string $progressSymbol): void
63-
{
62+
public function logUnexpectedChildProcessOutput(
63+
int $index,
64+
?int $pid,
65+
string $type,
66+
string $buffer,
67+
string $progressSymbol
68+
): void {
6469
throw new DomainException('Unexpected call.');
6570
}
6671
}

tests/Logger/StandardLoggerTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public function test_it_can_log_the_unexpected_output_of_a_child_process(): void
434434
$this->logger->logUnexpectedChildProcessOutput(
435435
2,
436436
23123,
437+
'out',
437438
'An error occurred.',
438439
self::ADVANCEMENT_CHARACTER,
439440
);
@@ -458,6 +459,7 @@ public function test_it_can_log_the_unexpected_output_of_a_stopped_child_process
458459
$this->logger->logUnexpectedChildProcessOutput(
459460
2,
460461
null,
462+
'out',
461463
'An error occurred.',
462464
self::ADVANCEMENT_CHARACTER,
463465
);
@@ -482,6 +484,7 @@ public function test_it_removes_the_progress_character_of_the_unexpected_output_
482484
$this->logger->logUnexpectedChildProcessOutput(
483485
23,
484486
23132,
487+
'out',
485488
'An error'.self::ADVANCEMENT_CHARACTER.' occurred.',
486489
self::ADVANCEMENT_CHARACTER,
487490
);

tests/ParallelExecutorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ public function test_it_processes_the_child_processes_output(): void
735735
],
736736
[
737737
'logUnexpectedChildProcessOutput',
738-
[10, null, 'FOO', $progressSymbol],
738+
[10, null, 'test', 'FOO', $progressSymbol],
739739
],
740740
[
741741
'logAdvance',

0 commit comments

Comments
 (0)