You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by ca...@apache.org on 2007/09/21 00:04:13 UTC

svn commit: r577946 - in /logging/log4cxx/trunk/src: main/cpp/file.cpp main/cpp/fileappender.cpp main/include/log4cxx/file.h test/cpp/fileappendertest.cpp

Author: carnold
Date: Thu Sep 20 15:04:13 2007
New Revision: 577946

URL: http://svn.apache.org/viewvc?rev=577946&view=rev
Log:
LOGCXX-134: FileAppender could create missing directories

Added:
    logging/log4cxx/trunk/src/test/cpp/fileappendertest.cpp
      - copied, changed from r577806, logging/log4j/trunk/tests/src/java/org/apache/log4j/FileAppenderTest.java
Modified:
    logging/log4cxx/trunk/src/main/cpp/file.cpp
    logging/log4cxx/trunk/src/main/cpp/fileappender.cpp
    logging/log4cxx/trunk/src/main/include/log4cxx/file.h

Modified: logging/log4cxx/trunk/src/main/cpp/file.cpp
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/cpp/file.cpp?rev=577946&r1=577945&r2=577946&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/cpp/file.cpp (original)
+++ logging/log4cxx/trunk/src/main/cpp/file.cpp Thu Sep 20 15:04:13 2007
@@ -152,3 +152,26 @@
     }
     return filenames;
 }
+
+LogString File::getParent(Pool&) const {
+     LogString::size_type slashPos = name.rfind(LOG4CXX_STR('/'));
+     LogString::size_type backPos = name.rfind(LOG4CXX_STR('\\'));
+     if (slashPos == LogString::npos) {
+         slashPos = backPos;
+     } else {
+         if (backPos != LogString::npos && backPos > slashPos) {
+             slashPos = backPos;
+         }
+     }
+     LogString parent;
+     if (slashPos != LogString::npos && slashPos > 0) {
+          parent.assign(name, 0, slashPos);
+     }
+     return parent;
+}
+
+bool File::mkdirs(Pool& p) const {
+     apr_status_t stat = apr_dir_make_recursive(convertBackSlashes(osName).c_str(),
+          APR_OS_DEFAULT, (apr_pool_t*) p.getAPRPool());
+     return stat == APR_SUCCESS;
+}
\ No newline at end of file

Modified: logging/log4cxx/trunk/src/main/cpp/fileappender.cpp
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/cpp/fileappender.cpp?rev=577946&r1=577945&r2=577946&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/cpp/fileappender.cpp (original)
+++ logging/log4cxx/trunk/src/main/cpp/fileappender.cpp Thu Sep 20 15:04:13 2007
@@ -261,7 +261,24 @@
       }
   }
 
-  OutputStreamPtr outStream(new FileOutputStream(filename, append1));
+  OutputStreamPtr outStream;
+  try {
+      outStream = new FileOutputStream(filename, append1);
+  } catch(IOException& ex) {
+      LogString parentName = File(filename).getParent(p);
+      if (!parentName.empty()) {
+          File parentDir(parentName);
+          if(!parentDir.exists(p) && parentDir.mkdirs(p)) {
+             outStream = new FileOutputStream(filename, append1);
+          } else {
+             throw ex;
+          }
+      } else {
+        throw ex;
+      }
+  }
+  
+  
   //
   //   if a new file and UTF-16, then write a BOM
   //

Modified: logging/log4cxx/trunk/src/main/include/log4cxx/file.h
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/include/log4cxx/file.h?rev=577946&r1=577945&r2=577946&view=diff
==============================================================================
--- logging/log4cxx/trunk/src/main/include/log4cxx/file.h (original)
+++ logging/log4cxx/trunk/src/main/include/log4cxx/file.h Thu Sep 20 15:04:13 2007
@@ -65,6 +65,9 @@
 
                     bool deleteFile(log4cxx::helpers::Pool& p) const;
                     bool renameTo(const File& dest, log4cxx::helpers::Pool& p) const;
+                    
+                    LogString getParent(log4cxx::helpers::Pool& p) const;
+                    bool mkdirs(log4cxx::helpers::Pool& p) const;
 
                 private:
                     LogString name;

Copied: logging/log4cxx/trunk/src/test/cpp/fileappendertest.cpp (from r577806, logging/log4j/trunk/tests/src/java/org/apache/log4j/FileAppenderTest.java)
URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/test/cpp/fileappendertest.cpp?p2=logging/log4cxx/trunk/src/test/cpp/fileappendertest.cpp&p1=logging/log4j/trunk/tests/src/java/org/apache/log4j/FileAppenderTest.java&r1=577806&r2=577946&rev=577946&view=diff
==============================================================================
--- logging/log4j/trunk/tests/src/java/org/apache/log4j/FileAppenderTest.java (original)
+++ logging/log4cxx/trunk/src/test/cpp/fileappendertest.cpp Thu Sep 20 15:04:13 2007
@@ -14,74 +14,72 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
 
-package org.apache.log4j;
+#include <log4cxx/helpers/pool.h>
+#include <log4cxx/fileappender.h>
+#include <log4cxx/patternlayout.h>
 
-import junit.framework.TestCase;
-
-import java.io.File;
-
-import java.lang.reflect.Method;
+using namespace log4cxx;
+using namespace log4cxx::helpers;
 
 
 /**
  *
  * FileAppender tests.
- *
- * @author Curt Arnold
  */
-public class FileAppenderTest extends TestCase {
+class FileAppenderTest : public CppUnit::TestFixture {
+  CPPUNIT_TEST_SUITE(FileAppenderTest);
+          CPPUNIT_TEST(testDirectoryCreation);
+          CPPUNIT_TEST(testgetSetThreshold);
+          CPPUNIT_TEST(testIsAsSevereAsThreshold);
+  CPPUNIT_TEST_SUITE_END();
+public:
   /**
    * Tests that any necessary directories are attempted to
    * be created if they don't exist.  See bug 9150.
    *
    */
-  public void testDirectoryCreation() {
-    //
-    //   known to fail on JDK 1.1
-    if (!System.getProperty("java.version").startsWith("1.1.")) {
-      File newFile = new File("output/newdir/temp.log");
-      newFile.delete();
-
-      File newDir = new File("output/newdir");
-      newDir.delete();
-
-      org.apache.log4j.FileAppender wa = new org.apache.log4j.FileAppender();
-      wa.setFile("output/newdir/temp.log");
-      wa.setLayout(new PatternLayout("%m%n"));
-      wa.activateOptions();
-
-      assertTrue(new File("output/newdir/temp.log").exists());
-    }
-  }
+  void testDirectoryCreation() {
+      File newFile(LOG4CXX_STR("output/newdir/temp.log"));
+      Pool p;
+      newFile.deleteFile(p);
+
+      File newDir(LOG4CXX_STR("output/newdir"));
+      newDir.deleteFile(p);
+
+      FileAppenderPtr wa(new FileAppender());
+      wa->setFile(LOG4CXX_STR("output/newdir/temp.log"));
+      wa->setLayout(new PatternLayout(LOG4CXX_STR("%m%n")));
+      wa->activateOptions(p);
 
-  /**
-   * Tests that the return type of getThreshold is Priority.
-   * @throws Exception
-   */
-  public void testGetThresholdReturnType() throws Exception {
-    Method method = FileAppender.class.getMethod("getThreshold", (Class[]) null);
-    assertTrue(method.getReturnType() == Priority.class);
+      CPPUNIT_ASSERT(File(LOG4CXX_STR("output/newdir/temp.log")).exists(p));
   }
 
   /**
    * Tests getThreshold and setThreshold.
    */
-  public void testgetSetThreshold() {
-    FileAppender appender = new FileAppender();
-    Priority debug = Level.DEBUG;
-    Priority all = Level.ALL;
-    assertNull(appender.getThreshold());
-    appender.setThreshold(debug);
-    assertTrue(appender.getThreshold() == debug);
+  void testgetSetThreshold() {
+    FileAppenderPtr appender = new FileAppender();
+    LevelPtr debug = Level::getDebug();
+    //
+    //  different from log4j where threshold is null.
+    //
+    CPPUNIT_ASSERT_EQUAL(Level::getAll(), appender->getThreshold());
+    appender->setThreshold(debug);
+    CPPUNIT_ASSERT_EQUAL(debug, appender->getThreshold());
   }
 
   /**
    * Tests isAsSevereAsThreshold.
    */
-  public void testIsAsSevereAsThreshold() {
-    FileAppender appender = new FileAppender();
-    Priority debug = Level.DEBUG;
-    assertTrue(appender.isAsSevereAsThreshold(debug));
+  void testIsAsSevereAsThreshold() {
+    FileAppenderPtr appender = new FileAppender();
+    LevelPtr debug = Level::getDebug();
+    CPPUNIT_ASSERT(appender->isAsSevereAsThreshold(debug));
   }
-}
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(FileAppenderTest);
+