]> source.dussan.org Git - aspectj.git/commitdiff
Andrew Huff's patch for the -log option
authoracolyer <acolyer>
Thu, 2 Jun 2005 11:46:31 +0000 (11:46 +0000)
committeracolyer <acolyer>
Thu, 2 Jun 2005 11:46:31 +0000 (11:46 +0000)
org.aspectj.ajdt.core/src/org/aspectj/ajdt/ajc/BuildArgParser.java
org.aspectj.ajdt.core/src/org/aspectj/tools/ajc/Main.java
taskdefs/src/org/aspectj/tools/ant/taskdefs/AjcTask.java
taskdefs/testsrc/org/aspectj/tools/ant/taskdefs/AjcTaskTest.java

index 9aaf068c3bb4528704a6bb1f3a2b92ff1b7190ea..2de67f69375642a4c18cfc089ddfeeaec0e0cccf 100644 (file)
@@ -491,6 +491,9 @@ public class BuildArgParser extends Main {
                                } else {
                                        showError("-outjar requires jar path argument");
                                }
+            } else if (arg.equals("-log")){
+                               // remove it as it's already been handled in org.aspectj.tools.ajc.Main
+                               args.remove(args.get(nextArgIndex));
             } else if (arg.equals("-incremental")) {
                 buildConfig.setIncrementalMode(true);
             } else if (arg.equals("-XincrementalFile")) {
index 5b701334a401079626d24854bb5fe3fdf77a0b85..3861d18e9446f9750abdc42c0c09816a209a85b4 100644 (file)
 package org.aspectj.tools.ajc;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
 import java.util.List;
+import java.util.Date;
 
 import org.aspectj.bridge.AbortException;
 import org.aspectj.bridge.ICommand;
@@ -212,12 +214,12 @@ public class Main {
         IMessageHolder holder = clientHolder;
         if (null == holder) {
             holder = ourHandler;
-            if (verbose) {
+//            if (verbose) {
                 ourHandler.setInterceptor(MessagePrinter.VERBOSE);
-            } else {
-                ourHandler.ignore(IMessage.INFO);
-                ourHandler.setInterceptor(MessagePrinter.TERSE);
-            }
+//            } else {
+//                ourHandler.ignore(IMessage.INFO);
+//                ourHandler.setInterceptor(MessagePrinter.TERSE);
+//            }
         }
         
         // make sure we handle out of memory gracefully...
@@ -268,7 +270,34 @@ public class Main {
      * @param holder the MessageHandler sink for messages.
      */
     public void run(String[] args, IMessageHolder holder) {
-        if (LangUtil.isEmpty(args)) {
+
+               boolean logMode = (-1 != ("" + LangUtil.arrayAsList(args)).indexOf("-log"));
+               PrintStream logStream = null;
+               FileOutputStream fos = null;
+               if (logMode){
+                       int logIndex = LangUtil.arrayAsList(args).indexOf("-log");
+                       String logFileName = args[logIndex + 1];
+                       File logFile = new File(logFileName);
+                       try{
+                               logFile.createNewFile();
+                               fos = new FileOutputStream(logFileName, true);
+                               logStream = new PrintStream(fos,true);
+                       } catch(Exception e){                   
+                               fail(holder, "Couldn't open log file: ", e);                            
+                       }
+                       Date now = new Date();
+                       logStream.println(now.toString());
+                       boolean verbose = (-1 != ("" + LangUtil.arrayAsList(args)).indexOf("-verbose"));
+                       if (verbose) {
+                                       ourHandler.setInterceptor(new LogModeMessagePrinter(true,logStream));
+                         } else {
+                             ourHandler.ignore(IMessage.INFO);
+                                         ourHandler.setInterceptor(new LogModeMessagePrinter(false,logStream));
+                         }
+                       holder = ourHandler;
+               }
+               
+               if (LangUtil.isEmpty(args)) {
             args = new String[] { "-?" };
         }  else if (controller.running()) {
             fail(holder, "already running with controller: " + controller, null);
@@ -324,6 +353,19 @@ public class Main {
                }
        } catch (Throwable t) {
             fail(holder, "unexpected exception", t);
+        } finally{
+                       if (logStream != null){
+                               logStream.close();
+                               logStream = null;
+                       }
+                       if (fos != null){
+                               try {
+                                       fos.close();
+                               } catch (IOException e){
+                                       fail(holder, "unexpected exception", e);
+                               }
+                               fos = null;
+                       }
         }
     }
     
index 60fba4b894b3a1c75ed6881b8b48653c57830846..0427f9c1f51efd6606f026e69a1a51f72ad28b3e 100644 (file)
@@ -25,7 +25,6 @@ import org.apache.tools.ant.types.*;
 import org.aspectj.bridge.*;
 import org.aspectj.tools.ajc.Main;
 import org.aspectj.tools.ajc.Main.MessagePrinter;
-import org.aspectj.tools.ajc.Main.LogModeMessagePrinter;
 import org.aspectj.util.*;
 
 
@@ -292,8 +291,6 @@ public class AjcTask extends MatchingTask {
        // ---------------------------- state and Ant interface thereto
     private boolean verbose;
     private boolean listFileArgs;
-       private boolean loggingMode;
-       private File logFile;
     private boolean failonerror;
     private boolean fork;
     private String maxMem;
@@ -407,8 +404,6 @@ public class AjcTask extends MatchingTask {
         verbose = false;
         xweaveDir = null;
         xdoneSignal = null;
-               loggingMode = false;
-               logFile = null;
     }
 
     protected void ignore(String ignored) {
@@ -546,9 +541,7 @@ public class AjcTask extends MatchingTask {
     }
 
     public void setLog(File file) {
-        //cmd.addFlagged("-log", file.getAbsolutePath());
-               this.loggingMode = true;
-               this.logFile = file;
+        cmd.addFlagged("-log", file.getAbsolutePath());
     }
     
     public void setProceedOnError(boolean proceedOnError) {  
@@ -1166,7 +1159,6 @@ public class AjcTask extends MatchingTask {
      * 
      */
     protected void executeInSameVM(String[] args) {
-               PrintStream logStream = null;
         if (null != maxMem) {
             log("maxMem ignored unless forked: " + maxMem, Project.MSG_WARN);
         }
@@ -1175,26 +1167,7 @@ public class AjcTask extends MatchingTask {
         if (null == holder) {
             MessageHandler mhandler = new MessageHandler(true);
                        final IMessageHandler delegate;
-                       if (!loggingMode){
               delegate  = verbose ? MessagePrinter.VERBOSE: MessagePrinter.TERSE;
-                       }
-                       else{
-                               String logFileName = logFile.toString(); 
-                               try {
-                                        logFile.createNewFile();
-                                    FileOutputStream fos = new FileOutputStream(logFileName, true);
-                                        logStream = new PrintStream(fos,true);
-                                        delegate = new LogModeMessagePrinter(verbose, logStream);
-                                        Date now = new Date();
-                                        logStream.println("Log started: " + now.toString());
-                               } catch (IOException e){
-                                       if (logStream != null) {
-                                               logStream.close();
-                                               logStream = null;
-                                       }
-                                       throw new BuildException("Logfile couldn't be written to: ",e);
-                               }
-                       }
             mhandler.setInterceptor(delegate);
             if (!verbose) {
                 mhandler.ignore(IMessage.INFO);
@@ -1218,14 +1191,7 @@ public class AjcTask extends MatchingTask {
             }
             main = newmain;          
         }
-        try {
-            main.runMain(args, false);
-        } finally {
-            main = null;
-                       if (logStream != null) {
-                               logStream.close();                                      
-                       }
-        }
+        main.runMain(args, false);
         if (failonerror) {
             int errs = holder.numMessages(IMessage.ERROR, false);
             errs -= numPreviousErrors;
@@ -1376,10 +1342,12 @@ public class AjcTask extends MatchingTask {
      * @return
      */
     protected int execInOtherVM(String[] args) {
-        try {
+        try {                  
+                       
             Project project = getProject();
-          LogStreamHandler handler = new LogStreamHandler(this,
-          Project.MSG_INFO, Project.MSG_WARN);
+                       PumpStreamHandler handler = new LogStreamHandler(this,
+                                         Project.MSG_INFO, Project.MSG_WARN);
+                 
 // replace above two lines with what follows as an aid to debugging when running the unit tests....
 //            LogStreamHandler handler = new LogStreamHandler(this,
 //                                 Project.MSG_INFO, Project.MSG_WARN) {
@@ -1402,7 +1370,8 @@ public class AjcTask extends MatchingTask {
 //                                     super.stop();
 //                             }
 //            };
-            Execute exe = new Execute(handler);
+
+                       Execute exe = new Execute(handler);
             exe.setAntRun(project);
             exe.setWorkingDirectory(project.getBaseDir());
             exe.setCommandline(args);
index d7019d061f1d15a783b936e5109641eaeedec084..fcc27eef126bdc430fc32d3dc697afa4ec86bf8c 100644 (file)
@@ -301,35 +301,17 @@ public class AjcTaskTest extends TestCase {
        // verifies that the log attribute of AjcTask writes output to the given log file
        public void testLoggingMode() {
                AjcTask task = getTask("default.lst");
-               File logFile = new File("testLogFile.txt");
+               task.setFailonerror(false);
+               File logFile = new File("testLogFile1.txt");
+               String s = logFile.getAbsolutePath();
                logFile.delete();
-               try {
-                       logFile.createNewFile();
-               } catch (IOException e) {
-                       fail("unexpected " + e.getMessage());
-               }
                long initialLength = logFile.length();
                task.setLog(logFile);
-               checkRun(task, null);
+               runTest(task,null,null);
                long newLength = logFile.length();
                assertTrue(newLength > initialLength);
                logFile.delete();
        }
-       
-       // this test method submitted by patch from Andrew Huff (IBM)
-       // verifies that the log attribute of AjcTask appends output to the given log file
-       public void testLoggingIsAppending(){
-               AjcTask task = getTask("compileError.lst");
-               task.setFailonerror(false);
-               File logFile = new File("testLogFile.txt");
-               task.setLog(logFile);
-               checkRun(task,null);            
-               long oldLength = logFile.length();
-               checkRun(task,null);
-               long newLength = logFile.length();
-               assertTrue(newLength > oldLength);
-               logFile.delete();
-       }
 
     private void checkRun(AjcTask task, String exceptionString) {
                try {