[Swift-commit] r6517 - in branches/faster: etc src/org/griphyn/vdl/karajan src/org/griphyn/vdl/util

hategan at ci.uchicago.edu hategan at ci.uchicago.edu
Fri May 24 18:56:24 CDT 2013


Author: hategan
Date: 2013-05-24 18:56:24 -0500 (Fri, 24 May 2013)
New Revision: 6517

Added:
   branches/faster/src/org/griphyn/vdl/util/LazyFileAppender.java
Modified:
   branches/faster/etc/log4j.properties
   branches/faster/src/org/griphyn/vdl/karajan/Loader.java
Log:
bye bye swift.log

Modified: branches/faster/etc/log4j.properties
===================================================================
--- branches/faster/etc/log4j.properties	2013-05-24 23:16:26 UTC (rev 6516)
+++ branches/faster/etc/log4j.properties	2013-05-24 23:56:24 UTC (rev 6517)
@@ -6,7 +6,7 @@
 log4j.appender.CONSOLE.Threshold=INFO
 log4j.appender.CONSOLE.layout.ConversionPattern=%m%n
 
-log4j.appender.FILE=org.apache.log4j.FileAppender
+log4j.appender.FILE=org.griphyn.vdl.util.LazyFileAppender
 log4j.appender.FILE.File=swift.log
 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
 log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSSZZZZZ} %-5p %c{1} %m%n

Modified: branches/faster/src/org/griphyn/vdl/karajan/Loader.java
===================================================================
--- branches/faster/src/org/griphyn/vdl/karajan/Loader.java	2013-05-24 23:16:26 UTC (rev 6516)
+++ branches/faster/src/org/griphyn/vdl/karajan/Loader.java	2013-05-24 23:56:24 UTC (rev 6517)
@@ -65,6 +65,7 @@
 import org.griphyn.vdl.toolkit.VDLt2VDLx;
 import org.griphyn.vdl.toolkit.VDLt2VDLx.IncorrectInvocationException;
 import org.griphyn.vdl.toolkit.VDLt2VDLx.ParsingException;
+import org.griphyn.vdl.util.LazyFileAppender;
 import org.griphyn.vdl.util.VDL2Config;
 import org.griphyn.vdl.util.VDL2ConfigProperties;
 import org.griphyn.vdl.util.VDL2ConfigProperties.PropInfo;
@@ -96,9 +97,6 @@
     public static String buildVersion;
 
     public static void main(String[] argv) {
-        if (logger.isDebugEnabled()) {
-            logger.debug("Swift started");
-        }
         ArgumentParser ap = buildArgumentParser();
         String project = null;
         try {
@@ -557,6 +555,9 @@
         }
         else {
             fa.setFile(f.getAbsolutePath());
+            if (fa instanceof LazyFileAppender) {
+                ((LazyFileAppender) fa).fileNameConfigured();
+            }
             fa.activateOptions();
         }
         Level level = Level.WARN;

Added: branches/faster/src/org/griphyn/vdl/util/LazyFileAppender.java
===================================================================
--- branches/faster/src/org/griphyn/vdl/util/LazyFileAppender.java	                        (rev 0)
+++ branches/faster/src/org/griphyn/vdl/util/LazyFileAppender.java	2013-05-24 23:56:24 UTC (rev 6517)
@@ -0,0 +1,95 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on May 24, 2013
+ */
+package org.griphyn.vdl.util;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.log4j.FileAppender;
+import org.apache.log4j.helpers.LogLog;
+
+public class LazyFileAppender extends FileAppender {
+    /**
+     * Override FileAppender.setFile to avoid creating an empty log file before
+     * the code has a chance to customize the file name.
+     */
+    public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
+            throws IOException {
+        LogLog.debug("setFile called: " + fileName + ", " + append);
+
+        // set a stdout writer just in case
+        reset();
+        Writer fw = createWriter(System.out);
+        this.setQWForFiles(fw);
+        
+        this.fileName = fileName;
+        this.fileAppend = append;
+        this.bufferedIO = bufferedIO;
+        this.bufferSize = bufferSize;
+        LogLog.debug("setFile ended");
+    }
+    
+    /**
+     * Calling this method will signal this class that the log file name
+     * has been configured and that the file can now be opened.
+     * @throws IOException 
+     */
+    public void fileNameConfigured() throws IOException {
+        LogLog.debug("fileNameConfigured called");
+
+        // It does not make sense to have immediate flush and bufferedIO.
+        if (this.bufferedIO) {
+            setImmediateFlush(false);
+        }
+
+        // Save file name since reset() sets it to null
+        String fileName = this.fileName;
+        reset();
+        this.fileName = fileName;
+        FileOutputStream ostream = null;
+        try {
+            //
+            // attempt to create file
+            //
+            ostream = new FileOutputStream(this.fileName, this.fileAppend);
+        }
+        catch (FileNotFoundException ex) {
+            //
+            // if parent directory does not exist then
+            // attempt to create it and try to create file
+            // see bug 9150
+            //
+            String parentName = new File(this.fileName).getParent();
+            if (parentName != null) {
+                File parentDir = new File(parentName);
+                if (!parentDir.exists() && parentDir.mkdirs()) {
+                    ostream = new FileOutputStream(this.fileName, this.fileAppend);
+                }
+                else {
+                    throw ex;
+                }
+            }
+            else {
+                throw ex;
+            }
+        }
+        Writer fw = createWriter(ostream);
+        if (this.bufferedIO) {
+            fw = new BufferedWriter(fw, this.bufferSize);
+        }
+        this.setQWForFiles(fw);
+        writeHeader();
+        LogLog.debug("fileNameConfigured ended");
+    }
+}




More information about the Swift-commit mailing list