1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Uses ANSI Color Code Sequences to colorize messages
22
 * sent to the console.
23
 *
24
 * If used with the -logfile option, the output file
25
 * will contain all the necessary escape codes to
26
 * display the text in colorized mode when displayed
27
 * in the console using applications like cat, more,
28
 * etc.
29
 *
30
 * This is designed to work on terminals that support ANSI
31
 * color codes.  It works on XTerm, ETerm, Mindterm, etc.
32
 * It also works on Win9x (with ANSI.SYS loaded.)
33
 *
34
 * NOTE:
35
 * It doesn't work on WinNT's COMMAND.COM even with
36
 * ANSI.SYS loaded.
37
 *
38
 * The default colors used for differentiating
39
 * the message levels can be changed by editing the
40
 * phing/listener/defaults.properties file.
41
 *
42
 * This file contains 5 key/value pairs:
43
 * AnsiColorLogger.ERROR_COLOR=2;31
44
 * AnsiColorLogger.WARNING_COLOR=2;35
45
 * AnsiColorLogger.INFO_COLOR=2;36
46
 * AnsiColorLogger.VERBOSE_COLOR=2;32
47
 * AnsiColorLogger.DEBUG_COLOR=2;34
48
 *
49
 * Another option is to pass a system variable named
50
 * ant.logger.defaults, with value set to the path of
51
 * the file that contains user defined Ansi Color
52
 * Codes, to the <B>java</B> command using -D option.
53
 *
54
 * To change these colors use the following chart:
55
 *
56
 *      <B>ANSI COLOR LOGGER CONFIGURATION</B>
57
 *
58
 * Format for AnsiColorLogger.*=
59
 *  Attribute;Foreground;Background
60
 *
61
 *  Attribute is one of the following:
62
 *  0 -> Reset All Attributes (return to normal mode)
63
 *  1 -> Bright (Usually turns on BOLD)
64
 *  2 -> Dim
65
 *  3 -> Underline
66
 *  5 -> link
67
 *  7 -> Reverse
68
 *  8 -> Hidden
69
 *
70
 *  Foreground is one of the following:
71
 *  30 -> Black
72
 *  31 -> Red
73
 *  32 -> Green
74
 *  33 -> Yellow
75
 *  34 -> Blue
76
 *  35 -> Magenta
77
 *  36 -> Cyan
78
 *  37 -> White
79
 *
80
 *  Background is one of the following:
81
 *  40 -> Black
82
 *  41 -> Red
83
 *  42 -> Green
84
 *  43 -> Yellow
85
 *  44 -> Blue
86
 *  45 -> Magenta
87
 *  46 -> Cyan
88
 *  47 -> White
89
 *
90
 * @author  Hans Lellelid <hans@xmpl.org> (Phing)
91
 * @author  Magesh Umasankar (Ant)
92
 * @package phing.listener
93
 */
94
class AnsiColorLogger extends DefaultLogger
95
{
96
    public const ATTR_NORMAL = 0;
97
    public const ATTR_BRIGHT = 1;
98
    public const ATTR_DIM = 2;
99
    public const ATTR_UNDERLINE = 3;
100
    public const ATTR_BLINK = 5;
101
    public const ATTR_REVERSE = 7;
102
    public const ATTR_HIDDEN = 8;
103

104
    public const FG_BLACK = 30;
105
    public const FG_RED = 31;
106
    public const FG_GREEN = 32;
107
    public const FG_YELLOW = 33;
108
    public const FG_BLUE = 34;
109
    public const FG_MAGENTA = 35;
110
    public const FG_CYAN = 36;
111
    public const FG_WHITE = 37;
112

113
    public const BG_BLACK = 40;
114
    public const BG_RED = 41;
115
    public const BG_GREEN = 42;
116
    public const BG_YELLOW = 44;
117
    public const BG_BLUE = 44;
118
    public const BG_MAGENTA = 45;
119
    public const BG_CYAN = 46;
120
    public const BG_WHITE = 47;
121

122
    public const PREFIX = "\x1b[";
123
    public const SUFFIX = "m";
124
    public const SEPARATOR = ';';
125
    public const END_COLOR = "\x1b[0m"; // self::PREFIX . self::SUFFIX;
126

127
    private $errColor;
128
    private $warnColor;
129
    private $infoColor;
130
    private $verboseColor;
131
    private $debugColor;
132

133
    private $colorsSet = false;
134

135
    /**
136
     * Construct new AnsiColorLogger
137
     * Perform initializations that cannot be done in var declarations.
138
     */
139 0
    public function __construct()
140
    {
141 0
        parent::__construct();
142 0
        $this->errColor = self::PREFIX . self::ATTR_NORMAL . self::SEPARATOR . self::FG_RED . self::SUFFIX;
143 0
        $this->warnColor = self::PREFIX . self::ATTR_NORMAL . self::SEPARATOR . self::FG_MAGENTA . self::SUFFIX;
144 0
        $this->infoColor = self::PREFIX . self::ATTR_NORMAL . self::SEPARATOR . self::FG_CYAN . self::SUFFIX;
145 0
        $this->verboseColor = self::PREFIX . self::ATTR_NORMAL . self::SEPARATOR . self::FG_GREEN . self::SUFFIX;
146 0
        $this->debugColor = self::PREFIX . self::ATTR_NORMAL . self::SEPARATOR . self::FG_BLUE . self::SUFFIX;
147
    }
148

149
    /**
150
     * Set the colors to use from a property file specified by the
151
     * special ant property ant.logger.defaults
152
     */
153 0
    final private function setColors()
154
    {
155 0
        $userColorFile = Phing::getProperty("phing.logger.defaults");
156 0
        $systemColorFile = new PhingFile(Phing::getResourcePath("phing/listener/defaults.properties"));
157

158 0
        $in = null;
159

160
        try {
161 0
            $prop = new Properties();
162

163 0
            if ($userColorFile !== null) {
164 0
                $prop->load($userColorFile);
165
            } else {
166 0
                $prop->load($systemColorFile);
167
            }
168

169 0
            $err = $prop->getProperty("AnsiColorLogger.ERROR_COLOR");
170 0
            $warn = $prop->getProperty("AnsiColorLogger.WARNING_COLOR");
171 0
            $info = $prop->getProperty("AnsiColorLogger.INFO_COLOR");
172 0
            $verbose = $prop->getProperty("AnsiColorLogger.VERBOSE_COLOR");
173 0
            $debug = $prop->getProperty("AnsiColorLogger.DEBUG_COLOR");
174 0
            if ($err !== null) {
175 0
                $this->errColor = self::PREFIX . $err . self::SUFFIX;
176
            }
177 0
            if ($warn !== null) {
178 0
                $this->warnColor = self::PREFIX . $warn . self::SUFFIX;
179
            }
180 0
            if ($info !== null) {
181 0
                $this->infoColor = self::PREFIX . $info . self::SUFFIX;
182
            }
183 0
            if ($verbose !== null) {
184 0
                $this->verboseColor = self::PREFIX . $verbose . self::SUFFIX;
185
            }
186 0
            if ($debug !== null) {
187 0
                $this->debugColor = self::PREFIX . $debug . self::SUFFIX;
188
            }
189 0
        } catch (IOException $ioe) {
190
            //Ignore exception - we will use the defaults.
191
        }
192
    }
193

194
    /**
195
     * @see DefaultLogger#printMessage
196
     * @param string $message
197
     * @param OutputStream $stream
198
     * @param int $priority
199
     */
200 0
    final protected function printMessage($message, OutputStream $stream, $priority)
201
    {
202 0
        if ($message !== null) {
203 0
            if (!$this->colorsSet) {
204 0
                $this->setColors();
205 0
                $this->colorsSet = true;
206
            }
207

208
            switch ($priority) {
209 0
                case Project::MSG_ERR:
210 0
                    $message = $this->errColor . $message . self::END_COLOR;
211 0
                    break;
212 0
                case Project::MSG_WARN:
213 0
                    $message = $this->warnColor . $message . self::END_COLOR;
214 0
                    break;
215 0
                case Project::MSG_INFO:
216 0
                    $message = $this->infoColor . $message . self::END_COLOR;
217 0
                    break;
218 0
                case Project::MSG_VERBOSE:
219 0
                    $message = $this->verboseColor . $message . self::END_COLOR;
220 0
                    break;
221 0
                case Project::MSG_DEBUG:
222 0
                    $message = $this->debugColor . $message . self::END_COLOR;
223 0
                    break;
224
            }
225

226 0
            $stream->write($message . PHP_EOL);
227
        }
228
    }
229
}

Read our documentation on viewing source code .

Loading