Loading src/Controller/SyslogWatcherController.php +14 −14 Original line number Diff line number Diff line Loading @@ -6,12 +6,12 @@ namespace Drupal\syslog_watcher\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Form\FormInterface; use Drupal\Core\Url; use Drupal\syslog_watcher\Services\LineFormatterInterface; use Drupal\syslog_watcher\Services\LineParserInterface; use Drupal\syslog_watcher\Services\PagerHelperInterface; use Drupal\syslog_watcher\SyslogFormatEntry; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Url; /** * Returns responses for Syslog Watcher routes. Loading Loading @@ -104,15 +104,15 @@ class SyslogWatcherController extends ControllerBase { return $build; } if (is_string($file_path) && is_readable($file_path)) { if (\is_string($file_path) && \is_readable($file_path)) { $file = new \SplFileObject($file_path); // Go to the end of the file to get the number of lines. $file->seek(PHP_INT_MAX - 1); $file->seek(\PHP_INT_MAX - 1); $lines_number = $file->key(); // Initialize the pager. list($start_line, $end_line) = $this->pagerHelper->initPager($lines_number); [$start_line, $end_line] = $this->pagerHelper->initPager($lines_number); if (!isset($start_line) || !isset($end_line)) { $this->messenger() ->addError($this->t('An error occurred during the pager initialization.')); Loading @@ -127,21 +127,21 @@ class SyslogWatcherController extends ControllerBase { $this->t('This file has %lines_number lines.', ['%lines_number' => $lines_number]), ]; for ($line = $start_line; $line >= $end_line; $line--) { for ($line = $start_line; $line >= $end_line; --$line) { // Parse the line $line. $file->seek($line); $parsed_line = []; if (is_string($file->current()) && !empty($file->current())) { if (\is_string($file->current()) && !empty($file->current())) { $parsed_line = $this->lineParser->parse($file->current()); } if (is_array($parsed_line) && !empty($parsed_line)) { if (\is_array($parsed_line) && !empty($parsed_line)) { // It could be weird for a user to deal with a line number of 0 // so we increment it. $human_readable_number_line = $file->key() + 1; // Format message: truncate and add link to see detail page. $message = $this->lineFormatter->formatData(SyslogFormatEntry::MESSAGE, $parsed_line); if (is_string($message) && !empty($message)) { if (\is_string($message) && !empty($message)) { $message = $this->lineFormatter->formatLinkedMessage($message, $human_readable_number_line); } Loading Loading @@ -231,7 +231,7 @@ class SyslogWatcherController extends ControllerBase { // This number corresponds to the human readable one // so we need to decrement it to deal with the line number // used for SplFileObject. $line_number--; --$line_number; // Initialize variables. $rows = []; $build = []; Loading @@ -250,7 +250,7 @@ class SyslogWatcherController extends ControllerBase { $syslog_format_array = SyslogFormatEntry::getFormats(); if (is_string($file_path) && is_readable($file_path)) { if (\is_string($file_path) && \is_readable($file_path)) { $information_message = [ $this->t('You are reading the line :line_number of the log file :file_path.', [ ':line_number' => ($line_number + 1), Loading @@ -265,12 +265,12 @@ class SyslogWatcherController extends ControllerBase { // Parse the line $line_number. $parsed_line = ''; if (is_string($file->current()) && !empty($file->current())) { if (\is_string($file->current()) && !empty($file->current())) { $parsed_line = $this->lineParser->parse($file->current()); } // Add line information as a row. if (is_array($parsed_line) && !empty($parsed_line)) { if (\is_array($parsed_line) && !empty($parsed_line)) { foreach ($syslog_format_array as $syslog_format => $syslog_label) { if (isset($parsed_line[$syslog_format])) { $rows[] = [ Loading Loading @@ -330,7 +330,7 @@ class SyslogWatcherController extends ControllerBase { protected function buildInformationMessage(array &$build, array $messages): void { $build['information_message'] = [ '#type' => 'container', '#weight' => -10, '#weight' => (int) -10, ]; foreach ($messages as $message) { $build['information_message'][] = [ Loading src/Form/SyslogWatcherPagerForm.php +17 −3 Original line number Diff line number Diff line Loading @@ -15,6 +15,21 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class SyslogWatcherPagerForm extends FormBase { /** * Default value for the number of lines per page. */ public const LINES_PER_PAGE_DEFAULT_VALUE = 50; /** * Default options for the number of lines per page. */ public const LINES_PER_PAGE_DEFAULT_OPTIONS = [ 5, 10, 25, 50, ]; /** * The pager helper. * Loading Loading @@ -52,7 +67,6 @@ class SyslogWatcherPagerForm extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['pager'] = [ '#type' => 'details', '#title' => $this->t('Pager'), Loading @@ -65,7 +79,7 @@ class SyslogWatcherPagerForm extends FormBase { $form['pager']['lines_per_page'] = [ '#type' => 'select', '#title' => $this->t('Number of lines per page to display'), '#options' => array_combine($options, $options), '#options' => \array_combine($options, $options), '#default_value' => $this->pagerHelper->getLinesPerPage(), ]; Loading src/Services/LineFormatter.php +3 −4 Original line number Diff line number Diff line Loading @@ -80,11 +80,11 @@ class LineFormatter implements LineFormatterInterface { * {@inheritdoc} */ public function formatLinkedMessage($message, $line_number = NULL) { $message_title = Unicode::truncate(Html::decodeEntities(strip_tags($message)), 256, TRUE, TRUE); $message_log_text = Unicode::truncate($message_title, 56, TRUE, TRUE); $message_title = Unicode::truncate(Html::decodeEntities(\strip_tags($message)), self::TITLE_MAX_LENGTH, TRUE, TRUE); $message_log_text = Unicode::truncate($message_title, self::TEXT_MAX_LENGTH, TRUE, TRUE); // The link generator will escape any unsafe HTML entities in the // final text. $message = new Link($message_log_text, new Url('syslog_watcher.line', ['line_number' => $line_number], [ return new Link($message_log_text, new Url('syslog_watcher.line', ['line_number' => $line_number], [ 'attributes' => [ // Provide a title for the link for useful hover hints. The // Attribute object will escape any unsafe HTML entities in the Loading @@ -92,7 +92,6 @@ class LineFormatter implements LineFormatterInterface { 'title' => $message_title, ], ])); return $message; } } src/Services/LineFormatterInterface.php +10 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,16 @@ namespace Drupal\syslog_watcher\Services; */ interface LineFormatterInterface { /** * Title max length. */ public const TITLE_MAX_LENGTH = 256; /** * Text max length. */ public const TEXT_MAX_LENGTH = 56; /** * Format a data according to its format entry. * Loading src/Services/LineParser.php +10 −11 Original line number Diff line number Diff line Loading @@ -38,33 +38,32 @@ class LineParser implements LineParserInterface { public function parse($line) { // We assume every log entry starts with "M j H:i:s hostname identity: " // so we remove this part before exploding the remainder. $fake_beginning = date('M j H:i:s') . ' ' . gethostname() . ' drupal: '; $fake_beginning_length = strlen($fake_beginning); $match = substr($line, 0, $fake_beginning_length); $fake_beginning = \date('M j H:i:s') . ' ' . \gethostname() . ' drupal: '; $fake_beginning_length = \strlen($fake_beginning); $match = \substr($line, 0, $fake_beginning_length); /** @var string $new_line */ $new_line = str_replace($match, '', $line); $new_line = \str_replace($match, '', $line); /** @var string $syslog_separator */ // Explode the line to work with it latter. /** @var non-empty-string $syslog_separator */ $syslog_separator = $this->configFactory->get('syslog_watcher.settings') ->get('separator'); $exploded_line = explode($syslog_separator, $new_line); $exploded_line = \explode($syslog_separator, $new_line); /** @var string $syslog_format */ // Create an array with format defined by the configuration form. $syslog_format = $this->configFactory->get('syslog.settings')->get('format'); /** @var array $syslog_format_array */ $syslog_format_array = explode($syslog_separator, $syslog_format); $syslog_format_array = \explode($syslog_separator, $syslog_format); if (!empty($exploded_line) && count($syslog_format_array) == count($exploded_line)) { if (\count($syslog_format_array) == \count($exploded_line)) { // Replace keys with format label. $result = array_combine($syslog_format_array, $exploded_line); $result = \array_combine($syslog_format_array, $exploded_line); } else { // There is an issue with the line, display the truncated raw line. $result = $syslog_format_array; $result[SyslogFormatEntry::MESSAGE] = Unicode::truncate(Html::decodeEntities(strip_tags(Xss::filterAdmin($line))), 256, TRUE, TRUE); $result[SyslogFormatEntry::MESSAGE] = Unicode::truncate(Html::decodeEntities(\strip_tags(Xss::filterAdmin($line))), self::LINE_MAX_LENGTH, TRUE, TRUE); } return $result; Loading Loading
src/Controller/SyslogWatcherController.php +14 −14 Original line number Diff line number Diff line Loading @@ -6,12 +6,12 @@ namespace Drupal\syslog_watcher\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Form\FormInterface; use Drupal\Core\Url; use Drupal\syslog_watcher\Services\LineFormatterInterface; use Drupal\syslog_watcher\Services\LineParserInterface; use Drupal\syslog_watcher\Services\PagerHelperInterface; use Drupal\syslog_watcher\SyslogFormatEntry; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Url; /** * Returns responses for Syslog Watcher routes. Loading Loading @@ -104,15 +104,15 @@ class SyslogWatcherController extends ControllerBase { return $build; } if (is_string($file_path) && is_readable($file_path)) { if (\is_string($file_path) && \is_readable($file_path)) { $file = new \SplFileObject($file_path); // Go to the end of the file to get the number of lines. $file->seek(PHP_INT_MAX - 1); $file->seek(\PHP_INT_MAX - 1); $lines_number = $file->key(); // Initialize the pager. list($start_line, $end_line) = $this->pagerHelper->initPager($lines_number); [$start_line, $end_line] = $this->pagerHelper->initPager($lines_number); if (!isset($start_line) || !isset($end_line)) { $this->messenger() ->addError($this->t('An error occurred during the pager initialization.')); Loading @@ -127,21 +127,21 @@ class SyslogWatcherController extends ControllerBase { $this->t('This file has %lines_number lines.', ['%lines_number' => $lines_number]), ]; for ($line = $start_line; $line >= $end_line; $line--) { for ($line = $start_line; $line >= $end_line; --$line) { // Parse the line $line. $file->seek($line); $parsed_line = []; if (is_string($file->current()) && !empty($file->current())) { if (\is_string($file->current()) && !empty($file->current())) { $parsed_line = $this->lineParser->parse($file->current()); } if (is_array($parsed_line) && !empty($parsed_line)) { if (\is_array($parsed_line) && !empty($parsed_line)) { // It could be weird for a user to deal with a line number of 0 // so we increment it. $human_readable_number_line = $file->key() + 1; // Format message: truncate and add link to see detail page. $message = $this->lineFormatter->formatData(SyslogFormatEntry::MESSAGE, $parsed_line); if (is_string($message) && !empty($message)) { if (\is_string($message) && !empty($message)) { $message = $this->lineFormatter->formatLinkedMessage($message, $human_readable_number_line); } Loading Loading @@ -231,7 +231,7 @@ class SyslogWatcherController extends ControllerBase { // This number corresponds to the human readable one // so we need to decrement it to deal with the line number // used for SplFileObject. $line_number--; --$line_number; // Initialize variables. $rows = []; $build = []; Loading @@ -250,7 +250,7 @@ class SyslogWatcherController extends ControllerBase { $syslog_format_array = SyslogFormatEntry::getFormats(); if (is_string($file_path) && is_readable($file_path)) { if (\is_string($file_path) && \is_readable($file_path)) { $information_message = [ $this->t('You are reading the line :line_number of the log file :file_path.', [ ':line_number' => ($line_number + 1), Loading @@ -265,12 +265,12 @@ class SyslogWatcherController extends ControllerBase { // Parse the line $line_number. $parsed_line = ''; if (is_string($file->current()) && !empty($file->current())) { if (\is_string($file->current()) && !empty($file->current())) { $parsed_line = $this->lineParser->parse($file->current()); } // Add line information as a row. if (is_array($parsed_line) && !empty($parsed_line)) { if (\is_array($parsed_line) && !empty($parsed_line)) { foreach ($syslog_format_array as $syslog_format => $syslog_label) { if (isset($parsed_line[$syslog_format])) { $rows[] = [ Loading Loading @@ -330,7 +330,7 @@ class SyslogWatcherController extends ControllerBase { protected function buildInformationMessage(array &$build, array $messages): void { $build['information_message'] = [ '#type' => 'container', '#weight' => -10, '#weight' => (int) -10, ]; foreach ($messages as $message) { $build['information_message'][] = [ Loading
src/Form/SyslogWatcherPagerForm.php +17 −3 Original line number Diff line number Diff line Loading @@ -15,6 +15,21 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class SyslogWatcherPagerForm extends FormBase { /** * Default value for the number of lines per page. */ public const LINES_PER_PAGE_DEFAULT_VALUE = 50; /** * Default options for the number of lines per page. */ public const LINES_PER_PAGE_DEFAULT_OPTIONS = [ 5, 10, 25, 50, ]; /** * The pager helper. * Loading Loading @@ -52,7 +67,6 @@ class SyslogWatcherPagerForm extends FormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['pager'] = [ '#type' => 'details', '#title' => $this->t('Pager'), Loading @@ -65,7 +79,7 @@ class SyslogWatcherPagerForm extends FormBase { $form['pager']['lines_per_page'] = [ '#type' => 'select', '#title' => $this->t('Number of lines per page to display'), '#options' => array_combine($options, $options), '#options' => \array_combine($options, $options), '#default_value' => $this->pagerHelper->getLinesPerPage(), ]; Loading
src/Services/LineFormatter.php +3 −4 Original line number Diff line number Diff line Loading @@ -80,11 +80,11 @@ class LineFormatter implements LineFormatterInterface { * {@inheritdoc} */ public function formatLinkedMessage($message, $line_number = NULL) { $message_title = Unicode::truncate(Html::decodeEntities(strip_tags($message)), 256, TRUE, TRUE); $message_log_text = Unicode::truncate($message_title, 56, TRUE, TRUE); $message_title = Unicode::truncate(Html::decodeEntities(\strip_tags($message)), self::TITLE_MAX_LENGTH, TRUE, TRUE); $message_log_text = Unicode::truncate($message_title, self::TEXT_MAX_LENGTH, TRUE, TRUE); // The link generator will escape any unsafe HTML entities in the // final text. $message = new Link($message_log_text, new Url('syslog_watcher.line', ['line_number' => $line_number], [ return new Link($message_log_text, new Url('syslog_watcher.line', ['line_number' => $line_number], [ 'attributes' => [ // Provide a title for the link for useful hover hints. The // Attribute object will escape any unsafe HTML entities in the Loading @@ -92,7 +92,6 @@ class LineFormatter implements LineFormatterInterface { 'title' => $message_title, ], ])); return $message; } }
src/Services/LineFormatterInterface.php +10 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,16 @@ namespace Drupal\syslog_watcher\Services; */ interface LineFormatterInterface { /** * Title max length. */ public const TITLE_MAX_LENGTH = 256; /** * Text max length. */ public const TEXT_MAX_LENGTH = 56; /** * Format a data according to its format entry. * Loading
src/Services/LineParser.php +10 −11 Original line number Diff line number Diff line Loading @@ -38,33 +38,32 @@ class LineParser implements LineParserInterface { public function parse($line) { // We assume every log entry starts with "M j H:i:s hostname identity: " // so we remove this part before exploding the remainder. $fake_beginning = date('M j H:i:s') . ' ' . gethostname() . ' drupal: '; $fake_beginning_length = strlen($fake_beginning); $match = substr($line, 0, $fake_beginning_length); $fake_beginning = \date('M j H:i:s') . ' ' . \gethostname() . ' drupal: '; $fake_beginning_length = \strlen($fake_beginning); $match = \substr($line, 0, $fake_beginning_length); /** @var string $new_line */ $new_line = str_replace($match, '', $line); $new_line = \str_replace($match, '', $line); /** @var string $syslog_separator */ // Explode the line to work with it latter. /** @var non-empty-string $syslog_separator */ $syslog_separator = $this->configFactory->get('syslog_watcher.settings') ->get('separator'); $exploded_line = explode($syslog_separator, $new_line); $exploded_line = \explode($syslog_separator, $new_line); /** @var string $syslog_format */ // Create an array with format defined by the configuration form. $syslog_format = $this->configFactory->get('syslog.settings')->get('format'); /** @var array $syslog_format_array */ $syslog_format_array = explode($syslog_separator, $syslog_format); $syslog_format_array = \explode($syslog_separator, $syslog_format); if (!empty($exploded_line) && count($syslog_format_array) == count($exploded_line)) { if (\count($syslog_format_array) == \count($exploded_line)) { // Replace keys with format label. $result = array_combine($syslog_format_array, $exploded_line); $result = \array_combine($syslog_format_array, $exploded_line); } else { // There is an issue with the line, display the truncated raw line. $result = $syslog_format_array; $result[SyslogFormatEntry::MESSAGE] = Unicode::truncate(Html::decodeEntities(strip_tags(Xss::filterAdmin($line))), 256, TRUE, TRUE); $result[SyslogFormatEntry::MESSAGE] = Unicode::truncate(Html::decodeEntities(\strip_tags(Xss::filterAdmin($line))), self::LINE_MAX_LENGTH, TRUE, TRUE); } return $result; Loading