Skip to content

Commit

Permalink
Merge pull request #46115 from nextcloud/enh/add-a-universal-debug-op…
Browse files Browse the repository at this point in the history
…tion-to-occ

feat(occ): Add a --debug option to output all log levels to the output
  • Loading branch information
come-nc authored Jul 8, 2024
2 parents fe90956 + 5b9966f commit a434dfb
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
7 changes: 5 additions & 2 deletions console.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ function exceptionHandler($exception) {
$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');

Expand Down
69 changes: 69 additions & 0 deletions core/Listener/BeforeMessageLoggedEventListener.php
Original file line number Diff line number Diff line change
@@ -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(...));
}
}
5 changes: 5 additions & 0 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ public static function init(): void {
self::$composerAutoloader = require_once OC::$SERVERROOT . '/lib/composer/autoload.php';
self::$composerAutoloader->setApcuPrefix(null);


try {
self::initPaths();
// setup 3rdparty autoloader
Expand All @@ -578,6 +579,10 @@ public static function init(): void {
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');
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
'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',
Expand Down Expand Up @@ -1243,6 +1244,7 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions lib/public/Console/ReservedOptions.php
Original file line number Diff line number Diff line change
@@ -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';
}

0 comments on commit a434dfb

Please sign in to comment.