-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
AnsiConsoleBackend.php
138 lines (123 loc) · 3.74 KB
/
AnsiConsoleBackend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace TYPO3\Flow\Log\Backend;
/*
* This file is part of the TYPO3.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use TYPO3\Flow\Annotations as Flow;
/**
* Extended ANSI console backend with human friendly formatting
*
* @api
*/
class AnsiConsoleBackend extends ConsoleBackend
{
const FG_BLACK = "\033[0;30m";
const FG_WHITE = "\033[1;37m";
const FG_GRAY = "\033[0;37m";
const FG_BLUE = "\033[0;34m";
const FG_CYAN = "\033[0;36m";
const FG_YELLOW = "\033[1;33m";
const FG_RED = "\033[0;31m";
const FG_GREEN = "\033[0;32m";
const BG_CYAN = "\033[46m";
const BG_GREEN = "\033[42m";
const BG_RED = "\033[41m";
const BG_YELLOW = "\033[43m";
const BG_WHITE = "\033[47m";
const END = "\033[0m";
/**
* @var array
*/
protected $tagFormats = array();
/**
* @var boolean
*/
protected $disableAnsi = false;
/**
* Open the log backend
*
* Initializes tag formats.
*
* @return void
*/
public function open()
{
parent::open();
$this->tagFormats = array(
'emergency' => self::FG_BLACK . self::BG_RED . '|' . self::END,
'alert' => self::FG_BLACK . self::BG_YELLOW . '|' . self::END,
'critical' => self::FG_BLACK . self::BG_CYAN . '|' . self::END,
'error' => self::FG_RED . '|' . self::END,
'warning' => self::FG_YELLOW . '|' . self::END,
'notice' => self::FG_WHITE . '|' . self::END,
'info' => self::FG_GREEN . '|' . self::END,
'debug' => self::FG_BLUE . '|' . self::END,
);
}
/**
* Appends the given message along with the additional information into the log.
*
* @param string $message
* @param integer $severity
* @param array $additionalData
* @param string $packageKey
* @param string $className
* @param string $methodName
* @return void
*/
public function append($message, $severity = LOG_INFO, $additionalData = null, $packageKey = null, $className = null, $methodName = null)
{
if ($severity > $this->severityThreshold) {
return;
}
$severityName = strtolower(trim($this->severityLabels[$severity]));
$output = '<' . $severityName. '>' . $message . '</' . $severityName . '>';
$output = $this->formatOutput($output);
if (is_resource($this->streamHandle)) {
fputs($this->streamHandle, $output . PHP_EOL);
}
}
/**
* Apply ansi formatting to output according to tags
*
* @param string $output
* @return string
*/
protected function formatOutput($output)
{
$tagFormats = $this->tagFormats;
$disableAnsi = $this->disableAnsi;
do {
$lastOutput = $output;
$output = preg_replace_callback('|(<([^>]+?)>(.*?)</\2>)|s', function ($matches) use ($tagFormats, $disableAnsi) {
$format = isset($tagFormats[$matches[2]]) ? $tagFormats[$matches[2]] : '|';
if ($disableAnsi) {
return $matches[3];
} else {
return str_replace('|', $matches[3], $format);
}
}, $output);
} while ($lastOutput !== $output);
return $output;
}
/**
* @param boolean $disableAnsi
*/
public function setDisableAnsi($disableAnsi)
{
$this->disableAnsi = $disableAnsi;
}
/**
* @return boolean
*/
public function getDisableAnsi()
{
return $this->disableAnsi;
}
}