1 18 19 package org.apache.tools.ant.listener; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogConfigurationException; 23 import org.apache.commons.logging.LogFactory; 24 25 import org.apache.tools.ant.BuildListener; 26 import org.apache.tools.ant.BuildLogger; 27 import org.apache.tools.ant.BuildEvent; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.Task; 30 import org.apache.tools.ant.UnknownElement; 31 32 import java.io.PrintStream ; 33 34 54 public class CommonsLoggingListener implements BuildListener, BuildLogger { 55 56 57 private boolean initialized = false; 58 59 private LogFactory logFactory; 60 61 64 public static final String TARGET_LOG = "org.apache.tools.ant.Target"; 65 68 public static final String PROJECT_LOG = "org.apache.tools.ant.Project"; 69 70 74 public CommonsLoggingListener() { 75 } 76 77 private Log getLog(String cat, String suffix) { 78 if (suffix != null) { 79 suffix = suffix.replace('.', '-'); 80 suffix = suffix.replace(' ', '-'); 81 cat = cat + "." + suffix; 82 } 83 PrintStream tmpOut = System.out; 84 PrintStream tmpErr = System.err; 85 System.setOut(out); 86 System.setErr(err); 87 88 if (!initialized) { 89 try { 90 logFactory = LogFactory.getFactory(); 91 } catch (LogConfigurationException e) { 92 e.printStackTrace(System.err); 93 return null; 94 } 95 } 96 97 initialized = true; 98 Log log = logFactory.getInstance(cat); 99 System.setOut(tmpOut); 100 System.setErr(tmpErr); 101 return log; 102 } 103 104 105 public void buildStarted(BuildEvent event) { 106 String categoryString = PROJECT_LOG; 107 Log log = getLog(categoryString, null); 108 109 if (initialized) { 110 realLog(log, "Build started.", Project.MSG_INFO, null); 111 } 112 } 113 114 115 public void buildFinished(BuildEvent event) { 116 if (initialized) { 117 String categoryString = PROJECT_LOG; 118 Log log = getLog(categoryString, event.getProject().getName()); 119 120 if (event.getException() == null) { 121 realLog(log, "Build finished.", Project.MSG_INFO, null); 122 } else { 123 realLog(log, "Build finished with error.", Project.MSG_ERR, 124 event.getException()); 125 } 126 } 127 } 128 129 132 133 public void targetStarted(BuildEvent event) { 134 if (initialized) { 135 Log log = getLog(TARGET_LOG, 136 event.getTarget().getName()); 137 realLog(log, "Start: " + event.getTarget().getName(), 140 Project.MSG_VERBOSE, null); 141 } 142 } 143 144 147 148 public void targetFinished(BuildEvent event) { 149 if (initialized) { 150 String targetName = event.getTarget().getName(); 151 Log log = getLog(TARGET_LOG, 152 event.getTarget().getName()); 153 if (event.getException() == null) { 154 realLog(log, "Target end: " + targetName, Project.MSG_DEBUG, null); 155 } else { 156 realLog(log, "Target \"" + targetName 157 + "\" finished with error.", Project.MSG_ERR, 158 event.getException()); 159 } 160 } 161 } 162 163 166 167 public void taskStarted(BuildEvent event) { 168 if (initialized) { 169 Task task = event.getTask(); 170 Object real = task; 171 if (task instanceof UnknownElement) { 172 Object realObj = ((UnknownElement) task).getTask(); 173 if (realObj != null) { 174 real = realObj; 175 } 176 } 177 Log log = getLog(real.getClass().getName(), null); 178 if (log.isTraceEnabled()) { 179 realLog(log, "Task \"" + task.getTaskName() + "\" started ", 180 Project.MSG_VERBOSE, null); 181 } 182 } 183 } 184 185 188 189 public void taskFinished(BuildEvent event) { 190 if (initialized) { 191 Task task = event.getTask(); 192 Object real = task; 193 if (task instanceof UnknownElement) { 194 Object realObj = ((UnknownElement) task).getTask(); 195 if (realObj != null) { 196 real = realObj; 197 } 198 } 199 Log log = getLog(real.getClass().getName(), null); 200 if (event.getException() == null) { 201 if (log.isTraceEnabled()) { 202 realLog(log, "Task \"" + task.getTaskName() + "\" finished.", 203 Project.MSG_VERBOSE, null); 204 } 205 } else { 206 realLog(log, "Task \"" + task.getTaskName() 207 + "\" finished with error.", Project.MSG_ERR, 208 event.getException()); 209 } 210 } 211 } 212 213 214 217 218 public void messageLogged(BuildEvent event) { 219 if (initialized) { 220 Object categoryObject = event.getTask(); 221 String categoryString = null; 222 String categoryDetail = null; 223 224 if (categoryObject == null) { 225 categoryObject = event.getTarget(); 226 if (categoryObject == null) { 227 categoryObject = event.getProject(); 228 categoryString = PROJECT_LOG; 229 categoryDetail = event.getProject().getName(); 230 } else { 231 categoryString = TARGET_LOG; 232 categoryDetail = event.getTarget().getName(); 233 } 234 } else { 235 if (event.getTarget() != null) { 237 categoryString = categoryObject.getClass().getName(); 238 categoryDetail = event.getTarget().getName(); 239 } else { 240 categoryString = categoryObject.getClass().getName(); 241 } 242 243 } 244 245 Log log = getLog(categoryString, categoryDetail); 246 int priority = event.getPriority(); 247 String message = event.getMessage(); 248 realLog(log, message, priority , null); 249 } 250 } 251 252 private void realLog(Log log, String message, int priority, Throwable t) { 253 PrintStream tmpOut = System.out; 254 PrintStream tmpErr = System.err; 255 System.setOut(out); 256 System.setErr(err); 257 switch (priority) { 258 case Project.MSG_ERR: 259 if (t == null) { 260 log.error(message); 261 } else { 262 log.error(message, t); 263 } 264 break; 265 case Project.MSG_WARN: 266 if (t == null) { 267 log.warn(message); 268 } else { 269 log.warn(message, t); 270 } 271 break; 272 case Project.MSG_INFO: 273 if (t == null) { 274 log.info(message); 275 } else { 276 log.info(message, t); 277 } 278 break; 279 case Project.MSG_VERBOSE: 280 log.debug(message); 281 break; 282 case Project.MSG_DEBUG: 283 log.debug(message); 284 break; 285 default: 286 log.error(message); 287 break; 288 } 289 System.setOut(tmpOut); 290 System.setErr(tmpErr); 291 } 292 293 PrintStream out = System.out; 295 PrintStream err = System.err; 296 298 303 public void setMessageOutputLevel(int level) { 304 } 306 307 311 public void setOutputPrintStream(PrintStream output) { 312 this.out = output; 313 } 314 315 320 public void setEmacsMode(boolean emacsMode) { 321 } 323 324 328 public void setErrorPrintStream(PrintStream err) { 329 this.err = err; 330 } 331 332 } 333 | Popular Tags |