1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-19 19:21:10 +01:00
phorge-phorge/src/applications/daemon/management/PhabricatorDaemonManagementLogWorkflow.php
epriestley 340b40172f Tweak --limit for bin/phd log
Summary:
Ref T5405.

  - `--limit` wasn't actually used anywhere.
  - Make it mean "the N newest lines".

Test Plan: Ran `bin/phd log`, `bin/phd log --limit 3`.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T5405

Differential Revision: https://secure.phabricator.com/D10385
2014-08-29 10:02:19 -07:00

102 lines
2.7 KiB
PHP

<?php
final class PhabricatorDaemonManagementLogWorkflow
extends PhabricatorDaemonManagementWorkflow {
public function didConstruct() {
$this
->setName('log')
->setExamples('**log** [__options__]')
->setSynopsis(
pht(
'Print the logs for all daemons, or some daemon(s) identified by '.
'ID. You can get the ID for a daemon from the Daemon Console in '.
'the web interface.'))
->setArguments(
array(
array(
'name' => 'id',
'param' => 'id',
'help' => 'Show logs for daemon(s) with given ID(s).',
'repeat' => true,
),
array(
'name' => 'limit',
'param' => 'N',
'default' => 100,
'help' => 'Show a specific number of log messages '.
'(default 100).',
),
));
}
public function execute(PhutilArgumentParser $args) {
$query = id(new PhabricatorDaemonLogQuery())
->setViewer($this->getViewer())
->setAllowStatusWrites(true);
$ids = $args->getArg('id');
if ($ids) {
$query->withIDs($ids);
}
$daemons = $query->execute();
if (!$daemons) {
if ($ids) {
throw new PhutilArgumentUsageException(
pht('No daemon(s) with id(s) "%s" exist!', implode(', ', $ids)));
} else {
throw new PhutilArgumentUsageException(
pht('No daemons are running.'));
}
}
$console = PhutilConsole::getConsole();
$limit = $args->getArg('limit');
$logs = id(new PhabricatorDaemonLogEvent())->loadAllWhere(
'logID IN (%Ld) ORDER BY id DESC LIMIT %d',
mpull($daemons, 'getID'),
$limit);
$logs = array_reverse($logs);
$lines = array();
foreach ($logs as $log) {
$text_lines = phutil_split_lines($log->getMessage(), $retain = false);
foreach ($text_lines as $line) {
$lines[] = array(
'id' => $log->getLogID(),
'type' => $log->getLogType(),
'date' => $log->getEpoch(),
'data' => $line,
);
}
}
// Each log message may be several lines. Limit the number of lines we
// output so that `--limit 123` means "show 123 lines", which is the most
// easily understandable behavior.
$lines = array_slice($lines, -$limit);
foreach ($lines as $line) {
$id = $line['id'];
$type = $line['type'];
$data = $line['data'];
$date = date('r', $line['date']);
$console->writeOut(
"%s\n",
sprintf(
'Daemon %d %s [%s] %s',
$id,
$type,
$date,
$data));
}
return 0;
}
}