1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.PrintStream ; 23 import org.apache.tools.ant.BuildEvent; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.BuildLogger; 26 import org.apache.tools.ant.DefaultLogger; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.SubBuildListener; 29 import org.apache.tools.ant.util.StringUtils; 30 31 37 public class RecorderEntry implements BuildLogger, SubBuildListener { 38 39 42 43 private String filename = null; 44 45 private boolean record = true; 46 47 private int loglevel = Project.MSG_INFO; 48 49 private PrintStream out = null; 50 51 private long targetStartTime = 0L; 52 53 private boolean emacsMode = false; 54 55 private Project project; 56 57 60 63 protected RecorderEntry(String name) { 64 targetStartTime = System.currentTimeMillis(); 65 filename = name; 66 } 67 68 71 74 public String getFilename() { 75 return filename; 76 } 77 78 83 public void setRecordState(Boolean state) { 84 if (state != null) { 85 flush(); 86 record = state.booleanValue(); 87 } 88 } 89 90 93 94 public void buildStarted(BuildEvent event) { 95 log("> BUILD STARTED", Project.MSG_DEBUG); 96 } 97 98 101 102 public void buildFinished(BuildEvent event) { 103 log("< BUILD FINISHED", Project.MSG_DEBUG); 104 105 if (record && out != null) { 106 Throwable error = event.getException(); 107 108 if (error == null) { 109 out.println(StringUtils.LINE_SEP + "BUILD SUCCESSFUL"); 110 } else { 111 out.println(StringUtils.LINE_SEP + "BUILD FAILED" 112 + StringUtils.LINE_SEP); 113 error.printStackTrace(out); 114 } 115 } 116 cleanup(); 117 } 118 119 128 public void subBuildFinished(BuildEvent event) { 129 if (event.getProject() == project) { 130 cleanup(); 131 } 132 } 133 134 141 public void subBuildStarted(BuildEvent event) { 142 } 143 144 147 148 public void targetStarted(BuildEvent event) { 149 log(">> TARGET STARTED -- " + event.getTarget(), Project.MSG_DEBUG); 150 log(StringUtils.LINE_SEP + event.getTarget().getName() + ":", 151 Project.MSG_INFO); 152 targetStartTime = System.currentTimeMillis(); 153 } 154 155 158 159 public void targetFinished(BuildEvent event) { 160 log("<< TARGET FINISHED -- " + event.getTarget(), Project.MSG_DEBUG); 161 162 String time = formatTime(System.currentTimeMillis() - targetStartTime); 163 164 log(event.getTarget() + ": duration " + time, Project.MSG_VERBOSE); 165 flush(); 166 } 167 168 171 172 public void taskStarted(BuildEvent event) { 173 log(">>> TASK STARTED -- " + event.getTask(), Project.MSG_DEBUG); 174 } 175 176 179 180 public void taskFinished(BuildEvent event) { 181 log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG); 182 flush(); 183 } 184 185 188 189 public void messageLogged(BuildEvent event) { 190 log("--- MESSAGE LOGGED", Project.MSG_DEBUG); 191 192 StringBuffer buf = new StringBuffer (); 193 194 if (event.getTask() != null) { 195 String name = event.getTask().getTaskName(); 196 197 if (!emacsMode) { 198 String label = "[" + name + "] "; 199 int size = DefaultLogger.LEFT_COLUMN_SIZE - label.length(); 200 201 for (int i = 0; i < size; i++) { 202 buf.append(" "); 203 } 204 buf.append(label); 205 } 206 } 207 buf.append(event.getMessage()); 208 209 log(buf.toString(), event.getPriority()); 210 } 211 212 213 219 private void log(String mesg, int level) { 220 if (record && (level <= loglevel) && out != null) { 221 out.println(mesg); 222 } 223 } 224 225 private void flush() { 226 if (record && out != null) { 227 out.flush(); 228 } 229 } 230 231 234 235 public void setMessageOutputLevel(int level) { 236 if (level >= Project.MSG_ERR && level <= Project.MSG_DEBUG) { 237 loglevel = level; 238 } 239 } 240 241 244 245 public void setOutputPrintStream(PrintStream output) { 246 closeFile(); 247 out = output; 248 } 249 250 251 254 255 public void setEmacsMode(boolean emacsMode) { 256 this.emacsMode = emacsMode; 257 } 258 259 260 263 264 public void setErrorPrintStream(PrintStream err) { 265 setOutputPrintStream(err); 266 } 267 268 269 private static String formatTime(long millis) { 270 long seconds = millis / 1000; 271 long minutes = seconds / 60; 272 273 274 if (minutes > 0) { 275 return Long.toString(minutes) + " minute" 276 + (minutes == 1 ? " " : "s ") 277 + Long.toString(seconds % 60) + " second" 278 + (seconds % 60 == 1 ? "" : "s"); 279 } else { 280 return Long.toString(seconds) + " second" 281 + (seconds % 60 == 1 ? "" : "s"); 282 } 283 284 } 285 286 293 public void setProject(Project project) { 294 this.project = project; 295 if (project != null) { 296 project.addBuildListener(this); 297 } 298 } 299 300 303 public void cleanup() { 304 closeFile(); 305 if (project != null) { 306 project.removeBuildListener(this); 307 } 308 project = null; 309 } 310 311 319 void openFile(boolean append) throws BuildException { 320 openFileImpl(append); 321 } 322 323 328 void closeFile() { 329 if (out != null) { 330 out.close(); 331 out = null; 332 } 333 } 334 335 341 void reopenFile() throws BuildException { 342 openFileImpl(true); 343 } 344 345 private void openFileImpl(boolean append) throws BuildException { 346 if (out == null) { 347 try { 348 out = new PrintStream (new FileOutputStream (filename, append)); 349 } catch (IOException ioe) { 350 throw new BuildException("Problems opening file using a " 351 + "recorder entry", ioe); 352 } 353 } 354 } 355 356 } 357 | Popular Tags |