fix(occ): Move debug log listener setup to a static method, add option for level

Also changed option from --debug to --debug-log to avoid conflicts

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
pull/46115/head
Côme Chilliet 5 months ago
parent 2ba0819243
commit 9baf8fea8c
No known key found for this signature in database
GPG Key ID: A3E2F658B28C760A
  1. 7
      console.php
  2. 30
      core/Listener/BeforeMessageLoggedEventListener.php
  3. 11
      lib/base.php

@ -75,13 +75,8 @@ try {
$eventLogger->start('console:build_application', 'Build Application instance and load commands');
$application = \OCP\Server::get(Application::class);
/* base.php will have removed eventual debug options from argv in $_SERVER */
$argv = $_SERVER['argv'];
if (($key = array_search('--debug', $argv)) !== false) {
// Remove --debug option if it was passed
unset($argv[$key]);
$argv = array_values($argv);
}
$input = new ArgvInput($argv);
$application->loadCommands($input, new ConsoleOutput());

@ -10,18 +10,28 @@ declare(strict_types=1);
namespace OC\Core\Listener;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\Log\BeforeMessageLoggedEvent;
use OCP\Server;
/**
* Listen to log calls and output them to STDOUT for debug purposes
* @template-implements IEventListener<BeforeMessageLoggedEvent>
*/
class BeforeMessageLoggedEventListener implements IEventListener {
public function __construct(
private int $level,
) {
}
public function handle(Event $event): void {
if (!$event instanceof BeforeMessageLoggedEvent) {
return;
}
if ($event->getLevel() < $this->level) {
return;
}
echo
match($event->getLevel()) {
0 => '[debug]',
@ -35,4 +45,24 @@ class BeforeMessageLoggedEventListener implements IEventListener {
.$event->getMessage()['message']
."\n";
}
/**
* Register listener to log messages and remove debug options from $_SERVER['argv']
*/
public static function setup(): void {
$eventDispatcher = Server::get(IEventDispatcher::class);
$argv = $_SERVER['argv'];
$level = 0;
foreach ($argv as $key => $arg) {
if ($arg === '--debug-log') {
unset($argv[$key]);
} elseif (str_starts_with($arg, '--debug-log-level=')) {
$level = (int)substr($arg, strlen('--debug-log-level='));
unset($argv[$key]);
}
}
$_SERVER['argv'] = array_values($argv);
$debugLoggerEventListener = new self($level);
$eventDispatcher->addListener(BeforeMessageLoggedEvent::class, $debugLoggerEventListener->handle(...));
}
}

@ -63,7 +63,6 @@ class OC {
* check if Nextcloud runs in cli mode
*/
public static bool $CLI = false;
public static bool $CLI_DEBUG = false;
public static \OC\Autoloader $loader;
@ -557,11 +556,6 @@ class OC {
self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
self::$composerAutoloader->setApcuPrefix(null);
if (self::$CLI && ($key = array_search('--debug', $_SERVER['argv'])) !== false) {
self::$CLI_DEBUG = true;
} else {
self::$CLI_DEBUG = false;
}
try {
self::initPaths();
@ -585,9 +579,8 @@ class OC {
self::$server = new \OC\Server(\OC::$WEBROOT, self::$config);
self::$server->boot();
if (self::$CLI_DEBUG) {
$eventDispatcher = \OCP\Server::get(IEventDispatcher::class);
$eventDispatcher->addServiceListener(\OCP\Log\BeforeMessageLoggedEvent::class, \OC\Core\Listener\BeforeMessageLoggedEventListener::class);
if (self::$CLI && in_array('--debug-log', $_SERVER['argv'])) {
\OC\Core\Listener\BeforeMessageLoggedEventListener::setup();
}
$eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class);

Loading…
Cancel
Save