logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carn...@apache.org
Subject svn commit: r510214 - in /logging/log4j/branches/v1_2-branch: src/java/org/apache/log4j/spi/ tests/src/java/org/apache/log4j/spi/
Date Wed, 21 Feb 2007 21:07:24 GMT
Author: carnold
Date: Wed Feb 21 13:07:23 2007
New Revision: 510214

URL: http://svn.apache.org/viewvc?view=rev&rev=510214
Log:
Bug 41219: Stacktraces of exceptions disappear occassionally

Added:
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/NullWriter.java
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/VectorWriter.java
Modified:
    logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/ThrowableInformation.java
    logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java

Added: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/NullWriter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/NullWriter.java?view=auto&rev=510214
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/NullWriter.java (added)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/NullWriter.java Wed Feb
21 13:07:23 2007
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.log4j.spi;
+import java.io.Writer;
+
+/**
+  * NullWriter is an obsolete class provided only for
+  *  binary compatibility with earlier versions of log4j and should not be used.
+  *
+  * @deprecated
+  */
+class NullWriter extends Writer {
+
+  public void close() {
+    // blank
+  }
+
+  public void flush() {
+    // blank
+  }
+
+  public void write(char[] cbuf, int off, int len) {
+    // blank
+  }
+}

Modified: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/ThrowableInformation.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/ThrowableInformation.java?view=diff&rev=510214&r1=510213&r2=510214
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/ThrowableInformation.java
(original)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/ThrowableInformation.java
Wed Feb 21 13:07:23 2007
@@ -16,9 +16,12 @@
 
 package org.apache.log4j.spi;
 
-import java.io.Writer;
+import java.io.IOException;
+import java.io.LineNumberReader;
 import java.io.PrintWriter;
-import java.util.Vector;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
 
 /**
   * ThrowableInformation is log4j's internal representation of
@@ -39,7 +42,7 @@
 
   private transient Throwable throwable;
   private String[] rep;
-  
+
   public
   ThrowableInformation(Throwable throwable) {
     this.throwable = throwable;
@@ -49,105 +52,33 @@
   Throwable getThrowable() {
     return throwable;
   }
-  
+
   public
   String[] getThrowableStrRep() {
     if(rep != null) {
       return (String[]) rep.clone();
     } else {
-      VectorWriter vw = new VectorWriter();
-      throwable.printStackTrace(vw);
-      rep = vw.toStringArray();
-      return rep;
+      StringWriter sw = new StringWriter();
+      PrintWriter pw = new PrintWriter(sw);
+      throwable.printStackTrace(pw);
+      pw.flush();
+      LineNumberReader reader = new LineNumberReader(
+              new StringReader(sw.toString()));
+      ArrayList lines = new ArrayList();
+      try {
+        String line = reader.readLine();
+        while(line != null) {
+          lines.add(line);
+          line = reader.readLine();
+        }
+      } catch(IOException ex) {
+          lines.add(ex.toString());
+      }
+      rep = new String[lines.size()];
+      lines.toArray(rep);
     }
+    return rep;
   }
 }
 
-/**
-  * VectorWriter is a seemingly trivial implemtantion of PrintWriter.
-  * The throwable instance that we are trying to represent is asked to
-  * print itself to a VectorWriter. 
-  *
-  * By our design choice, our string representation of the throwable
-  * does not contain any line separators. It follows that println()
-  * methods of VectorWriter ignore the 'ln' part.
-  * */
-class VectorWriter extends PrintWriter {
-    
-  private Vector v;
-  
-  VectorWriter() {
-    super(new NullWriter());
-    v = new Vector();
-  }
-
-  public void print(Object o) {      
-    v.addElement(String.valueOf(o));
-  }
-  
-  public void print(char[] chars) {
-    v.addElement(new String(chars));
-  }
-  
-  public void print(String s) {
-    v.addElement(s);
-  }
-
-  public void println(Object o) {      
-    v.addElement(String.valueOf(o));
-  }
-  
-  // JDK 1.1.x apprenly uses this form of println while in
-  // printStackTrace()
-  public
-  void println(char[] chars) {
-    v.addElement(new String(chars));
-  }
-  
-  public  
-  void println(String s) {
-    v.addElement(s);
-  }
-
-  public void write(char[] chars) {
-    v.addElement(new String(chars));
-  }
-
-  public void write(char[] chars, int off, int len) {
-    v.addElement(new String(chars, off, len));
-  }
-
-  public void write(String s, int off, int len) {
-    v.addElement(s.substring(off, off+len));
-  }
-
-  public void write(String s) {
-     v.addElement(s);
-  }
-
-  public String[] toStringArray() {
-    int len = v.size();
-    String[] sa = new String[len];
-    for(int i = 0; i < len; i++) {
-      sa[i] = (String) v.elementAt(i);
-    }
-    return sa;
-  }
-
-}  
-
-class NullWriter extends Writer {    
-  
-  public void close() {
-    // blank
-  }
-
-  public void flush() {
-    // blank
-  }
-
-  public void write(char[] cbuf, int off, int len) {
-    // blank
-  }
-}
 

Added: logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/VectorWriter.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/VectorWriter.java?view=auto&rev=510214
==============================================================================
--- logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/VectorWriter.java (added)
+++ logging/log4j/branches/v1_2-branch/src/java/org/apache/log4j/spi/VectorWriter.java Wed
Feb 21 13:07:23 2007
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.log4j.spi;
+
+import java.io.PrintWriter;
+import java.util.Vector;
+
+/**
+  * VectorWriter is an obsolete class provided only for
+  *  binary compatibility with earlier versions of log4j and should not be used.
+  *
+  * @deprecated
+  */
+class VectorWriter extends PrintWriter {
+
+  private Vector v;
+
+    /**
+     * @deprecated
+     */
+  VectorWriter() {
+    super(new NullWriter());
+    v = new Vector();
+  }
+
+  public void print(Object o) {
+    v.addElement(String.valueOf(o));
+  }
+
+  public void print(char[] chars) {
+    v.addElement(new String(chars));
+  }
+
+  public void print(String s) {
+    v.addElement(s);
+  }
+
+  public void println(Object o) {
+    v.addElement(String.valueOf(o));
+  }
+
+  // JDK 1.1.x apprenly uses this form of println while in
+  // printStackTrace()
+  public
+  void println(char[] chars) {
+    v.addElement(new String(chars));
+  }
+
+  public
+  void println(String s) {
+    v.addElement(s);
+  }
+
+  public void write(char[] chars) {
+    v.addElement(new String(chars));
+  }
+
+  public void write(char[] chars, int off, int len) {
+    v.addElement(new String(chars, off, len));
+  }
+
+  public void write(String s, int off, int len) {
+    v.addElement(s.substring(off, off+len));
+  }
+
+  public void write(String s) {
+     v.addElement(s);
+  }
+
+  public String[] toStringArray() {
+    int len = v.size();
+    String[] sa = new String[len];
+    for(int i = 0; i < len; i++) {
+      sa[i] = (String) v.elementAt(i);
+    }
+    return sa;
+  }
+
+}
+

Modified: logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java?view=diff&rev=510214&r1=510213&r2=510214
==============================================================================
--- logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java
(original)
+++ logging/log4j/branches/v1_2-branch/tests/src/java/org/apache/log4j/spi/ThrowableInformationTest.java
Wed Feb 21 13:07:23 2007
@@ -1,9 +1,10 @@
 /*
- * Copyright 1999,2005 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
@@ -13,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.log4j.spi;
 
 import junit.framework.TestCase;
@@ -70,16 +70,11 @@
     public void testOverriddenBehavior() {
         ThrowableInformation ti = new ThrowableInformation(new OverriddenThrowable());
         String[] rep = ti.getThrowableStrRep();
-        assertEquals(9, rep.length);
-        assertEquals("print(Object)", rep[0]);
-        assertEquals("print(char[])", rep[1]);
-        assertEquals("print(String)", rep[2]);
-        assertEquals("println(Object)", rep[3]);
-        assertEquals("println(char[])", rep[4]);
-        assertEquals("println(String)", rep[5]);
-        assertEquals("write(char[])", rep[6]);
-        assertEquals("ite(char", rep[7]);
-        assertEquals("ite(Stri", rep[8]);
+        assertEquals(4, rep.length);
+        assertEquals("print(Object)print(char[])print(String)println(Object)", rep[0]);
+        assertEquals("println(char[])", rep[1]);
+        assertEquals("println(String)", rep[2]);
+        assertEquals("write(char[])ite(charite(Stri", rep[3]);
     }
 
     /**
@@ -122,27 +117,21 @@
     public void testNotOverriddenBehavior() {
         ThrowableInformation ti = new ThrowableInformation(new NotOverriddenThrowable());
         String[] rep = ti.getThrowableStrRep();
-        //
-        //   The results under log4j 1.2.14 could change depending on implementation
-        //     of java.io.PrintWriter
-        //
-        assertEquals(10, rep.length);
-        assertEquals(String.valueOf(true), rep[0]);
-//      Calls to print(char) are discarded
-//         assertEquals("a", rep[1]);
-        assertEquals(String.valueOf(1), rep[1]);
-        assertEquals(String.valueOf(2L), rep[2]);
-        assertEquals(String.valueOf(Float.MAX_VALUE), rep[3]);
-        assertEquals(String.valueOf(Double.MIN_VALUE), rep[4]);
-        assertEquals(String.valueOf(true), rep[5]);
-//      Calls to println(char) are discarded
-//        assertEquals("a", rep[7]);
-        assertEquals(String.valueOf(1), rep[6]);
-        assertEquals(String.valueOf(2L), rep[7]);
-        assertEquals(String.valueOf(Float.MAX_VALUE), rep[8]);
-        assertEquals(String.valueOf(Double.MIN_VALUE), rep[9]);
-//        output to write(int) are discarded
-//        assertEquals("C", rep[12]);
+        assertEquals(7, rep.length);
+        StringBuffer buf = new StringBuffer(String.valueOf(true));
+        buf.append('a');
+        buf.append(String.valueOf(1));
+        buf.append(String.valueOf(2L));
+        buf.append(String.valueOf(Float.MAX_VALUE));
+        buf.append(String.valueOf(Double.MIN_VALUE));
+        buf.append(String.valueOf(true));
+        assertEquals(buf.toString(), rep[0]);
+        assertEquals("a", rep[1]);
+        assertEquals(String.valueOf(1), rep[2]);
+        assertEquals(String.valueOf(2L), rep[3]);
+        assertEquals(String.valueOf(Float.MAX_VALUE), rep[4]);
+        assertEquals(String.valueOf(Double.MIN_VALUE), rep[5]);
+        assertEquals("C", rep[6]);
     }
 
     /**
@@ -177,11 +166,10 @@
     public void testNull() {
         ThrowableInformation ti = new ThrowableInformation(new NullThrowable());
         String[] rep = ti.getThrowableStrRep();
-        assertEquals(4, rep.length);
-        assertEquals(String.valueOf((Object) null), rep[0]);
-        assertNull(rep[1]);
-        assertEquals(String.valueOf((Object) null), rep[2]);
-        assertNull(rep[3]);
+        assertEquals(2, rep.length);
+        String nullStr = String.valueOf((Object) null);
+        assertEquals(nullStr + nullStr + nullStr, rep[0]);
+        assertEquals(nullStr, rep[1]);
     }
 
     /**
@@ -212,5 +200,87 @@
         ThrowableInformation ti = new ThrowableInformation(new EmptyThrowable());
         String[] rep = ti.getThrowableStrRep();
         assertEquals(0, rep.length);
+    }
+
+    /**
+     * Custom throwable that emits a specified string in printStackTrace.
+     */
+    private static final class StringThrowable extends Throwable {
+        /**
+         * Stack trace.
+         */
+        private final String stackTrace;
+        /**
+         * Create new instance.
+         * @param trace stack trace.
+         */
+        public StringThrowable(final String trace) {
+            stackTrace = trace;
+        }
+
+        /**
+         * Print stack trace.
+         *
+         * @param s print writer.
+         */
+        public void printStackTrace(final PrintWriter s) {
+            s.print(stackTrace);
+        }
+    }
+
+    /**
+     * Test capturing stack trace from throwable that just has a line feed.
+     */
+    public void testLineFeed() {
+        ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\n"));
+        String[] rep = ti.getThrowableStrRep();
+        assertEquals(1, rep.length);
+        assertEquals("", rep[0]);
+    }
+
+    /**
+     * Test capturing stack trace from throwable that just has a carriage return.
+     */
+    public void testCarriageReturn() {
+        ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\r"));
+        String[] rep = ti.getThrowableStrRep();
+        assertEquals(1, rep.length);
+        assertEquals("", rep[0]);
+    }
+
+    /**
+     * Test parsing of line breaks.
+     */
+    public void testParsing() {
+        ThrowableInformation ti = new ThrowableInformation(
+                new StringThrowable("Line1\rLine2\nLine3\r\nLine4\n\rLine6"));
+        String[] rep = ti.getThrowableStrRep();
+        assertEquals(6, rep.length);
+        assertEquals("Line1", rep[0]);
+        assertEquals("Line2", rep[1]);
+        assertEquals("Line3", rep[2]);
+        assertEquals("Line4", rep[3]);
+        assertEquals("", rep[4]);
+        assertEquals("Line6", rep[5]);
+    }
+
+    /**
+     * Test capturing stack trace from throwable that a line feed followed by blank.
+     */
+    public void testLineFeedBlank() {
+        ThrowableInformation ti = new ThrowableInformation(new StringThrowable("\n "));
+        String[] rep = ti.getThrowableStrRep();
+        assertEquals(2, rep.length);
+        assertEquals("", rep[0]);
+        assertEquals(" ", rep[1]);
+    }
+
+    /**
+     * Test that getThrowable returns the throwable provided to the constructor.
+     */
+    public void testGetThrowable() {
+        Throwable t = new StringThrowable("Hello, World");
+        ThrowableInformation ti = new ThrowableInformation(t);
+        assertSame(t, ti.getThrowable());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message