Skip to content

Commit 80ec749

Browse files
committed
Improved console output format
- Added a bit more of performance information - Deleted Mb from MEM size - Change format of output lines - Fixed calculation of MEM size (divided by 1024 instead of 1000)
1 parent 65451b9 commit 80ec749

13 files changed

Lines changed: 242 additions & 46 deletions

src/Console/RunServerCommand.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Drift\Console\OutputPrinter;
1919
use Drift\Server\Adapter\KernelAdapter;
2020
use Drift\Server\Application;
21-
use Drift\Server\ConsoleServerMessage;
21+
use Drift\Server\ConsoleMasterServerMessage;
2222
use Drift\Server\Context\ServerContext;
2323
use Drift\Server\Mime\MimeTypeChecker;
2424
use React\EventLoop\LoopInterface;
@@ -68,14 +68,15 @@ protected function executeServerCommand(
6868
$filesystem
6969
)
7070
->then(function (KernelAdapter $kernelAdapter) use ($application, $outputPrinter, $serverContext, $filesystem, &$forceShutdownReference) {
71-
(new ConsoleServerMessage('Kernel built.', '~', true))->print($outputPrinter);
72-
(new ConsoleServerMessage('Kernel preloaded.', '~', true))->print($outputPrinter);
73-
(new ConsoleServerMessage('Kernel ready to accept requests.', '~', true))->print($outputPrinter);
71+
(new ConsoleMasterServerMessage('Kernel built.', true))->print($outputPrinter);
72+
(new ConsoleMasterServerMessage('Kernel preloaded.', true))->print($outputPrinter);
73+
(new ConsoleMasterServerMessage('Kernel ready to accept requests.', true))->print($outputPrinter);
7474

7575
if ($serverContext->hasExchanges()) {
76-
(new ConsoleServerMessage('Kernel connected to exchanges.', '~', true))->print($outputPrinter);
76+
(new ConsoleMasterServerMessage('Kernel connected to exchanges.', true))->print($outputPrinter);
7777
}
7878

79+
(new ConsoleMasterServerMessage('...', true))->print($outputPrinter);
7980
$applicationCallback = function () use ($application, $kernelAdapter, $filesystem, &$forceShutdownReference) {
8081
$application->runServer(
8182
$kernelAdapter,
@@ -90,9 +91,10 @@ protected function executeServerCommand(
9091
$this->forkApplication($serverContext->getWorkers(), $applicationCallback, $outputPrinter);
9192
}
9293
}, function (Throwable $e) use ($outputPrinter) {
93-
(new ConsoleServerMessage($e->getMessage(), '~', false))->print($outputPrinter);
94-
(new ConsoleServerMessage('The server will shut down.', '~', false))->print($outputPrinter);
95-
(new ConsoleServerMessage('Bye!', '~', false))->print($outputPrinter);
94+
(new ConsoleMasterServerMessage('...', false))->print($outputPrinter);
95+
(new ConsoleMasterServerMessage($e->getMessage(), false))->print($outputPrinter);
96+
(new ConsoleMasterServerMessage('The server will shut down.', false))->print($outputPrinter);
97+
(new ConsoleMasterServerMessage('Bye!', false))->print($outputPrinter);
9698
});
9799
}
98100

@@ -117,7 +119,7 @@ private function forkApplication(
117119

118120
case 0:
119121
$GLOBALS['number_of_process'] = str_pad(\strval($numberOfFork), 2, '0', STR_PAD_LEFT);
120-
(new ConsoleServerMessage("Worker #$numberOfFork starting", '~', true))->print($outputPrinter);
122+
(new ConsoleMasterServerMessage("Worker #$numberOfFork starting", true))->print($outputPrinter);
121123
$callable();
122124
break;
123125

src/Console/ServerCommand.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
use Drift\EventLoop\EventLoopUtils;
2121
use Drift\Server\Console\Style\Muted;
2222
use Drift\Server\Console\Style\Purple;
23-
use Drift\Server\ConsoleServerMessage;
23+
use Drift\Server\ConsoleMasterServerMessage;
2424
use Drift\Server\Context\ServerContext;
2525
use Drift\Server\ServerHeaderPrinter;
2626
use Exception;
2727
use React\EventLoop\Loop;
2828
use React\EventLoop\LoopInterface;
2929
use Symfony\Component\Console\Command\Command;
30+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
3031
use Symfony\Component\Console\Input\InputArgument;
3132
use Symfony\Component\Console\Input\InputInterface;
3233
use Symfony\Component\Console\Input\InputOption;
@@ -134,21 +135,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
134135
$forceShutdownReference
135136
);
136137

137-
(new ConsoleServerMessage('EventLoop is running.', '~', true))->print($outputPrinter);
138+
(new ConsoleMasterServerMessage('EventLoop is running.', true))->print($outputPrinter);
138139
EventLoopUtils::runLoop($loop, (\intval($input->getOption('allowed-loop-stops')) + 1), function (int $timesMissing) use ($outputPrinter, &$forceShutdownReference) {
139140
if ($forceShutdownReference) {
140-
(new ConsoleServerMessage(
141-
sprintf('Loop forced to stop.'), '~', false)
141+
(new ConsoleMasterServerMessage(
142+
sprintf('Loop forced to stop.'), false)
142143
)->print($outputPrinter);
143144
} else {
144-
(new ConsoleServerMessage(
145-
sprintf('Rerunning EventLoop. %d retries missing', $timesMissing), '~', false)
145+
(new ConsoleMasterServerMessage(
146+
sprintf('Rerunning EventLoop. %d retries missing', $timesMissing), false)
146147
)->print($outputPrinter);
147148
}
148149
}, $forceShutdownReference);
149-
(new ConsoleServerMessage('EventLoop stopped.', '~', false))->print($outputPrinter);
150-
(new ConsoleServerMessage('Closing the server.', '~', false))->print($outputPrinter);
151-
(new ConsoleServerMessage('Bye bye!.', '~', false))->print($outputPrinter);
150+
(new ConsoleMasterServerMessage('...', false))->print($outputPrinter);
151+
(new ConsoleMasterServerMessage('EventLoop stopped.', false))->print($outputPrinter);
152+
(new ConsoleMasterServerMessage('Closing the server.', false))->print($outputPrinter);
153+
(new ConsoleMasterServerMessage('Bye bye!.', false))->print($outputPrinter);
152154

153155
return 0;
154156
}
@@ -186,6 +188,7 @@ protected function createOutputPrinter(
186188
$outputFormatter = $output->getFormatter();
187189
$outputFormatter->setStyle('muted', new Muted());
188190
$outputFormatter->setStyle('purple', new Purple());
191+
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
189192

190193
return new OutputPrinter($output, $isQuiet, $isAlmostQuiet);
191194
}

src/Console/Style/Performance.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Drift Server
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\Server\Console\Style;
17+
18+
/**
19+
* Class Purple.
20+
*/
21+
final class Performance extends CustomStyle
22+
{
23+
public function apply(string $text)
24+
{
25+
return sprintf("\033[01;95m%s\e[0m", $text);
26+
}
27+
}

src/ConsoleMasterServerMessage.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Drift Server
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\Server;
17+
18+
use Drift\Console\OutputPrinter;
19+
20+
/**
21+
* Class ConsoleMasterServerMessage.
22+
*/
23+
final class ConsoleMasterServerMessage implements Printable
24+
{
25+
private string $message;
26+
private bool $ok;
27+
28+
/**
29+
* @param string $message
30+
* @param bool $ok
31+
*/
32+
public function __construct(
33+
string $message,
34+
bool $ok
35+
) {
36+
$this->message = $message;
37+
$this->ok = $ok;
38+
}
39+
40+
/**
41+
* Print.
42+
*
43+
* @param OutputPrinter $outputPrinter
44+
*/
45+
public function print(OutputPrinter $outputPrinter)
46+
{
47+
if (!$outputPrinter->shouldPrintImportantOutput()) {
48+
return;
49+
}
50+
51+
$color = 'red';
52+
if ($this->ok) {
53+
$color = 'green';
54+
}
55+
56+
$forkNumber = isset($GLOBALS['number_of_process'])
57+
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
58+
: '';
59+
$outputPrinter->print("$forkNumber<fg=$color;options=bold>SRV</>");
60+
$outputPrinter->print(' ');
61+
$outputPrinter->print('<muted>'.$this->message.'</muted>');
62+
$outputPrinter->printLine();
63+
}
64+
}

src/ConsoleMessage.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Drift Server
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
*/
13+
14+
declare(strict_types=1);
15+
16+
namespace Drift\Server;
17+
18+
abstract class ConsoleMessage implements Printable
19+
{
20+
/**
21+
* @param string $elapsedType
22+
*
23+
* @return string
24+
*/
25+
protected function styledPerformance(string $elapsedType): string
26+
{
27+
$info = [
28+
$this->toLength($elapsedType, str_contains($elapsedType, 'μ') ? 8 : 7),
29+
$this->toLength((string) (int) (memory_get_usage() / 1048576), 4),
30+
$this->toLength((string) (int) (memory_get_usage(true) / 1048576), 4),
31+
];
32+
33+
return '<performance>['.implode('|', $info).']</performance>';
34+
}
35+
36+
/**
37+
* @param string $string
38+
* @param int $length
39+
*
40+
* @return string
41+
*/
42+
protected function toLength(string $string, int $length): string
43+
{
44+
return str_pad($string, $length, ' ', STR_PAD_LEFT);
45+
}
46+
}

src/ConsoleRequestMessage.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Class ConsoleRequestMessage.
2222
*/
23-
final class ConsoleRequestMessage implements Printable
23+
final class ConsoleRequestMessage extends ConsoleMessage
2424
{
2525
private string $url;
2626
private string $method;
@@ -58,7 +58,7 @@ public function __construct(
5858
*/
5959
public function print(OutputPrinter $outputPrinter)
6060
{
61-
$hasError = \intval($this->code) >= 400;
61+
$hasError = $this->code >= 400;
6262
if (
6363
!$outputPrinter->shouldPrintRegularOutput() &&
6464
(!$outputPrinter->shouldPrintImportantOutput() || !$hasError)
@@ -74,12 +74,12 @@ public function print(OutputPrinter $outputPrinter)
7474
$color = 'red';
7575
}
7676

77+
$performance = $this->styledPerformance($this->elapsedTime);
7778
$forkNumber = isset($GLOBALS['number_of_process'])
7879
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
7980
: '';
8081
$outputPrinter->print("$forkNumber<fg=$color;options=bold>{$this->code}</>");
81-
$outputPrinter->print(" $method $this->url ");
82-
$outputPrinter->print('(<muted>'.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB</muted>)');
82+
$outputPrinter->print(" $performance $method $this->url ");
8383
if ($this->code >= 400) {
8484
$outputPrinter->print(' - <muted>'.$this->messageInMessage($this->message).'</muted>');
8585
}

src/ConsoleServerMessage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Class ConsoleServerMessage.
2222
*/
23-
final class ConsoleServerMessage implements Printable
23+
final class ConsoleServerMessage extends ConsoleMessage
2424
{
2525
private string $message;
2626
private string $elapsedTime;
@@ -59,13 +59,13 @@ public function print(OutputPrinter $outputPrinter)
5959
$color = 'green';
6060
}
6161

62+
$performance = $this->styledPerformance($this->elapsedTime);
6263
$forkNumber = isset($GLOBALS['number_of_process'])
6364
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
6465
: '';
6566
$outputPrinter->print("$forkNumber<fg=$color;options=bold>SRV</>");
6667
$outputPrinter->print(' ');
67-
$outputPrinter->print('(<muted>'.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB</muted>)');
68-
$outputPrinter->print(' - <muted>'.$this->message.'</muted>');
68+
$outputPrinter->print(" $performance {$this->message}</muted>");
6969
$outputPrinter->printLine();
7070
}
7171
}

src/ConsoleStaticMessage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Class ConsoleStaticMessage.
2222
*/
23-
final class ConsoleStaticMessage implements Printable
23+
final class ConsoleStaticMessage extends ConsoleMessage
2424
{
2525
private string $url;
2626
private string $elapsedTime;
@@ -52,12 +52,12 @@ public function print(OutputPrinter $outputPrinter)
5252

5353
$method = str_pad('GET', 6, ' ');
5454

55+
$performance = $this->styledPerformance($this->elapsedTime);
5556
$forkNumber = isset($GLOBALS['number_of_process'])
5657
? "<fg=white>[{$GLOBALS['number_of_process']}] </>"
5758
: '';
5859
$outputPrinter->print("$forkNumber<purple>200</purple>");
59-
$outputPrinter->print(" $method $this->url ");
60-
$outputPrinter->print('(<muted>'.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB</muted>');
60+
$outputPrinter->print(" $performance $method $this->url ");
6161
$outputPrinter->printLine();
6262
}
6363
}

tests/ApplicationStaticFolderTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public function testRegular()
4040
$this->waitForChange($process, $initialOutput);
4141

4242
$this->assertStringContainsString(
43-
'200 GET',
43+
'200',
44+
$process->getOutput()
45+
);
46+
47+
$this->assertStringContainsString(
48+
' GET ',
4449
$process->getOutput()
4550
);
4651

0 commit comments

Comments
 (0)