1 | /*
|
---|
2 | * Copyright 2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.listener;
|
---|
18 |
|
---|
19 | import java.io.FileInputStream;
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.InputStream;
|
---|
22 | import java.io.PrintStream;
|
---|
23 | import java.util.Properties;
|
---|
24 | import org.apache.tools.ant.DefaultLogger;
|
---|
25 | import org.apache.tools.ant.Project;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Uses ANSI Color Code Sequences to colorize messages
|
---|
29 | * sent to the console.
|
---|
30 | *
|
---|
31 | * If used with the -logfile option, the output file
|
---|
32 | * will contain all the necessary escape codes to
|
---|
33 | * display the text in colorized mode when displayed
|
---|
34 | * in the console using applications like cat, more,
|
---|
35 | * etc.
|
---|
36 | *
|
---|
37 | * This is designed to work on terminals that support ANSI
|
---|
38 | * color codes. It works on XTerm, ETerm, Mindterm, etc.
|
---|
39 | * It also works on Win9x (with ANSI.SYS loaded.)
|
---|
40 | *
|
---|
41 | * NOTE:
|
---|
42 | * It doesn't work on WinNT's COMMAND.COM even with
|
---|
43 | * ANSI.SYS loaded.
|
---|
44 | *
|
---|
45 | * The default colors used for differentiating
|
---|
46 | * the message levels can be changed by editing the
|
---|
47 | * /org/apache/tools/ant/listener/defaults.properties
|
---|
48 | * file.
|
---|
49 | * This file contains 5 key/value pairs:
|
---|
50 | * AnsiColorLogger.ERROR_COLOR=2;31
|
---|
51 | * AnsiColorLogger.WARNING_COLOR=2;35
|
---|
52 | * AnsiColorLogger.INFO_COLOR=2;36
|
---|
53 | * AnsiColorLogger.VERBOSE_COLOR=2;32
|
---|
54 | * AnsiColorLogger.DEBUG_COLOR=2;34
|
---|
55 | *
|
---|
56 | * Another option is to pass a system variable named
|
---|
57 | * ant.logger.defaults, with value set to the path of
|
---|
58 | * the file that contains user defined Ansi Color
|
---|
59 | * Codes, to the <B>java</B> command using -D option.
|
---|
60 | *
|
---|
61 | * To change these colors use the following chart:
|
---|
62 | *
|
---|
63 | * <B>ANSI COLOR LOGGER CONFIGURATION</B>
|
---|
64 | *
|
---|
65 | * Format for AnsiColorLogger.*=
|
---|
66 | * Attribute;Foreground;Background
|
---|
67 | *
|
---|
68 | * Attribute is one of the following:
|
---|
69 | * 0 -> Reset All Attributes (return to normal mode)
|
---|
70 | * 1 -> Bright (Usually turns on BOLD)
|
---|
71 | * 2 -> Dim
|
---|
72 | * 3 -> Underline
|
---|
73 | * 5 -> link
|
---|
74 | * 7 -> Reverse
|
---|
75 | * 8 -> Hidden
|
---|
76 | *
|
---|
77 | * Foreground is one of the following:
|
---|
78 | * 30 -> Black
|
---|
79 | * 31 -> Red
|
---|
80 | * 32 -> Green
|
---|
81 | * 33 -> Yellow
|
---|
82 | * 34 -> Blue
|
---|
83 | * 35 -> Magenta
|
---|
84 | * 36 -> Cyan
|
---|
85 | * 37 -> White
|
---|
86 | *
|
---|
87 | * Background is one of the following:
|
---|
88 | * 40 -> Black
|
---|
89 | * 41 -> Red
|
---|
90 | * 42 -> Green
|
---|
91 | * 43 -> Yellow
|
---|
92 | * 44 -> Blue
|
---|
93 | * 45 -> Magenta
|
---|
94 | * 46 -> Cyan
|
---|
95 | * 47 -> White
|
---|
96 | *
|
---|
97 | */
|
---|
98 | public final class AnsiColorLogger extends DefaultLogger {
|
---|
99 | // private static final int ATTR_NORMAL = 0;
|
---|
100 | // private static final int ATTR_BRIGHT = 1;
|
---|
101 | private static final int ATTR_DIM = 2;
|
---|
102 | // private static final int ATTR_UNDERLINE = 3;
|
---|
103 | // private static final int ATTR_BLINK = 5;
|
---|
104 | // private static final int ATTR_REVERSE = 7;
|
---|
105 | // private static final int ATTR_HIDDEN = 8;
|
---|
106 |
|
---|
107 | // private static final int FG_BLACK = 30;
|
---|
108 | private static final int FG_RED = 31;
|
---|
109 | private static final int FG_GREEN = 32;
|
---|
110 | // private static final int FG_YELLOW = 33;
|
---|
111 | private static final int FG_BLUE = 34;
|
---|
112 | private static final int FG_MAGENTA = 35;
|
---|
113 | private static final int FG_CYAN = 36;
|
---|
114 | // private static final int FG_WHITE = 37;
|
---|
115 |
|
---|
116 | // private static final int BG_BLACK = 40;
|
---|
117 | // private static final int BG_RED = 41;
|
---|
118 | // private static final int BG_GREEN = 42;
|
---|
119 | // private static final int BG_YELLOW = 44;
|
---|
120 | // private static final int BG_BLUE = 44;
|
---|
121 | // private static final int BG_MAGENTA = 45;
|
---|
122 | // private static final int BG_CYAN = 46;
|
---|
123 | // private static final int BG_WHITE = 47;
|
---|
124 |
|
---|
125 | private static final String PREFIX = "\u001b[";
|
---|
126 | private static final String SUFFIX = "m";
|
---|
127 | private static final char SEPARATOR = ';';
|
---|
128 | private static final String END_COLOR = PREFIX + SUFFIX;
|
---|
129 |
|
---|
130 | private String errColor
|
---|
131 | = PREFIX + ATTR_DIM + SEPARATOR + FG_RED + SUFFIX;
|
---|
132 | private String warnColor
|
---|
133 | = PREFIX + ATTR_DIM + SEPARATOR + FG_MAGENTA + SUFFIX;
|
---|
134 | private String infoColor
|
---|
135 | = PREFIX + ATTR_DIM + SEPARATOR + FG_CYAN + SUFFIX;
|
---|
136 | private String verboseColor
|
---|
137 | = PREFIX + ATTR_DIM + SEPARATOR + FG_GREEN + SUFFIX;
|
---|
138 | private String debugColor
|
---|
139 | = PREFIX + ATTR_DIM + SEPARATOR + FG_BLUE + SUFFIX;
|
---|
140 |
|
---|
141 | private boolean colorsSet = false;
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Set the colors to use from a property file specified by the
|
---|
145 | * special ant property ant.logger.defaults
|
---|
146 | */
|
---|
147 | private final void setColors() {
|
---|
148 | String userColorFile = System.getProperty("ant.logger.defaults");
|
---|
149 | String systemColorFile =
|
---|
150 | "/org/apache/tools/ant/listener/defaults.properties";
|
---|
151 |
|
---|
152 | InputStream in = null;
|
---|
153 |
|
---|
154 | try {
|
---|
155 | Properties prop = new Properties();
|
---|
156 |
|
---|
157 | if (userColorFile != null) {
|
---|
158 | in = new FileInputStream(userColorFile);
|
---|
159 | } else {
|
---|
160 | in = getClass().getResourceAsStream(systemColorFile);
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (in != null) {
|
---|
164 | prop.load(in);
|
---|
165 | }
|
---|
166 |
|
---|
167 | String err = prop.getProperty("AnsiColorLogger.ERROR_COLOR");
|
---|
168 | String warn = prop.getProperty("AnsiColorLogger.WARNING_COLOR");
|
---|
169 | String info = prop.getProperty("AnsiColorLogger.INFO_COLOR");
|
---|
170 | String verbose = prop.getProperty("AnsiColorLogger.VERBOSE_COLOR");
|
---|
171 | String debug = prop.getProperty("AnsiColorLogger.DEBUG_COLOR");
|
---|
172 | if (err != null) {
|
---|
173 | errColor = PREFIX + err + SUFFIX;
|
---|
174 | }
|
---|
175 | if (warn != null) {
|
---|
176 | warnColor = PREFIX + warn + SUFFIX;
|
---|
177 | }
|
---|
178 | if (info != null) {
|
---|
179 | infoColor = PREFIX + info + SUFFIX;
|
---|
180 | }
|
---|
181 | if (verbose != null) {
|
---|
182 | verboseColor = PREFIX + verbose + SUFFIX;
|
---|
183 | }
|
---|
184 | if (debug != null) {
|
---|
185 | debugColor = PREFIX + debug + SUFFIX;
|
---|
186 | }
|
---|
187 | } catch (IOException ioe) {
|
---|
188 | //Ignore - we will use the defaults.
|
---|
189 | } finally {
|
---|
190 | if (in != null) {
|
---|
191 | try {
|
---|
192 | in.close();
|
---|
193 | } catch (IOException e) {
|
---|
194 | //Ignore - We do not want this to stop the build.
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * @see DefaultLogger#printMessage
|
---|
202 | */
|
---|
203 | protected final void printMessage(final String message,
|
---|
204 | final PrintStream stream,
|
---|
205 | final int priority) {
|
---|
206 | if (message != null && stream != null) {
|
---|
207 | if (!colorsSet) {
|
---|
208 | setColors();
|
---|
209 | colorsSet = true;
|
---|
210 | }
|
---|
211 |
|
---|
212 | final StringBuffer msg = new StringBuffer(message);
|
---|
213 | switch (priority) {
|
---|
214 | case Project.MSG_ERR:
|
---|
215 | msg.insert(0, errColor);
|
---|
216 | msg.append(END_COLOR);
|
---|
217 | break;
|
---|
218 | case Project.MSG_WARN:
|
---|
219 | msg.insert(0, warnColor);
|
---|
220 | msg.append(END_COLOR);
|
---|
221 | break;
|
---|
222 | case Project.MSG_INFO:
|
---|
223 | msg.insert(0, infoColor);
|
---|
224 | msg.append(END_COLOR);
|
---|
225 | break;
|
---|
226 | case Project.MSG_VERBOSE:
|
---|
227 | msg.insert(0, verboseColor);
|
---|
228 | msg.append(END_COLOR);
|
---|
229 | break;
|
---|
230 | case Project.MSG_DEBUG:
|
---|
231 | msg.insert(0, debugColor);
|
---|
232 | msg.append(END_COLOR);
|
---|
233 | break;
|
---|
234 | }
|
---|
235 | final String strmessage = msg.toString();
|
---|
236 | stream.println(strmessage);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|