View Javadoc
1 package xdoclet.ant; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogConfigurationException; 5 import org.apache.commons.logging.impl.Jdk14Logger; 6 import org.apache.commons.logging.impl.LogFactoryImpl; 7 import org.apache.tools.ant.Project; 8 9 import java.io.PrintWriter; 10 import java.io.StringWriter; 11 12 import java.util.HashMap; 13 import java.util.Map; 14 15 /*** 16 * A LogFactory that returns a Log that uses Ant's logging mechanism. 17 * Ant's Logging API does not 18 * have the concept of categories. The output level is globally set, and it works as 19 * if there was only one "Category". The Log objects will prepend the logger name 20 * to all output messages. 21 * 22 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a> 23 * @version $Revision: 1.7 $ 24 */ 25 public class AntLogFactory extends LogFactoryImpl { 26 private static Project _project; 27 private static final Map LOGS = new HashMap(); 28 private static final Map LEVELS = new HashMap(); 29 30 static { 31 LEVELS.put( new Integer( Project.MSG_DEBUG ), "DEBUG" ); 32 LEVELS.put( new Integer( Project.MSG_ERR ), "ERROR" ); 33 LEVELS.put( new Integer( Project.MSG_INFO ), "INFO" ); 34 LEVELS.put( new Integer( Project.MSG_VERBOSE ), "VERBOSE" ); 35 LEVELS.put( new Integer( Project.MSG_WARN ), "WARNING" ); 36 } 37 38 /*** 39 * Sets the Ant project. 40 * @param project the Ant project. 41 */ 42 static final void setProject( Project project ) { 43 _project = project; 44 } 45 46 /*** 47 * Gets a Log instance for a class. 48 * @param clazz the Class for which the Log is retrieved. 49 * @return a Log instance. 50 * @throws LogConfigurationException if the Log object could not be created. 51 */ 52 public final Log getInstance( Class clazz ) 53 throws LogConfigurationException { 54 return getInstance( clazz.getName() ); 55 } 56 57 /*** 58 * Gets a Log instance for a String. 59 * @param name the String for which the Log is retrieved. 60 * @return a Log instance. 61 * @throws LogConfigurationException if the Log object could not be created. 62 */ 63 public final Log getInstance( final String name ) 64 throws LogConfigurationException { 65 Log log; 66 67 if( _project == null ) { 68 // We're not running in an Ant environment. (The VM might have 69 // run XDoclet via Ant and registered us. Use JDK 1.4 logging. 70 log = new Jdk14Logger( name ); 71 LOGS.put( name, log ); 72 log.warn( "Running outside Ant now. Using JDK 1.4 logging." ); 73 } 74 75 log = ( Log ) LOGS.get( name ); 76 77 if( log == null ) { 78 log = new Log() { 79 // log if prefix is not specified or if it's set and the log name starts with the prefix 80 private final String logPrefix = _project.getProperty( "xdoclet.log.prefix" ); 81 private final boolean shouldLog = logPrefix == null || name.startsWith( logPrefix ); 82 83 private final void log( int antLevel, Object msg, Throwable ex ) { 84 85 if( shouldLog ) { 86 String message = ( msg != null ) ? msg.toString() : "[NO MESSAGE]"; 87 String level = ( String ) LEVELS.get( new Integer( antLevel ) ); 88 89 _project.log( new StringBuffer( "[XDOCLET " ).append( level ).append( " " ).append( name ).append( "]: " ).append( message ) 90 .toString(), antLevel ); 91 92 if( ex != null ) { 93 StringWriter sw = new StringWriter(); 94 95 ex.printStackTrace( new PrintWriter( sw ) ); 96 _project.log( sw.getBuffer().toString() ); 97 } 98 } 99 } 100 101 public final void trace( Object message ) { 102 log( Project.MSG_DEBUG, message, null ); 103 } 104 105 public final void trace( Object message, Throwable t ) { 106 log( Project.MSG_DEBUG, message, t ); 107 } 108 109 public final void debug( Object message ) { 110 log( Project.MSG_DEBUG, message, null ); 111 } 112 113 public final void debug( Object message, Throwable t ) { 114 log( Project.MSG_DEBUG, message, t ); 115 } 116 117 public final void info( Object message ) { 118 log( Project.MSG_INFO, message, null ); 119 } 120 121 public final void info( Object message, Throwable t ) { 122 log( Project.MSG_DEBUG, message, t ); 123 } 124 125 public final void warn( Object message ) { 126 log( Project.MSG_WARN, message, null ); 127 } 128 129 public final void warn( Object message, Throwable t ) { 130 log( Project.MSG_WARN, message, t ); 131 } 132 133 public final void error( Object message ) { 134 log( Project.MSG_ERR, message, null ); 135 } 136 137 public final void error( Object message, Throwable t ) { 138 log( Project.MSG_ERR, message, t ); 139 } 140 141 public final void fatal( Object message ) { 142 log( Project.MSG_ERR, message, null ); 143 } 144 145 public final void fatal( Object message, Throwable t ) { 146 log( Project.MSG_ERR, message, t ); 147 } 148 149 // Unfortunately, the BuildLogger in Ant doesn't have an API 150 // that lets us ask what logging level it's configured to. 151 // Therefore we have to assume logging is enabled for all categories 152 public final boolean isDebugEnabled() { 153 return true; 154 } 155 156 public final boolean isErrorEnabled() { 157 return true; 158 } 159 160 public final boolean isFatalEnabled() { 161 return true; 162 } 163 164 public final boolean isInfoEnabled() { 165 return true; 166 } 167 168 public final boolean isTraceEnabled() { 169 return true; 170 } 171 172 public final boolean isWarnEnabled() { 173 return true; 174 } 175 }; 176 177 LOGS.put( name, log ); 178 } 179 180 return log; 181 } 182 }

This page was automatically generated by Maven