KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > listener > AnsiColorLogger


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

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

99 public final class AnsiColorLogger extends DefaultLogger {
100     // private static final int ATTR_NORMAL = 0;
101
// private static final int ATTR_BRIGHT = 1;
102
private static final int ATTR_DIM = 2;
103     // private static final int ATTR_UNDERLINE = 3;
104
// private static final int ATTR_BLINK = 5;
105
// private static final int ATTR_REVERSE = 7;
106
// private static final int ATTR_HIDDEN = 8;
107

108     // private static final int FG_BLACK = 30;
109
private static final int FG_RED = 31;
110     private static final int FG_GREEN = 32;
111     // private static final int FG_YELLOW = 33;
112
private static final int FG_BLUE = 34;
113     private static final int FG_MAGENTA = 35;
114     private static final int FG_CYAN = 36;
115     // private static final int FG_WHITE = 37;
116

117     // private static final int BG_BLACK = 40;
118
// private static final int BG_RED = 41;
119
// private static final int BG_GREEN = 42;
120
// private static final int BG_YELLOW = 44;
121
// private static final int BG_BLUE = 44;
122
// private static final int BG_MAGENTA = 45;
123
// private static final int BG_CYAN = 46;
124
// private static final int BG_WHITE = 47;
125

126     private static final String JavaDoc PREFIX = "\u001b[";
127     private static final String JavaDoc SUFFIX = "m";
128     private static final char SEPARATOR = ';';
129     private static final String JavaDoc END_COLOR = PREFIX + SUFFIX;
130
131     private String JavaDoc errColor
132         = PREFIX + ATTR_DIM + SEPARATOR + FG_RED + SUFFIX;
133     private String JavaDoc warnColor
134         = PREFIX + ATTR_DIM + SEPARATOR + FG_MAGENTA + SUFFIX;
135     private String JavaDoc infoColor
136         = PREFIX + ATTR_DIM + SEPARATOR + FG_CYAN + SUFFIX;
137     private String JavaDoc verboseColor
138         = PREFIX + ATTR_DIM + SEPARATOR + FG_GREEN + SUFFIX;
139     private String JavaDoc debugColor
140         = PREFIX + ATTR_DIM + SEPARATOR + FG_BLUE + SUFFIX;
141
142     private boolean colorsSet = false;
143
144     /**
145      * Set the colors to use from a property file specified by the
146      * special ant property ant.logger.defaults
147      */

148     private void setColors() {
149         String JavaDoc userColorFile = System.getProperty("ant.logger.defaults");
150         String JavaDoc systemColorFile =
151             "/org/apache/tools/ant/listener/defaults.properties";
152
153         InputStream JavaDoc in = null;
154
155         try {
156             Properties JavaDoc prop = new Properties JavaDoc();
157
158             if (userColorFile != null) {
159                 in = new FileInputStream JavaDoc(userColorFile);
160             } else {
161                 in = getClass().getResourceAsStream(systemColorFile);
162             }
163
164             if (in != null) {
165                 prop.load(in);
166             }
167
168             String JavaDoc errC = prop.getProperty("AnsiColorLogger.ERROR_COLOR");
169             String JavaDoc warn = prop.getProperty("AnsiColorLogger.WARNING_COLOR");
170             String JavaDoc info = prop.getProperty("AnsiColorLogger.INFO_COLOR");
171             String JavaDoc verbose = prop.getProperty("AnsiColorLogger.VERBOSE_COLOR");
172             String JavaDoc debug = prop.getProperty("AnsiColorLogger.DEBUG_COLOR");
173             if (errC != null) {
174                 errColor = PREFIX + errC + SUFFIX;
175             }
176             if (warn != null) {
177                 warnColor = PREFIX + warn + SUFFIX;
178             }
179             if (info != null) {
180                 infoColor = PREFIX + info + SUFFIX;
181             }
182             if (verbose != null) {
183                 verboseColor = PREFIX + verbose + SUFFIX;
184             }
185             if (debug != null) {
186                 debugColor = PREFIX + debug + SUFFIX;
187             }
188         } catch (IOException JavaDoc ioe) {
189             //Ignore - we will use the defaults.
190
} finally {
191             if (in != null) {
192                 try {
193                     in.close();
194                 } catch (IOException JavaDoc e) {
195                     //Ignore - We do not want this to stop the build.
196
}
197             }
198         }
199     }
200
201     /**
202      * @see DefaultLogger#printMessage
203      */

204     /** {@inheritDoc}. */
205     protected void printMessage(final String JavaDoc message,
206                                       final PrintStream JavaDoc stream,
207                                       final int priority) {
208         if (message != null && stream != null) {
209             if (!colorsSet) {
210                 setColors();
211                 colorsSet = true;
212             }
213
214             final StringBuffer JavaDoc msg = new StringBuffer JavaDoc(message);
215             switch (priority) {
216                 case Project.MSG_ERR:
217                     msg.insert(0, errColor);
218                     msg.append(END_COLOR);
219                     break;
220                 case Project.MSG_WARN:
221                     msg.insert(0, warnColor);
222                     msg.append(END_COLOR);
223                     break;
224                 case Project.MSG_INFO:
225                     msg.insert(0, infoColor);
226                     msg.append(END_COLOR);
227                     break;
228                 case Project.MSG_VERBOSE:
229                     msg.insert(0, verboseColor);
230                     msg.append(END_COLOR);
231                     break;
232                 case Project.MSG_DEBUG:
233                     // Fall through
234
default:
235                     msg.insert(0, debugColor);
236                     msg.append(END_COLOR);
237                     break;
238             }
239             final String JavaDoc strmessage = msg.toString();
240             stream.println(strmessage);
241         }
242     }
243 }
244
Popular Tags