1 12 13 package org.eclipse.ant.internal.ui.antsupport.logger; 14 15 import java.io.BufferedReader ; 16 import java.io.File ; 17 import java.io.IOException ; 18 import java.io.StringReader ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import org.apache.tools.ant.BuildEvent; 23 import org.apache.tools.ant.Location; 24 import org.apache.tools.ant.Project; 25 import org.apache.tools.ant.Target; 26 import org.apache.tools.ant.util.FileUtils; 27 import org.eclipse.ant.internal.core.AbstractEclipseBuildLogger; 28 import org.eclipse.ant.internal.ui.AntUtil; 29 import org.eclipse.ant.internal.ui.ExternalHyperlink; 30 import org.eclipse.ant.internal.ui.IAntUIConstants; 31 import org.eclipse.ant.internal.ui.antsupport.AntSupportMessages; 32 import org.eclipse.ant.internal.ui.antsupport.logger.util.AntDebugState; 33 import org.eclipse.ant.internal.ui.launchConfigurations.AntProcess; 34 import org.eclipse.ant.internal.ui.launchConfigurations.AntStreamMonitor; 35 import org.eclipse.ant.internal.ui.launchConfigurations.AntStreamsProxy; 36 import org.eclipse.ant.internal.ui.launchConfigurations.TaskLinkManager; 37 import org.eclipse.core.resources.IFile; 38 import org.eclipse.core.runtime.OperationCanceledException; 39 import org.eclipse.debug.core.DebugPlugin; 40 import org.eclipse.debug.core.model.IProcess; 41 import org.eclipse.debug.ui.console.FileLink; 42 import org.eclipse.jface.text.IRegion; 43 import org.eclipse.jface.text.Region; 44 import org.eclipse.ui.console.IHyperlink; 45 46 public class AntProcessBuildLogger extends NullBuildLogger { 47 48 private File fBuildFileParent= null; 49 private long fStartTime; 50 private Map fFileNameToIFile= new HashMap (); 51 52 55 private AntProcess fProcess = null; 56 57 protected void logMessage(String message, BuildEvent event, int overridePriority) { 58 int priority= overridePriority; 59 if (priority == -1) { 60 priority= event.getPriority(); 61 } 62 63 if (priority > getMessageOutputLevel()) { 64 return; 65 } 66 AntProcess antProcess = getAntProcess(fProcessId); 67 if (antProcess == null) { 68 return; 69 } 70 71 StringBuffer fullMessage= new StringBuffer (); 72 if (!loggingToLogFile()) { 73 fullMessage.append(System.getProperty("line.separator")); } 75 if (event.getException() == null && event.getTask() != null && !fEmacsMode) { 76 adornMessage(event, fullMessage); 77 } else { 78 fullMessage.append(message); 79 } 80 message= fullMessage.toString(); 81 82 if (loggingToLogFile()) { 83 logMessageToLogFile(message, priority); 84 } else { 85 AntStreamMonitor monitor = getMonitor(priority); 86 monitor.append(message); 87 } 88 } 89 90 97 private void adornMessage(BuildEvent event, StringBuffer fullMessage) { 98 String name = event.getTask().getTaskName(); 99 if (name == null) { 100 name = "null"; } 102 Location location = event.getTask().getLocation(); 103 StringBuffer column= new StringBuffer (); 104 int size = IAntUIConstants.LEFT_COLUMN_SIZE - (name.length() + 3); 105 for (int i = 0; i < size; i++) { 106 column.append(' '); 107 } 108 StringBuffer labelBuff= new StringBuffer (); 109 labelBuff.append('['); 110 labelBuff.append(name); 111 labelBuff.append("] "); 113 int offset = Math.max(size, 0) + 1; 114 String label= labelBuff.toString(); 115 if (event.getMessage() == null) { 116 return; 117 } 118 try { 119 BufferedReader r = new BufferedReader (new StringReader (event.getMessage())); 120 String line = r.readLine(); 121 fullMessage.append(column); 122 appendAndLink(fullMessage, location, label, offset, line); 123 line = r.readLine(); 124 while (line != null) { 125 fullMessage.append(System.getProperty("line.separator")); fullMessage.append(column); 127 appendAndLink(fullMessage, location, label, offset, line); 128 line = r.readLine(); 129 } 130 } catch (IOException e) { 131 if (event.getMessage() != null) { 132 fullMessage.append(label).append(event.getMessage()); 133 } 134 } 135 } 136 137 private void appendAndLink(StringBuffer fullMessage, Location location, String label, int offset, String line) { 138 fullMessage.append(label); 139 fullMessage.append(line); 140 if (location != null) { 141 String newLine= (label + line).trim(); 142 IRegion region= new Region(offset, label.length() - 3); IHyperlink link= getLocationLink(location); 144 if (link != null) { 145 TaskLinkManager.addTaskHyperlink(getAntProcess(fProcessId), link, region, newLine); 146 } 147 } 148 } 149 150 private AntStreamMonitor getMonitor(int priority) { 151 AntStreamsProxy proxy = (AntStreamsProxy)fProcess.getStreamsProxy(); 152 AntStreamMonitor monitor = null; 153 switch (priority) { 154 case Project.MSG_INFO: 155 monitor = (AntStreamMonitor)proxy.getOutputStreamMonitor(); 156 break; 157 case Project.MSG_ERR: 158 monitor = (AntStreamMonitor)proxy.getErrorStreamMonitor(); 159 break; 160 case Project.MSG_DEBUG: 161 monitor = (AntStreamMonitor)proxy.getDebugStreamMonitor(); 162 break; 163 case Project.MSG_WARN: 164 monitor = (AntStreamMonitor)proxy.getWarningStreamMonitor(); 165 break; 166 case Project.MSG_VERBOSE: 167 monitor = (AntStreamMonitor)proxy.getVerboseStreamMonitor(); 168 break; 169 } 170 return monitor; 171 } 172 173 private void logMessageToLogFile(String message, int priority) { 174 if (priority == Project.MSG_ERR) { 175 getErrorPrintStream().println(message); 176 } else { 177 getOutputPrintStream().println(message); 178 } 179 } 180 181 188 private IHyperlink getLocationLink(Location location) { 189 if (location != null && !location.equals(Location.UNKNOWN_LOCATION)) { 190 try { 191 String fileName= location.getFileName(); 192 IFile file= (IFile) fFileNameToIFile.get(fileName); 193 int lineNumber= location.getLineNumber(); 194 if (file != null) { 195 return new FileLink(file, null, -1, -1, lineNumber); 196 } 197 file= AntUtil.getFileForLocation(fileName, fBuildFileParent); 198 if (file != null) { 199 fFileNameToIFile.put(fileName, file); 200 return new FileLink(file, null, -1, -1, lineNumber); 201 } 202 File javaIOFile= FileUtils.newFileUtils().resolveFile(fBuildFileParent, fileName); 204 if (javaIOFile.exists()) { 205 return new ExternalHyperlink(javaIOFile, lineNumber); 206 } 207 } catch (NoSuchMethodError e) { 208 return AntUtil.getLocationLink(location.toString(), fBuildFileParent); 210 } 211 } 212 return null; 213 } 214 215 219 protected AntProcess getAntProcess(String processId) { 220 if (fProcess == null && processId != null) { 221 IProcess[] all = DebugPlugin.getDefault().getLaunchManager().getProcesses(); 222 for (int i = 0; i < all.length; i++) { 223 IProcess process = all[i]; 224 if (process instanceof AntProcess && processId.equals(process.getAttribute(AbstractEclipseBuildLogger.ANT_PROCESS_ID))) { 225 fProcess = (AntProcess)process; 226 break; 227 } 228 } 229 } 230 return fProcess; 231 } 232 233 238 public void buildStarted(BuildEvent event) { 239 fStartTime= System.currentTimeMillis(); 240 } 241 242 245 public void buildFinished(BuildEvent event) { 246 String message= handleException(event); 247 if (message != null) { 248 try { 249 BufferedReader r = new BufferedReader (new StringReader (message)); 250 String line = r.readLine(); 251 logMessage(line, event, Project.MSG_ERR); 252 line = r.readLine(); 253 AntProcess antProcess = getAntProcess(fProcessId); 254 while (line != null) { 255 logMessage(line, event, Project.MSG_ERR); 256 if (!message.startsWith("Total time:")) { AntUtil.linkBuildFailedMessage(line, antProcess); 258 } 259 line = r.readLine(); 260 } 261 logMessage("", event, Project.MSG_ERR); } catch (IOException e) { 263 } 264 } 265 fHandledException= null; 266 fBuildFileParent= null; 267 if (!(event.getException() instanceof OperationCanceledException)) { 268 logMessage(getTimeString(System.currentTimeMillis() - fStartTime), event, fMessageOutputLevel); 269 } 270 fProcess= null; 271 event.getProject().removeBuildListener(this); 272 fFileNameToIFile= null; 273 } 274 275 private String getTimeString(long milliseconds) { 276 long seconds = milliseconds / 1000; 277 long minutes = seconds / 60; 278 seconds= seconds % 60; 279 280 StringBuffer result= new StringBuffer (AntSupportMessages.AntProcessBuildLogger_Total_time); 281 if (minutes > 0) { 282 result.append(minutes); 283 if (minutes > 1) { 284 result.append(AntSupportMessages.AntProcessBuildLogger__minutes_2); 285 } else { 286 result.append(AntSupportMessages.AntProcessBuildLogger__minute_3); 287 } 288 } 289 if (seconds > 0) { 290 if (minutes > 0) { 291 result.append(' '); 292 } 293 result.append(seconds); 294 295 if (seconds > 1) { 296 result.append(AntSupportMessages.AntProcessBuildLogger__seconds_4); 297 } else { 298 result.append(AntSupportMessages.AntProcessBuildLogger__second_5); 299 } 300 } 301 if (seconds == 0 && minutes == 0) { 302 result.append(milliseconds); 303 result.append(AntSupportMessages.AntProcessBuildLogger__milliseconds_6); 304 } 305 306 result.append(System.getProperty("line.separator")); return result.toString(); 308 } 309 310 313 public void messageLogged(BuildEvent event) { 314 if (event.getPriority() > getMessageOutputLevel()) { 315 return; 316 } 317 if (event.getMessage() != null && event.getMessage().length() > 0) { 318 logMessage(event.getMessage(), event, -1); 319 } 320 } 321 322 325 public void targetStarted(BuildEvent event) { 326 if (Project.MSG_INFO > getMessageOutputLevel()) { 327 return; 328 } 329 Target target= event.getTarget(); 330 StringBuffer msg= new StringBuffer (System.getProperty("line.separator")); String targetName= target.getName(); 332 msg.append(targetName); 333 msg.append(':'); 334 String message= msg.toString(); 335 Location location= AntDebugState.getLocation(target); 336 if (location != null && location != Location.UNKNOWN_LOCATION) { 337 IRegion region= new Region(0, targetName.length()); 338 IHyperlink link= getLocationLink(location); 339 if (link != null) { 340 TaskLinkManager.addTaskHyperlink(getAntProcess(fProcessId), link, region, message.trim()); 341 } 342 } 343 logMessage(message, event, Project.MSG_INFO); 344 } 345 346 private boolean loggingToLogFile() { 347 return getErrorPrintStream() != null && getErrorPrintStream() != System.err; 349 } 350 } 351 | Popular Tags |