Skip to content

Commit

Permalink
Added formatter for compiler messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Nov 27, 2018
1 parent 86ccc38 commit 9c667de
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 133 deletions.
128 changes: 0 additions & 128 deletions Library/Logger.php

This file was deleted.

135 changes: 135 additions & 0 deletions Library/Logger/Formatter/CompilerFormatter.php
@@ -0,0 +1,135 @@
<?php

/*
* This file is part of the Zephir.
*
* (c) Zephir Team <team@zephir-lang.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zephir\Logger\Formatter;

use Monolog\Formatter\LineFormatter;
use Zephir\Config;

/**
* Formatter for warnings/notices/errors generated in compilation.
*/
final class CompilerFormatter extends LineFormatter
{
const SIMPLE_FORMAT = "%level_name%: %message% in %file% on line %line% %type%\n";

/**
* @var Config
*/
private $config;

/**
* The contents of the files that are involved in the log message.
*
* @var array
*/
private $filesContent = [];

public function __construct(Config $config)
{
parent::__construct();

$this->config = $config;
}

/**
* {@inheritdoc}
*/
public function format(array $record)
{
if ($this->config->get('silent')) {
return '';
}

$vars = parent::normalize($record);

$output = $this->format;

// unused
unset($vars['extra']);
$output = str_replace('%extra%', '', $output);
$output = str_replace('%context%', '', $output);

// ignore empty context or invalid format
if (!empty($vars['context']) &&
\is_array($vars['context']) &&
2 == \count($vars['context'])
) {
$type = $vars['context'][0];
$node = $vars['context'][1];

if (!$this->config->get($type, 'warnings')) {
return '';
}

$vars['type'] = "[{$type}]";

if (!isset($node['file'])) {
$vars['file'] = 'unknown';
$vars['line'] = '0';
} else {
$vars['file'] = $node['file'];
$vars['line'] = $node['line'];
$output .= PHP_EOL;

$lines = $this->getFileContents($node['file']);
if (isset($lines[$node['line'] - 1])) {
$line = $lines[$node['line'] - 1];
$output .= "\t".str_replace("\t", ' ', $line);
if (($node['char'] - 1) > 0) {
$output .= PHP_EOL."\t".str_repeat('-', $node['char'] - 1).'^'.PHP_EOL;
}
}
}

$output = str_replace('%file%', $this->stringify($vars['file']), $output);
$output = str_replace('%line%', $this->stringify($vars['line']), $output);
$output = str_replace('%type%', $this->stringify($vars['type']), $output);
}

unset($vars['context']);

// WARNING -> Warning
$vars['level_name'] = ucfirst(strtolower($vars['level_name']));

foreach ($vars as $var => $val) {
if (false !== strpos($output, '%'.$var.'%')) {
$output = str_replace('%'.$var.'%', $this->stringify($val), $output);
}
}

// remove leftover %extra.xxx% and %context.xxx% if any
if (false !== strpos($output, '%')) {
$output = preg_replace('/%(?:extra|context)\..+?%/', '', $output);
$output = preg_replace('/ %type%\n/', "\n", $output);
$output = preg_replace('/on line %line%/', '', $output);
$output = preg_replace('/ in %file% /', '', $output);
}

return $output;
}

/**
* Gets the contents of the files that are involved in the log message.
*
* @param string $file File path
*
* @return array
*/
private function getFileContents($file)
{
if (!isset($this->filesContent[$file])) {
$this->filesContent[$file] = file_exists($file) ? file($file) : [];
}

return $this->filesContent[$file];
}
}
4 changes: 2 additions & 2 deletions Library/config/config.yml
Expand Up @@ -15,14 +15,14 @@ monolog:
process_psr_3_messages: true
bubble: false
level: WARNING
formatter: compiler.log.formatter
formatter: compiler_log_formatter
console_stdout:
type: stream
path: 'php://stdout'
process_psr_3_messages: true
bubble: false
level: INFO
formatter: compiler.log.formatter
formatter: compiler_log_formatter

oneup_flysystem:
adapters:
Expand Down
5 changes: 2 additions & 3 deletions Library/config/services.yml
Expand Up @@ -54,6 +54,5 @@ services:
arguments:
- '@monolog.logger'

compiler.log.formatter:
class: Monolog\Formatter\LineFormatter
arguments: ["%%level_name%%: %%message%%\n"]
compiler_log_formatter:
class: Zephir\Logger\Formatter\CompilerFormatter
Empty file modified phpunit 100644 → 100755
Empty file.

0 comments on commit 9c667de

Please sign in to comment.