[logback-dev] svn commit: r1695 - in logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic: . pattern spi

noreply.ceki at qos.ch noreply.ceki at qos.ch
Wed May 14 15:26:59 CEST 2008


Author: ceki
Date: Wed May 14 15:26:59 2008
New Revision: 1695

Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicGlobal.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/ThrowableInformationConverter.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableInformation.java

Log:
- refactoring of ThrowableInformation.
 Code responsible for extracting the string array has been moved into 
 the ThorableToStringArray class. This allows StatusPrinter code
 (in lb-core) to compute string representation of throwables without creating
 a ThrowableInformationConverter (in lb-classic).
 

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicGlobal.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicGlobal.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicGlobal.java	Wed May 14 15:26:59 2008
@@ -11,7 +11,6 @@
 
 public class ClassicGlobal {
   static public final char LOGGER_SEPARATOR = '.';
-  static public final String CAUSED_BY = "Caused by: ";
   static public final char DOT = '.';
   static public final String USER_MDC_KEY = "user";
   

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/ThrowableInformationConverter.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/ThrowableInformationConverter.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/pattern/ThrowableInformationConverter.java	Wed May 14 15:26:59 2008
@@ -132,7 +132,7 @@
     for (int i = 1; i < length; i++) {
       String string = stringRep[i];
 
-      if (string.startsWith(ch.qos.logback.classic.ClassicGlobal.CAUSED_BY)) {
+      if (string.startsWith(CoreGlobal.CAUSED_BY)) {
         // nothing
       } else if (Character.isDigit(string.charAt(0))) {
         buf.append("\t... ");

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableInformation.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableInformation.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableInformation.java	Wed May 14 15:26:59 2008
@@ -11,7 +11,7 @@
 
 import java.util.Arrays;
 
-import ch.qos.logback.classic.ClassicGlobal;
+import ch.qos.logback.core.helpers.ThrowableToStringArray;
 
 public class ThrowableInformation implements java.io.Serializable {
 
@@ -21,80 +21,13 @@
 
   public ThrowableInformation(Throwable throwable) {
     this.throwable = throwable;
-    sa = extractStringRep(throwable, null);
+    sa = ThrowableToStringArray.extractStringRep(throwable, null);
   }
 
   public Throwable getThrowable() {
     return throwable;
   }
   
-  public String[] extractStringRep(Throwable t, StackTraceElement[] parentSTE) {
-    String[] result;
-
-    StackTraceElement[] ste = t.getStackTrace();
-    final int commonFrames = findCommonFrames(ste, parentSTE);
-
-    final String[] firstArray;
-    if (commonFrames == 0) {
-      firstArray = new String[ste.length + 1];
-    } else {
-      firstArray = new String[ste.length - commonFrames + 2];
-    }
-
-    String prefix = "";
-    if (parentSTE != null) {
-      prefix = ClassicGlobal.CAUSED_BY;
-    }
-
-    firstArray[0] = prefix + t.getClass().getName();
-    if (t.getMessage() != null) {
-      firstArray[0] += ": " + t.getMessage();
-    }
-
-    for (int i = 0; i < (ste.length - commonFrames); i++) {
-      firstArray[i + 1] = ste[i].toString();
-    }
-
-    if (commonFrames != 0) {
-      firstArray[firstArray.length - 1] = commonFrames
-          + " common frames omitted";
-    }
-
-    Throwable cause = t.getCause();
-    if (cause != null) {
-      final String[] causeArray = extractStringRep(cause, ste);
-      String[] tmp = new String[firstArray.length + causeArray.length];
-      System.arraycopy(firstArray, 0, tmp, 0, firstArray.length);
-      System
-          .arraycopy(causeArray, 0, tmp, firstArray.length, causeArray.length);
-      result = tmp;
-    } else {
-      result = firstArray;
-    }
-    return result;
-  }
-
-  private int findCommonFrames(StackTraceElement[] ste,
-      StackTraceElement[] parentSTE) {
-    if (parentSTE == null) {
-      return 0;
-    }
-
-    int steIndex = ste.length - 1;
-    int parentIndex = parentSTE.length - 1;
-    int count = 0;
-    while (steIndex >= 0 && parentIndex >= 0) {
-      if (ste[steIndex].equals(parentSTE[parentIndex])) {
-        count++;
-      } else {
-        break;
-      }
-      steIndex--;
-      parentIndex--;
-    }
-    return count;
-  }
-
   /**
    * The string representation of the exceptopn (throwable) that this object
    * represents.



More information about the logback-dev mailing list