1 package net.sourceforge.cruisecontrol.util; 2 3 import java.io.File ; 4 import java.io.FileWriter ; 5 import java.io.IOException ; 6 import java.io.Writer ; 7 import java.text.SimpleDateFormat ; 8 import java.util.Date ; 9 10 import org.apache.tools.ant.BuildEvent; 11 import org.apache.tools.ant.XmlLogger; 12 13 20 public class XmlLoggerWithStatus extends XmlLogger { 21 22 private Writer out; 23 private boolean inited; 24 private String targetFilter; 25 26 private static final SimpleDateFormat FORMATTER = new SimpleDateFormat ("HH:mm:ss"); 27 28 35 public void init(BuildEvent event) { 36 if (!inited) { 37 String statusFileName = event.getProject().getProperty("XmlLoggerWithStatus.file"); 38 if (statusFileName == null) { 39 statusFileName = "buildstatus.txt"; 40 } 41 this.targetFilter = event.getProject().getProperty("XmlLoggerWithStatus.filter"); 42 43 File parentDir = new File (statusFileName).getParentFile(); 45 if (parentDir != null && !parentDir.exists()) { 46 parentDir.mkdirs(); 47 } 48 49 try { 51 out = new FileWriter (statusFileName, true); 52 } catch (Exception e) { 53 System.err.println("Error opening file " + statusFileName + " for appending"); 54 e.printStackTrace(System.err); 55 } 56 inited = true; 57 } 58 } 59 60 63 public void buildFinished(BuildEvent event) { 64 super.buildFinished(event); 65 if (out != null) { 66 try { 67 out.close(); 68 } catch (IOException ignored) { 69 } 70 } 71 } 72 73 76 public void targetStarted(BuildEvent event) { 77 super.targetStarted(event); 78 init(event); 79 if (out != null) { 80 String name = event.getTarget().getName(); 81 if (this.targetFilter != null && name.matches(this.targetFilter)){ 82 return; 83 } 84 StringBuffer content = new StringBuffer (); 85 content.append(System.getProperty("line.separator")); 86 content.append(FORMATTER.format(new Date ())); 87 content.append(" ["); 88 content.append(name); 89 content.append("] "); 90 writeStatus(content); 91 } 92 } 93 94 97 private void writeStatus(StringBuffer content) { 98 try { 99 out.write(content.toString()); 100 out.flush(); 101 } catch (IOException e) { 102 System.err.println("Error writing statusline to writer"); 103 e.printStackTrace(System.err); 104 } 105 } 106 107 } 108 | Popular Tags |