ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cos...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/listener CommonsLoggingListener.java
Date Wed, 08 Jan 2003 04:35:22 GMT
costin      2003/01/07 20:35:22

  Modified:    src/main/org/apache/tools/ant/listener
                        CommonsLoggingListener.java
  Log:
  Few changes:
  - implement the BuildLogger - so it can be used with -logger
  
  - few hacks/changes to support logging messages to stdout/stderr without
  going to infinite loop. The trick is to create the logger with stdout
  beeing the "real" one. Just to be sure it also logs with stdout set to
  the real one. This could be done more cleanely probably - with some
  changes in core.
  
  - less verbosity ( task started/stoped doesn't need info, etc ).
  
  - the log category is based on the real task name, combine with the
  target. That means you can arbitrarily enable debug on tasks and
  to tasks that are in particular target.
  
  Revision  Changes    Path
  1.2       +173 -49   jakarta-ant/src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java
  
  Index: CommonsLoggingListener.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/listener/CommonsLoggingListener.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommonsLoggingListener.java	23 Apr 2002 18:33:52 -0000	1.1
  +++ CommonsLoggingListener.java	8 Jan 2003 04:35:21 -0000	1.2
  @@ -57,11 +57,9 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogConfigurationException;
   import org.apache.commons.logging.LogFactory;
  -import org.apache.tools.ant.BuildEvent;
  -import org.apache.tools.ant.BuildListener;
  -import org.apache.tools.ant.Project;
  -import org.apache.tools.ant.Target;
  -import org.apache.tools.ant.Task;
  +import org.apache.tools.ant.*;
  +
  +import java.io.PrintStream;
   
   /**
    * Jakarta Commons Logging listener.
  @@ -69,10 +67,22 @@
    * causes an infinite loop since it writes to System.err, which Ant traps
    * and reroutes to the logger/listener layer.
    *
  + * The following names are used for the log:
  + *  org.apache.tools.ant.Project.PROJECT_NAME  - for project events
  + *  org.apache.tools.ant.Target.TARGET_NAME - for target events
  + *  TASK_CLASS_NAME.TARGET_NAME - for events in individual targets.
  + *
  + * In all target and project names we replace "." and " " with "-".
  + *
  + * TODO: we should use the advanced context logging features ( and expose them
  + * in c-l first :-)
  + * TODO: this is _very_ inefficient. Switching the out and tracking the logs
  + * can be optimized a lot - but may require few more changes to the core.
  + *
    * @author Erik Hatcher
    * @since Ant 1.5
    */
  -public class CommonsLoggingListener implements BuildListener {
  +public class CommonsLoggingListener implements BuildListener, BuildLogger {
   
       /** Indicates if the listener was initialized. */
       private boolean initialized = false;
  @@ -84,23 +94,44 @@
        * can be obtained.
        */
       public CommonsLoggingListener() {
  -        try {
  -            logFactory = LogFactory.getFactory();
  -        } catch (LogConfigurationException e) {
  -            e.printStackTrace(System.err);
  -            return;
  +    }
  +
  +    private Log getLog( String cat, String suffix ) {
  +        if( suffix != null ) {
  +            suffix=suffix.replace('.', '-');
  +            suffix=suffix.replace(' ', '-');
  +            cat=cat + "." + suffix;
  +        }
  +        PrintStream tmpOut=System.out;
  +        PrintStream tmpErr=System.err;
  +        System.setOut( out );
  +        System.setErr( err );
  +
  +        if( ! initialized ) {
  +            try {
  +                logFactory = LogFactory.getFactory();
  +            } catch (LogConfigurationException e) {
  +                e.printStackTrace(System.err);
  +                return null;
  +            }
           }
   
           initialized = true;
  +        Log log=logFactory.getInstance(cat);
  +        System.setOut( tmpOut );
  +        System.setErr( tmpErr );
  +        return log;
       }
   
       /**
        * @see BuildListener#buildStarted
        */
       public void buildStarted(BuildEvent event) {
  +        String categoryString= "org.apache.tools.ant.Project";
  +        Log log=getLog(categoryString, null);
  +
           if (initialized) {
  -            Log log = logFactory.getInstance(Project.class);
  -            log.info("Build started.");
  +            realLog( log, "Build started.", Project.MSG_INFO, null);
           }
       }
   
  @@ -109,11 +140,14 @@
        */
       public void buildFinished(BuildEvent event) {
           if (initialized) {
  -            Log log = logFactory.getInstance(Project.class);
  +            String categoryString= "org.apache.tools.ant.Project";
  +            Log log=getLog(categoryString, event.getProject().getName());
  +
               if (event.getException() == null) {
  -                log.info("Build finished.");
  +                realLog( log, "Build finished.", Project.MSG_INFO, null);
               } else {
  -                log.error("Build finished with error.", event.getException());
  +                realLog( log, "Build finished with error.", Project.MSG_ERR,
  +                        event.getException());
               }
           }
       }
  @@ -123,8 +157,12 @@
        */
       public void targetStarted(BuildEvent event) {
           if (initialized) {
  -            Log log = logFactory.getInstance(Target.class);
  -            log.info("Target \"" + event.getTarget().getName() + "\" started.");
  +            Log log = getLog("org.apache.tools.ant.Target",
  +                    event.getTarget().getName() );
  +            // Since task log category includes target, we don't really
  +            // need this message
  +            realLog( log, "Start: " + event.getTarget().getName(),
  +                    Project.MSG_DEBUG, null);
           }
       }
   
  @@ -134,12 +172,14 @@
       public void targetFinished(BuildEvent event) {
           if (initialized) {
               String targetName = event.getTarget().getName();
  -            Log log = logFactory.getInstance(Target.class);
  +            Log log = getLog("org.apache.tools.ant.Target",
  +                    event.getTarget().getName() );
               if (event.getException() == null) {
  -                log.info("Target \"" + targetName + "\" finished.");
  +                realLog(log, "Target end: " + targetName, Project.MSG_DEBUG, null);
               } else {
  -                log.error("Target \"" + targetName
  -                        + "\" finished with error.", event.getException());
  +                realLog(log, "Target \"" + targetName
  +                        + "\" finished with error.", Project.MSG_ERR,
  +                        event.getException());
               }
           }
       }
  @@ -150,8 +190,18 @@
       public void taskStarted(BuildEvent event) {
           if (initialized) {
               Task task = event.getTask();
  -            Log log = logFactory.getInstance(task.getClass().getName());
  -            log.info("Task \"" + task.getTaskName() + "\" started.");
  +            Object real=task;
  +            if( task instanceof UnknownElement ) {
  +                Object realObj=((UnknownElement)task).getObject();
  +                if( realObj!=null ) {
  +                    real=realObj;
  +                }
  +            }
  +            Log log = getLog(real.getClass().getName(), null);
  +            if( log.isTraceEnabled()) {
  +                realLog( log, "Task \"" + task.getTaskName() + "\" started ",
  +                        Project.MSG_VERBOSE, null);
  +            }
           }
       }
   
  @@ -161,50 +211,124 @@
       public void taskFinished(BuildEvent event) {
           if (initialized) {
               Task task = event.getTask();
  -            Log log = logFactory.getInstance(task.getClass().getName());
  +            Object real=task;
  +            if( task instanceof UnknownElement ) {
  +                Object realObj=((UnknownElement)task).getObject();
  +                if( realObj!=null ) {
  +                    real=realObj;
  +                }
  +            }
  +            Log log = getLog(real.getClass().getName(), null);
               if (event.getException() == null) {
  -                log.info("Task \"" + task.getTaskName() + "\" finished.");
  +                if( log.isTraceEnabled() ) {
  +                    realLog( log, "Task \"" + task.getTaskName() + "\" finished.",
  +                            Project.MSG_VERBOSE, null);
  +                }
               } else {
  -                log.error("Task \"" + task.getTaskName()
  -                        + "\" finished with error.", event.getException());
  +                realLog( log, "Task \"" + task.getTaskName()
  +                        + "\" finished with error.", Project.MSG_ERR,
  +                        event.getException());
               }
           }
       }
   
  +
       /**
        * @see BuildListener#messageLogged
        */
       public void messageLogged(BuildEvent event) {
           if (initialized) {
               Object categoryObject = event.getTask();
  +            String categoryString=null;
  +            String categoryDetail=null;
  +
               if (categoryObject == null) {
                   categoryObject = event.getTarget();
                   if (categoryObject == null) {
                       categoryObject = event.getProject();
  +                    categoryString="org.apache.tools.ant.Project";
  +                    categoryDetail=event.getProject().getName();
  +                } else {
  +                    categoryString= "org.apache.tools.ant.Target";
  +                    categoryDetail=event.getTarget().getName();
  +                }
  +            } else {
  +                // It's a task - append the target
  +                if( event.getTarget() != null ) {
  +                    categoryString=categoryObject.getClass().getName();
  +                    categoryDetail=event.getTarget().getName();
  +                } else {
  +                    categoryString=categoryObject.getClass().getName();
                   }
  -            }
   
  -            Log log = logFactory.getInstance(categoryObject.getClass().getName());
  -            switch (event.getPriority()) {
  -                case Project.MSG_ERR:
  -                    log.error(event.getMessage());
  -                    break;
  -                case Project.MSG_WARN:
  -                    log.warn(event.getMessage());
  -                    break;
  -                case Project.MSG_INFO:
  -                    log.info(event.getMessage());
  -                    break;
  -                case Project.MSG_VERBOSE:
  -                    log.debug(event.getMessage());
  -                    break;
  -                case Project.MSG_DEBUG:
  -                    log.debug(event.getMessage());
  -                    break;
  -                default:
  -                    log.error(event.getMessage());
  -                    break;
               }
  +
  +            Log log = getLog(categoryString, categoryDetail);
  +            int priority=event.getPriority();
  +            String message=event.getMessage();
  +            realLog( log, message, priority , null);
  +        }
  +    }
  +
  +    private void realLog( Log log, String message, int priority, Throwable t )
  +    {
  +        PrintStream tmpOut=System.out;
  +        PrintStream tmpErr=System.err;
  +        System.setOut( out );
  +        System.setErr( err );
  +        switch (priority) {
  +            case Project.MSG_ERR:
  +                if( t==null ) {
  +                    log.error(message);
  +                } else {
  +                    log.error( message,t );
  +                }
  +                break;
  +            case Project.MSG_WARN:
  +                if( t==null ) {
  +                    log.warn(message);
  +                } else {
  +                    log.warn( message,t );
  +                }
  +                break;
  +            case Project.MSG_INFO:
  +                if( t==null ) {
  +                    log.info(message);
  +                } else {
  +                    log.info( message,t );
  +                }
  +                break;
  +            case Project.MSG_VERBOSE:
  +                log.debug(message);
  +                break;
  +            case Project.MSG_DEBUG:
  +                log.debug(message);
  +                break;
  +            default:
  +                log.error(message);
  +                break;
           }
  +        System.setOut( tmpOut );
  +        System.setErr( tmpErr );
       }
  +
  +    PrintStream out;
  +    PrintStream err;
  +
  +    public void setMessageOutputLevel(int level) {
  +        // Use the logger config
  +    }
  +
  +    public void setOutputPrintStream(PrintStream output) {
  +        this.out = output;
  +    }
  +
  +    public void setEmacsMode(boolean emacsMode) {
  +        // Doesn't make sense for c-l. Use the logger config
  +    }
  +
  +    public void setErrorPrintStream(PrintStream err) {
  +        this.err=err;
  +    }
  +
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message