1 18 19 package org.apache.tools.ant; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.PrintStream ; 24 import java.io.StringReader ; 25 26 import org.apache.tools.ant.util.DateUtils; 27 import org.apache.tools.ant.util.StringUtils; 28 29 35 public class DefaultLogger implements BuildLogger { 36 40 public static final int LEFT_COLUMN_SIZE = 12; 41 42 44 protected PrintStream out; 45 46 47 protected PrintStream err; 48 49 50 protected int msgOutputLevel = Project.MSG_ERR; 51 52 53 private long startTime = System.currentTimeMillis(); 54 55 57 protected static final String lSep = StringUtils.LINE_SEP; 58 60 61 protected boolean emacsMode = false; 62 64 65 68 public DefaultLogger() { 69 } 70 71 87 public void setMessageOutputLevel(int level) { 88 this.msgOutputLevel = level; 89 } 90 91 97 public void setOutputPrintStream(PrintStream output) { 98 this.out = new PrintStream (output, true); 99 } 100 101 107 public void setErrorPrintStream(PrintStream err) { 108 this.err = new PrintStream (err, true); 109 } 110 111 117 public void setEmacsMode(boolean emacsMode) { 118 this.emacsMode = emacsMode; 119 } 120 121 126 public void buildStarted(BuildEvent event) { 127 startTime = System.currentTimeMillis(); 128 } 129 130 138 public void buildFinished(BuildEvent event) { 139 Throwable error = event.getException(); 140 StringBuffer message = new StringBuffer (); 141 if (error == null) { 142 message.append(StringUtils.LINE_SEP); 143 message.append(getBuildSuccessfulMessage()); 144 } else { 145 message.append(StringUtils.LINE_SEP); 146 message.append(getBuildFailedMessage()); 147 message.append(StringUtils.LINE_SEP); 148 149 if (Project.MSG_VERBOSE <= msgOutputLevel 150 || !(error instanceof BuildException)) { 151 message.append(StringUtils.getStackTrace(error)); 152 } else { 153 message.append(error.toString()).append(lSep); 154 } 155 } 156 message.append(StringUtils.LINE_SEP); 157 message.append("Total time: "); 158 message.append(formatTime(System.currentTimeMillis() - startTime)); 159 160 String msg = message.toString(); 161 if (error == null) { 162 printMessage(msg, out, Project.MSG_VERBOSE); 163 } else { 164 printMessage(msg, err, Project.MSG_ERR); 165 } 166 log(msg); 167 } 168 169 174 protected String getBuildFailedMessage() { 175 return "BUILD FAILED"; 176 } 177 178 183 protected String getBuildSuccessfulMessage() { 184 return "BUILD SUCCESSFUL"; 185 } 186 187 194 public void targetStarted(BuildEvent event) { 195 if (Project.MSG_INFO <= msgOutputLevel 196 && !event.getTarget().getName().equals("")) { 197 String msg = StringUtils.LINE_SEP 198 + event.getTarget().getName() + ":"; 199 printMessage(msg, out, event.getPriority()); 200 log(msg); 201 } 202 } 203 204 209 public void targetFinished(BuildEvent event) { 210 } 211 212 217 public void taskStarted(BuildEvent event) { 218 } 219 220 225 public void taskFinished(BuildEvent event) { 226 } 227 228 236 public void messageLogged(BuildEvent event) { 237 int priority = event.getPriority(); 238 if (priority <= msgOutputLevel) { 240 241 StringBuffer message = new StringBuffer (); 242 if (event.getTask() != null && !emacsMode) { 243 String name = event.getTask().getTaskName(); 245 String label = "[" + name + "] "; 246 int size = LEFT_COLUMN_SIZE - label.length(); 247 StringBuffer tmp = new StringBuffer (); 248 for (int i = 0; i < size; i++) { 249 tmp.append(" "); 250 } 251 tmp.append(label); 252 label = tmp.toString(); 253 254 try { 255 BufferedReader r = 256 new BufferedReader ( 257 new StringReader (event.getMessage())); 258 String line = r.readLine(); 259 boolean first = true; 260 do { 261 if (first) { 262 if (line == null) { 263 message.append(label); 264 break; 265 } 266 } else { 267 message.append(StringUtils.LINE_SEP); 268 } 269 first = false; 270 message.append(label).append(line); 271 line = r.readLine(); 272 } while (line != null); 273 } catch (IOException e) { 274 message.append(label).append(event.getMessage()); 276 } 277 } else { 278 message.append(event.getMessage()); 279 } 280 Throwable ex = event.getException(); 281 if (Project.MSG_DEBUG <= msgOutputLevel && ex != null) { 282 message.append(StringUtils.getStackTrace(ex)); 283 } 284 285 String msg = message.toString(); 286 if (priority != Project.MSG_ERR) { 287 printMessage(msg, out, priority); 288 } else { 289 printMessage(msg, err, priority); 290 } 291 log(msg); 292 } 293 } 294 295 304 protected static String formatTime(final long millis) { 305 return DateUtils.formatElapsedTime(millis); 306 } 307 308 318 protected void printMessage(final String message, 319 final PrintStream stream, 320 final int priority) { 321 stream.println(message); 322 } 323 324 330 protected void log(String message) { 331 } 332 } 333 | Popular Tags |