diff options
-rw-r--r-- | console.php | 7 | ||||
-rw-r--r-- | core/Listener/BeforeMessageLoggedEventListener.php | 69 | ||||
-rw-r--r-- | lib/base.php | 5 | ||||
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | lib/public/Console/ReservedOptions.php | 24 |
6 files changed, 107 insertions, 2 deletions
diff --git a/console.php b/console.php index 7b067a84a35..4e1acb00c97 100644 --- a/console.php +++ b/console.php @@ -75,13 +75,16 @@ try { $eventLogger->start('console:build_application', 'Build Application instance and load commands'); $application = \OCP\Server::get(Application::class); - $application->loadCommands(new ArgvInput(), new ConsoleOutput()); + /* base.php will have removed eventual debug options from argv in $_SERVER */ + $argv = $_SERVER['argv']; + $input = new ArgvInput($argv); + $application->loadCommands($input, new ConsoleOutput()); $eventLogger->end('console:build_application'); $eventLogger->start('console:run', 'Run the command'); $application->setAutoExit(false); - $exitCode = $application->run(); + $exitCode = $application->run($input); $eventLogger->end('console:run'); diff --git a/core/Listener/BeforeMessageLoggedEventListener.php b/core/Listener/BeforeMessageLoggedEventListener.php new file mode 100644 index 00000000000..ef771ca0a81 --- /dev/null +++ b/core/Listener/BeforeMessageLoggedEventListener.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OC\Core\Listener; + +use OCP\Console\ReservedOptions; +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]', + 1 => '[info]', + 2 => '[warning]', + 3 => '[error]', + 4 => '[fatal]', + default => '['.$event->getLevel().']', + } + .' ['.$event->getApp().'] ' + .$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 === '--'.ReservedOptions::DEBUG_LOG) { + unset($argv[$key]); + } elseif (str_starts_with($arg, '--'.ReservedOptions::DEBUG_LOG_LEVEL.'=')) { + $level = (int)substr($arg, strlen('--'.ReservedOptions::DEBUG_LOG_LEVEL.'=')); + unset($argv[$key]); + } + } + $_SERVER['argv'] = array_values($argv); + $debugLoggerEventListener = new self($level); + $eventDispatcher->addListener(BeforeMessageLoggedEvent::class, $debugLoggerEventListener->handle(...)); + } +} diff --git a/lib/base.php b/lib/base.php index 91ca87426e1..c84a1246288 100644 --- a/lib/base.php +++ b/lib/base.php @@ -556,6 +556,7 @@ class OC { self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php'; self::$composerAutoloader->setApcuPrefix(null); + try { self::initPaths(); // setup 3rdparty autoloader @@ -578,6 +579,10 @@ class OC { self::$server = new \OC\Server(\OC::$WEBROOT, self::$config); self::$server->boot(); + if (self::$CLI && in_array('--'.\OCP\Console\ReservedOptions::DEBUG_LOG, $_SERVER['argv'])) { + \OC\Core\Listener\BeforeMessageLoggedEventListener::setup(); + } + $eventLogger = Server::get(\OCP\Diagnostics\IEventLogger::class); $eventLogger->log('autoloader', 'Autoloader', $loaderStart, $loaderEnd); $eventLogger->start('boot', 'Initialize'); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 958223bf2fc..f0addbcdaa8 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -220,6 +220,7 @@ return array( 'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => $baseDir . '/lib/public/Config/BeforePreferenceSetEvent.php', 'OCP\\Console\\ConsoleEvent' => $baseDir . '/lib/public/Console/ConsoleEvent.php', + 'OCP\\Console\\ReservedOptions' => $baseDir . '/lib/public/Console/ReservedOptions.php', 'OCP\\Constants' => $baseDir . '/lib/public/Constants.php', 'OCP\\Contacts\\ContactsMenu\\IAction' => $baseDir . '/lib/public/Contacts/ContactsMenu/IAction.php', 'OCP\\Contacts\\ContactsMenu\\IActionFactory' => $baseDir . '/lib/public/Contacts/ContactsMenu/IActionFactory.php', @@ -1243,6 +1244,7 @@ return array( 'OC\\Core\\Events\\PasswordResetEvent' => $baseDir . '/core/Events/PasswordResetEvent.php', 'OC\\Core\\Exception\\LoginFlowV2NotFoundException' => $baseDir . '/core/Exception/LoginFlowV2NotFoundException.php', 'OC\\Core\\Exception\\ResetPasswordException' => $baseDir . '/core/Exception/ResetPasswordException.php', + 'OC\\Core\\Listener\\BeforeMessageLoggedEventListener' => $baseDir . '/core/Listener/BeforeMessageLoggedEventListener.php', 'OC\\Core\\Listener\\BeforeTemplateRenderedListener' => $baseDir . '/core/Listener/BeforeTemplateRenderedListener.php', 'OC\\Core\\Middleware\\TwoFactorMiddleware' => $baseDir . '/core/Middleware/TwoFactorMiddleware.php', 'OC\\Core\\Migrations\\Version13000Date20170705121758' => $baseDir . '/core/Migrations/Version13000Date20170705121758.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index df24fac3765..51044d28a46 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -253,6 +253,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceSetEvent.php', 'OCP\\Console\\ConsoleEvent' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEvent.php', + 'OCP\\Console\\ReservedOptions' => __DIR__ . '/../../..' . '/lib/public/Console/ReservedOptions.php', 'OCP\\Constants' => __DIR__ . '/../../..' . '/lib/public/Constants.php', 'OCP\\Contacts\\ContactsMenu\\IAction' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IAction.php', 'OCP\\Contacts\\ContactsMenu\\IActionFactory' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IActionFactory.php', @@ -1276,6 +1277,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Events\\PasswordResetEvent' => __DIR__ . '/../../..' . '/core/Events/PasswordResetEvent.php', 'OC\\Core\\Exception\\LoginFlowV2NotFoundException' => __DIR__ . '/../../..' . '/core/Exception/LoginFlowV2NotFoundException.php', 'OC\\Core\\Exception\\ResetPasswordException' => __DIR__ . '/../../..' . '/core/Exception/ResetPasswordException.php', + 'OC\\Core\\Listener\\BeforeMessageLoggedEventListener' => __DIR__ . '/../../..' . '/core/Listener/BeforeMessageLoggedEventListener.php', 'OC\\Core\\Listener\\BeforeTemplateRenderedListener' => __DIR__ . '/../../..' . '/core/Listener/BeforeTemplateRenderedListener.php', 'OC\\Core\\Middleware\\TwoFactorMiddleware' => __DIR__ . '/../../..' . '/core/Middleware/TwoFactorMiddleware.php', 'OC\\Core\\Migrations\\Version13000Date20170705121758' => __DIR__ . '/../../..' . '/core/Migrations/Version13000Date20170705121758.php', diff --git a/lib/public/Console/ReservedOptions.php b/lib/public/Console/ReservedOptions.php new file mode 100644 index 00000000000..aff7a7daa3a --- /dev/null +++ b/lib/public/Console/ReservedOptions.php @@ -0,0 +1,24 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\Console; + +/** + * @since 30.0.0 + */ +final class ReservedOptions { + /** + * @since 30.0.0 + */ + public const DEBUG_LOG = 'debug-log'; + /** + * @since 30.0.0 + */ + public const DEBUG_LOG_LEVEL = 'debug-log-level'; +} |