1 28 29 package com.idaremedia.antx.capture; 30 31 import org.apache.tools.ant.BuildEvent; 32 import org.apache.tools.ant.BuildListener; 33 import org.apache.tools.ant.Project; 34 35 import com.idaremedia.antx.AntX; 36 import com.idaremedia.antx.NoiseLevel; 37 import com.idaremedia.antx.apis.ProblemHandler; 38 import com.idaremedia.antx.helpers.Strings; 39 import com.idaremedia.antx.ownhelpers.LocalTk; 40 import com.idaremedia.antx.starters.BuildListenerSkeleton; 41 import com.idaremedia.antx.starters.TaskSet; 42 43 70 71 public class CaptureLogsTask extends TaskSet implements LogsRecorder 72 { 73 76 public CaptureLogsTask() 77 { 78 super(AntX.capture+"CaptureLogsTask:"); 79 } 80 81 82 86 public CaptureLogsTask(String iam) 87 { 88 super(iam); 89 } 90 91 92 93 106 public void setImportantFrom(NoiseLevel threshold) 107 { 108 if (threshold==NoiseLevel.FATAL) { 109 threshold = NoiseLevel.ERROR; } 111 m_importantThreshold= threshold; 112 } 113 114 115 116 121 public NoiseLevel getImportantThreshold() 122 { 123 NoiseLevel nl= m_importantThreshold; 124 return (nl!=null) ? nl : NoiseLevel.getDefault(getProject()); 125 } 126 127 128 129 134 public final boolean isImportant(NoiseLevel nl) 135 { 136 require_(nl!=null,"isImportnt- nonzro NL"); 137 if (NoiseLevel.isAsBadAs(nl,getImportantThreshold())) { 138 return true; 139 } 140 return false; 141 } 142 143 144 145 151 public void setSplitEntries(boolean splitEm) 152 { 153 m_splitMsgs = splitEm; 154 } 155 156 157 162 public final boolean willSplitEntries() 163 { 164 return m_splitMsgs; 165 } 166 167 168 169 174 public void setIncludeSource(boolean includeEm) 175 { 176 m_addMsgSource = includeEm; 177 } 178 179 180 186 public final boolean willIncludeSource() 187 { 188 return m_addMsgSource; 189 } 190 191 192 193 199 public String copyOfImportantLogs() 200 { 201 return getImportantBuffer().substring(0); 202 } 203 204 205 206 212 public String copyOfAllLogs() 213 { 214 return getAllBuffer().substring(0); 215 } 216 217 218 219 222 public void clearLogs() 223 { 224 resetBuffers(); 225 } 226 227 228 229 234 protected final StringBuffer getImportantBuffer() 235 { 236 return m_boobooBuffer; 237 } 238 239 240 241 245 protected final StringBuffer getAllBuffer() 246 { 247 return m_allBuffer; 248 } 249 250 251 252 255 protected final void resetBuffers() 256 { 257 getAllBuffer().delete(0,getAllBuffer().length()); 258 getImportantBuffer().delete(0,getImportantBuffer().length()); 259 } 260 261 262 263 266 protected void performNestedTasks() 267 { 268 Project P = getProject(); 269 try { 270 verify_(!m_isInstalled,"perform- not installed"); 271 verify_(!P.getBuildListeners().contains(getBuildListener()),"perform- not installed"); 272 P.addBuildListener(getBuildListener()); 273 m_isInstalled = true; 274 CapturedLogs.installRecorder(this,m_errHandler); 275 performTheTasksList(); 276 } finally { 277 if (m_isInstalled) { 278 m_isInstalled = false; 279 P.removeBuildListener(getBuildListener()); 280 CapturedLogs.unwindRecorder(m_errHandler); 281 } 282 resetBuffers(); 283 } 284 } 285 286 287 288 291 protected final BuildListener getBuildListener() 292 { 293 return m_buildListener; 294 } 295 296 297 298 private volatile boolean m_isInstalled; 299 private NoiseLevel m_importantThreshold; private boolean m_splitMsgs; private boolean m_addMsgSource; private final Recorder m_buildListener = new Recorder(); 303 private final StringBuffer m_allBuffer = new StringBuffer (320); 304 private final StringBuffer m_boobooBuffer = new StringBuffer (150); 305 private final ProblemHandler m_errHandler= new ProblemHandler() { 306 public void problem(Object nugget, int nl) { 307 CaptureLogsTask.this.log(String.valueOf(nugget),nl); 308 } 309 }; 310 311 312 319 private class Recorder extends BuildListenerSkeleton { 320 326 public void messageLogged(BuildEvent e) { 327 String msg = e.getMessage(); 328 if (willIncludeSource()) { 329 msg = LocalTk.purtyEventMsg(e); 330 } 331 addMsg(getAllBuffer(),msg); 332 if (NoiseLevel.isAsBadAs(e.getPriority(),getImportantThreshold())) { 333 addMsg(getImportantBuffer(),msg); 334 } 335 } 336 337 private void addMsg(StringBuffer sb, String msg) 338 { 339 sb.append(msg); 340 if (willSplitEntries()) { 341 sb.append(Strings.NL); 342 } 343 } 344 } 345 } 346 347 348 | Popular Tags |