[logback-dev] svn commit: r2182 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/spi test/java/ch/qos/logback/classic/corpus test/java/ch/qos/logback/classic/corpusTest test/java/ch/qos/logback/classic/net/testObjectBuilders test/java/ch/qos/logback/classic/spi

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Mar 5 20:55:12 CET 2009


Author: ceki
Date: Thu Mar  5 20:55:12 2009
New Revision: 2182

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java
      - copied, changed from r2179, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMakerUtil.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/TextFileUtilTest.java
      - copied, changed from r2179, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/FileToWord.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java
Removed:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableDataPointUtil.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMakerUtil.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/FileToWord.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/testObjectBuilders/LoggingEventExtBuilder.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/LoggingEventExt.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ILoggingEvent.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventVO.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/StackTraceElementProxy.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyUtil.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyVO.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/TextFileUtil.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/CallerDataTest.java

Log:
ongoing work on the corpus

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java	Thu Mar  5 20:55:12 2009
@@ -12,17 +12,21 @@
 public class ClassicConstants {
   static public final char LOGGER_SEPARATOR = '.';
   static public final String USER_MDC_KEY = "user";
-  
+
   public static final String LOGBACK_CONTEXT_SELECTOR = "logback.ContextSelector";
   public static String JNDI_CONFIGURATION_RESOURCE = "java:comp/env/logback/configuration-resource";
   public static String JNDI_CONTEXT_NAME = "java:comp/env/logback/context-name";
-  
-  
+
   /**
-   * The maximum number of package separators (dots) that abbreviation algorithms
-   * can handle. Class or logger names  with more separators will have their first
-   * MAX_DOTS parts shortened.
+   * The maximum number of package separators (dots) that abbreviation
+   * algorithms can handle. Class or logger names with more separators will have
+   * their first MAX_DOTS parts shortened.
    * 
    */
   public static final int MAX_DOTS = 16;
+
+  /**
+   * The default stack data depth computed during caller data extraction.
+   */
+  public static final int DEFAULT_MAX_CALLEDER_DATA_DEPTH = 8;
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java	Thu Mar  5 20:55:12 2009
@@ -62,6 +62,8 @@
   private final TurboFilterList turboFilterList = new TurboFilterList();
   private boolean packagingDataEnabled = true; 
   
+  private int maxCallerDataDepth = ClassicConstants.DEFAULT_MAX_CALLEDER_DATA_DEPTH;
+  
   boolean started = false;
 
   public LoggerContext() {
@@ -334,5 +336,13 @@
   public String toString() {
     return this.getClass().getName() + "[" + getName() + "]";
   }
+
+  public int getMaxCallerDataDepth() {
+    return maxCallerDataDepth;
+  }
+
+  public void setMaxCallerDataDepth(int maxCallerDataDepth) {
+    this.maxCallerDataDepth = maxCallerDataDepth;
+  }
   
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/CallerData.java	Thu Mar  5 20:55:12 2009
@@ -28,21 +28,20 @@
 
   // All logger call's in log4j-over-slf4j use the Category class
   private static final String LOG4J_CATEGORY = "org.apache.log4j.Category";
-  
+
   /**
    * When caller information is not available this constant is used for the line
    * number.
    */
   public static final int LINE_NA = -1;
 
-  
-  public static String CALLER_DATA_NA = "?#?:?"+CoreConstants.LINE_SEPARATOR;
+  public static String CALLER_DATA_NA = "?#?:?" + CoreConstants.LINE_SEPARATOR;
 
   /**
    * This value is returned in case no caller data could be extracted.
    */
   public static CallerData[] EMPTY_CALLER_DATA_ARRAY = new CallerData[0];
-  
+
   /**
    * Caller's line number.
    */
@@ -64,7 +63,7 @@
   String methodName;
 
   boolean nativeMethod = false;
-  
+
   public CallerData(String fileName, String className, String methodName,
       int lineNumber) {
     this.fileName = fileName;
@@ -82,50 +81,59 @@
   }
 
   /**
-   * Extract caller data information as an array based on a Throwable passed as parameter
+   * Extract caller data information as an array based on a Throwable passed as
+   * parameter
    */
-  public static CallerData[] extract(Throwable t, String fqnOfInvokingClass) {
+  public static CallerData[] extract(Throwable t, String fqnOfInvokingClass,
+      final int maxDepth) {
     if (t == null) {
       return null;
     }
 
     StackTraceElement[] steArray = t.getStackTrace();
     CallerData[] callerDataArray;
-    
+
     int found = LINE_NA;
     for (int i = 0; i < steArray.length; i++) {
-      if(isDirectlyInvokingClass(steArray[i].getClassName(), fqnOfInvokingClass)) {
+      if (isDirectlyInvokingClass(steArray[i].getClassName(),
+          fqnOfInvokingClass)) {
         // the caller is assumed to be the next stack frame, hence the +1.
         found = i + 1;
       } else {
-        if(found != LINE_NA) {
+        if (found != LINE_NA) {
           break;
         }
       }
     }
 
     // we failed to extract caller data
-    if(found == LINE_NA) {
+    if (found == LINE_NA) {
       return EMPTY_CALLER_DATA_ARRAY;
     }
-    
-    callerDataArray = new CallerData[steArray.length - found];
-    for (int i = found; i < steArray.length; i++) {
-      callerDataArray[i-found] = new CallerData(steArray[i]);
+
+    int availableDepth = steArray.length - found;
+    int desiredDepth = maxDepth < (availableDepth) ? maxDepth : availableDepth;
+
+    callerDataArray = new CallerData[desiredDepth];
+    for (int i = 0; i < desiredDepth; i++) {
+      callerDataArray[i] = new CallerData(steArray[found+i]);
     }
     return callerDataArray;
   }
-  
-  public static boolean isDirectlyInvokingClass(String currentClass, String fqnOfInvokingClass) {
-    // the check for org.apachje.log4j.Category class is intended to support log4j-over-slf4j
+
+  public static boolean isDirectlyInvokingClass(String currentClass,
+      String fqnOfInvokingClass) {
+    // the check for org.apachje.log4j.Category class is intended to support
+    // log4j-over-slf4j
     // it solves http://bugzilla.slf4j.org/show_bug.cgi?id=66
-    if(currentClass.equals(fqnOfInvokingClass) || currentClass.equals(LOG4J_CATEGORY)) {
+    if (currentClass.equals(fqnOfInvokingClass)
+        || currentClass.equals(LOG4J_CATEGORY)) {
       return true;
     } else {
       return false;
     }
   }
-  
+
   public boolean equals(Object o) {
     // LogLog.info("equals called");
     if (this == o) {
@@ -173,8 +181,7 @@
   /**
    * Return the file name of the caller.
    * 
-   * <p>
-   * This information is not always available.
+   * <p> This information is not always available.
    */
   public String getFileName() {
     return fileName;
@@ -183,8 +190,7 @@
   /**
    * Returns the line number of the caller.
    * 
-   * <p>
-   * This information is not always available.
+   * <p> This information is not always available.
    */
   public int getLineNumber() {
     return lineNumber;

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ILoggingEvent.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ILoggingEvent.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ILoggingEvent.java	Thu Mar  5 20:55:12 2009
@@ -10,23 +10,54 @@
  * The core interface in logback-classic.
  * 
  * @author Ceki G&uuml;lc&uuml;
+ * @since 0.9.16
  */
 public interface ILoggingEvent {
 
   public String getThreadName();
+
   public Level getLevel();
+
   public String getMessage();
+
   public Object[] getArgumentArray();
+
   public String getFormattedMessage();
 
   public String getLoggerName();
+
   public LoggerContextVO getLoggerContextVO();
 
   public IThrowableProxy getThrowableProxy();
+
+  /**
+   * Return {@link CallerData} associated with this event. Note that calling
+   * this event may trigger the computation of caller data.
+   * 
+   * @return the CallerData associated with this event.
+   * 
+   * @see #hasCallerData()
+   */
   public CallerData[] getCallerData();
+
+  /**
+   * If this event has caller data, then true is returned. Otherwise the
+   * returned value is null.
+   * 
+   * <p>Logback components wishing to use caller data if available without
+   * causing it to be computed can invoke this method before invoking
+   * {@link #getCallerData()}.
+   * 
+   * @return whether this event has caller data
+   */
+  public boolean hasCallerData();
+
   public Marker getMarker();
+
   public Map<String, String> getMDCPropertyMap();
+
   public long getTimeStamp();
+
   public void prepareForDeferredProcessing();
 
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	Thu Mar  5 20:55:12 2009
@@ -51,10 +51,9 @@
    */
   private String threadName;
 
-  
   private String loggerName;
-  private LoggerContextVO loggerContextRemoteView;
-
+  private LoggerContext loggerContext;
+  private LoggerContextVO loggerContextVO;
   
   /**
    * Level of logging event.
@@ -87,7 +86,7 @@
    * created.
    */
   private long timeStamp;
-  
+
   public LoggingEvent() {
   }
 
@@ -95,15 +94,16 @@
       Throwable throwable, Object[] argArray) {
     this.fqnOfLoggerClass = fqcn;
     this.loggerName = logger.getName();
-    this.loggerContextRemoteView = logger.getLoggerRemoteView().getLoggerContextView();
+    this.loggerContext = logger.getLoggerContext();
+    this.loggerContextVO = loggerContext.getLoggerContextRemoteView();
     this.level = level;
-    
+
     this.message = message;
 
     if (throwable != null) {
       this.throwableProxy = new ThrowableProxy(throwable);
       LoggerContext lc = logger.getLoggerContext();
-      if(lc.isPackagingDataEnabled()) {
+      if (lc.isPackagingDataEnabled()) {
         this.throwableProxy.calculatePackagingData();
       }
     }
@@ -118,7 +118,7 @@
         .getMDCAdapter();
     mdcPropertyMap = logbackMDCAdapter.getPropertyMap();
   }
-  
+
   public void setArgumentArray(Object[] argArray) {
     if (this.argumentArray != null) {
       throw new IllegalStateException("argArray has been already set");
@@ -137,11 +137,11 @@
   public String getLoggerName() {
     return loggerName;
   }
-  
+
   public void setLoggerName(String loggerName) {
     this.loggerName = loggerName;
   }
-  
+
   public String getThreadName() {
     if (threadName == null) {
       threadName = (Thread.currentThread()).getName();
@@ -198,11 +198,11 @@
   }
 
   public LoggerContextVO getLoggerContextVO() {
-    return loggerContextRemoteView;
+    return loggerContextVO;
   }
 
-  public void setLoggerContextRemoteView(LoggerContextVO loggerContextRemoteView) {
-    this.loggerContextRemoteView = loggerContextRemoteView;
+  public void setLoggerContextRemoteView(LoggerContextVO loggerContextVO) {
+    this.loggerContextVO = loggerContextVO;
   }
 
   public String getMessage() {
@@ -242,15 +242,17 @@
    * caller information. </p>
    */
   public CallerData[] getCallerData() {
-    // we rely on the fact that fqnOfLoggerClass does not survive
-    // serialization
-    if (callerDataArray == null && fqnOfLoggerClass != null) {
-      callerDataArray = CallerData.extract(new Throwable(), fqnOfLoggerClass);
+    if (callerDataArray == null) {
+      callerDataArray = CallerData.extract(new Throwable(), fqnOfLoggerClass, loggerContext.getMaxCallerDataDepth());
     }
     return callerDataArray;
   }
 
-  public void setCallerInformation(CallerData[] callerDataArray) {
+  public boolean hasCallerData() {
+    return (callerDataArray != null);
+  }
+
+  public void setCallerData(CallerData[] callerDataArray) {
     this.callerDataArray = callerDataArray;
   }
 
@@ -267,10 +269,9 @@
   }
 
   public long getContextBirthTime() {
-    return loggerContextRemoteView.getBirthTime();
+    return loggerContextVO.getBirthTime();
   }
 
-  
   // computer formatted lazy as suggested in
   // http://jira.qos.ch/browse/LBCLASSIC-47
   public String getFormattedMessage() {

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventVO.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventVO.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventVO.java	Thu Mar  5 20:55:12 2009
@@ -13,6 +13,12 @@
 
 // http://www.riehle.org/computer-science/research/1998/ubilab-tr-1998-10-1.html
 
+/**
+ * A read-only and serializable implementation of {@link ILoggingEvent}.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ * @since 0.9.16
+ */
 public class LoggingEventVO implements ILoggingEvent, Serializable {
 
   private static final long serialVersionUID = 6553722650255690312L;
@@ -23,7 +29,7 @@
   private String threadName;
   private String loggerName;
   private LoggerContextVO loggerContextVO;
-  
+
   private transient Level level;
   private String message;
 
@@ -39,7 +45,6 @@
   private Marker marker;
   private Map<String, String> mdcPropertyMap;
   private long timeStamp;
-  
 
   public static LoggingEventVO build(ILoggingEvent le) {
     LoggingEventVO ledo = new LoggingEventVO();
@@ -51,7 +56,7 @@
     ledo.argumentArray = (le.getArgumentArray());
     ledo.marker = le.getMarker();
     ledo.mdcPropertyMap = le.getMDCPropertyMap();
-    ledo.setTimeStamp(le.getTimeStamp());
+    ledo.timeStamp = le.getTimeStamp();
     ledo.throwableProxy = ThrowableProxyVO.build(le.getThrowableProxy());
     return ledo;
   }
@@ -63,11 +68,11 @@
   public LoggerContextVO getLoggerContextVO() {
     return loggerContextVO;
   }
-  
+
   public String getLoggerName() {
     return loggerName;
   }
-  
+
   public Level getLevel() {
     return level;
   }
@@ -102,6 +107,10 @@
     return callerDataArray;
   }
 
+  public boolean hasCallerData() {
+    return callerDataArray != null;
+  }
+
   public Marker getMarker() {
     return marker;
   }
@@ -110,10 +119,6 @@
     return timeStamp;
   }
 
-  public void setTimeStamp(long timeStamp) {
-    this.timeStamp = timeStamp;
-  }
-
 
 
   public long getContextBirthTime() {
@@ -200,7 +205,6 @@
     } else if (!loggerName.equals(other.loggerName))
       return false;
 
-    
     if (threadName == null) {
       if (other.threadName != null)
         return false;

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/StackTraceElementProxy.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/StackTraceElementProxy.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/StackTraceElementProxy.java	Thu Mar  5 20:55:12 2009
@@ -31,7 +31,10 @@
     return ste;
   }
   
-  void setClassPackagingData(ClassPackagingData cpd) {
+  public void setClassPackagingData(ClassPackagingData cpd) {
+    if(this.cpd != null) {
+      throw new IllegalStateException("Packaging data has been already set");
+    }
     this.cpd = cpd;
   }
 

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxy.java	Thu Mar  5 20:55:12 2009
@@ -104,24 +104,11 @@
     for (StackTraceElementProxy step : stackTraceElementProxyArray) {
       String string = step.toString();
       builder.append(string);
-      extraData(builder, step);
+      ThrowableProxyUtil.appendPackagingData(builder, step);
       builder.append(CoreConstants.LINE_SEPARATOR);
     }
     System.out.println(builder.toString());
   }
 
-  protected void extraData(StringBuilder builder, StackTraceElementProxy step) {
-    if (step != null) {
-      ClassPackagingData cpd = step.getClassPackagingData();
-      if (cpd != null) {
-        if (!cpd.isExact()) {
-          builder.append(" ~[");
-        } else {
-          builder.append(" [");
-        }
-        builder.append(cpd.getCodeLocation()).append(':').append(
-            cpd.getVersion()).append(']');
-      }
-    }
-  }
+
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyUtil.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyUtil.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyUtil.java	Thu Mar  5 20:55:12 2009
@@ -20,8 +20,6 @@
  */
 public class ThrowableProxyUtil {
 
-  //static final ThrowableDataPoint[] TEMPLATE_ARRAY = new ThrowableDataPoint[0];
-
   static public void build(ThrowableProxy nestedTP, Throwable nestedThrowable,
       ThrowableProxy parentTP) {
 
@@ -68,25 +66,42 @@
     return count;
   }
 
-  static String asString(IThrowableProxy tp) {
+  static public String asString(IThrowableProxy tp) {
     StringBuilder sb = new StringBuilder();
 
     while (tp != null) {
-     
       printFirstLine(sb, tp);
       printSTEPArray(sb, tp);
       tp = tp.getCause();
     }
     return sb.toString();
-
   }
 
+  static void appendPackagingData(StringBuilder builder, StackTraceElementProxy step) {
+    if (step != null) {
+      ClassPackagingData cpd = step.getClassPackagingData();
+      if (cpd != null) {
+        if (!cpd.isExact()) {
+          builder.append(" ~[");
+        } else {
+          builder.append(" [");
+        }
+   
+        builder.append(cpd.getCodeLocation()).append(':').append(
+            cpd.getVersion()).append(']');
+      }
+    }
+  }
+  
   static public void printSTEPArray(StringBuilder sb, IThrowableProxy tp) {
     StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
     int commonFrames = tp.getCommonFrames();
+
     for (int i = 0; i < stepArray.length - commonFrames; i++) {
       StackTraceElementProxy step = stepArray[i];
-      sb.append(step.toString()).append(CoreConstants.LINE_SEPARATOR);
+      sb.append(step.toString());
+      appendPackagingData(sb, step);
+      sb.append(CoreConstants.LINE_SEPARATOR);
     }
     
     if (commonFrames > 0) {
@@ -96,12 +111,12 @@
     
   }
 
-  static public void printFirstLine(StringBuilder sb, IThrowableProxy tp) {
+  static public void printFirstLine(StringBuilder buf, IThrowableProxy tp) {
     int commonFrames = tp.getCommonFrames();
     if (commonFrames > 0) {
-      sb.append(CoreConstants.CAUSED_BY);
+      buf.append(CoreConstants.CAUSED_BY);
     }
-    sb.append(tp.getClassName()).append(": ").append(tp.getMessage());
-    sb.append(CoreConstants.LINE_SEPARATOR);
+    buf.append(tp.getClassName()).append(": ").append(tp.getMessage());
+    buf.append(CoreConstants.LINE_SEPARATOR);
   }
 }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyVO.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyVO.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ThrowableProxyVO.java	Thu Mar  5 20:55:12 2009
@@ -94,7 +94,4 @@
     }
     return tpvo;
   }
-
-
-
 }

Copied: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java (from r2179, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMakerUtil.java)
==============================================================================
--- /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMakerUtil.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/CorpusMaker.java	Thu Mar  5 20:55:12 2009
@@ -9,13 +9,24 @@
  */
 package ch.qos.logback.classic.corpus;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.Random;
 
 import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.spi.ClassPackagingData;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.classic.spi.LoggerContextVO;
+import ch.qos.logback.classic.spi.PubLoggingEventVO;
+import ch.qos.logback.classic.spi.StackTraceElementProxy;
+import ch.qos.logback.classic.spi.ThrowableProxy;
+import ch.qos.logback.classic.spi.ThrowableProxyVO;
 
-public class CorpusMakerUtil {
+public class CorpusMaker {
 
+ static final int STANDARD_CORPUS_SIZE = 100*1000;
+  
   // level distribution is determined by the following table
   // it corresponds to TRACE 20%, DEBUG 30%, INFO 30%, WARN 10%,
   // ERROR 10%. See also getRandomLevel() method.
@@ -35,15 +46,88 @@
   static final int AVERAGE_MESSAGE_WORDS = 8;
   static final int STD_DEV_FOR_MESSAGE_WORDS = 4;
 
+  static final int THREAD_POOL_SIZE = 10;
+
   final Random random;
   List<String> worldList;
+  String[] threadNamePool;
 
-  CorpusMakerUtil(long seed, List<String> worldList) {
+  public CorpusMaker(long seed, List<String> worldList) {
     random = new Random(seed);
     this.worldList = worldList;
+    buildThreadNamePool();
+  }
+
+  void buildThreadNamePool() {
+    threadNamePool = new String[THREAD_POOL_SIZE];
+    for (int i = 0; i < THREAD_POOL_SIZE; i++) {
+      threadNamePool[i] = "CorpusMakerThread-" + i;
+    }
+  }
+
+  static public ILoggingEvent[] makeStandardCorpus() throws IOException {
+    List<String> worldList = TextFileUtil
+        .toWords("src/test/input/corpus/origin_of_species.txt");
+    CorpusMaker corpusMaker = new CorpusMaker(10, worldList);
+    return corpusMaker.make(STANDARD_CORPUS_SIZE);
+  }
+
+  public ILoggingEvent[] make(int n) {
+
+    LoggerContextVO lcVO = getRandomlyNamedLoggerContextVO();
+
+    PubLoggingEventVO[] plevoArray = new PubLoggingEventVO[n];
+    for (int i = 0; i < n; i++) {
+      PubLoggingEventVO e = new PubLoggingEventVO();
+      plevoArray[i] = e;
+      e.timeStamp = getRandomLong();
+      e.loggerName = getRandomLoggerName();
+      e.level = getRandomLevel();
+      MessageEntry me = getRandomMessageEntry();
+      e.message = me.message;
+      e.argumentArray = me.argumentArray;
+      e.loggerContextVO = lcVO;
+      Throwable t = getRandomThrowable(e.level);
+      if (t != null) {
+        e.throwableProxy = ThrowableProxyVO.build(new ThrowableProxy(t));
+        pupulateWithPackagingData(e.throwableProxy
+            .getStackTraceElementProxyArray());
+      }
+      e.threadName = getRandomThreadName();
+    }
+    return plevoArray;
+  }
+
+  void pupulateWithPackagingData(StackTraceElementProxy[] stepArray) {
+    int i = 0;
+    for (StackTraceElementProxy step : stepArray) {
+      String identifier = "na";
+      String version = "na";
+      if (i++ % 2 == 0) {
+        identifier = getRandomJavaIdentifier();
+        version = getRandomJavaIdentifier();
+      }
+      ClassPackagingData cpd = new ClassPackagingData(identifier, version);
+      step.setClassPackagingData(cpd);
+    }
 
   }
 
+  LoggerContextVO getRandomlyNamedLoggerContextVO() {
+    LoggerContext lc = new LoggerContext();
+    lc.setName(getRandomJavaIdentifier());
+    return new LoggerContextVO(lc);
+  }
+
+  long getRandomLong() {
+    return random.nextLong();
+  }
+
+  String getRandomThreadName() {
+    int index = random.nextInt(THREAD_POOL_SIZE);
+    return threadNamePool[index];
+  }
+
   String getRandomWord() {
     int size = worldList.size();
     int randomIndex = random.nextInt(size);
@@ -123,14 +207,20 @@
     return 3;
   }
 
+  String getRandomJavaIdentifier() {
+    String w = getRandomWord();
+    w = w.replaceAll("\\p{Punct}", "");
+    return w;
+  }
+
   String getRandomLoggerName() {
     int parts = RandomUtil.gaussianAsPositiveInt(random,
         AVERAGE_LOGGER_NAME_PARTS, STD_DEV_FOR_LOGGER_NAME_PARTS);
     StringBuilder sb = new StringBuilder();
     for (int i = 1; i < parts; i++) {
-      sb.append(getRandomWord()).append('.');
+      sb.append(getRandomJavaIdentifier()).append('.');
     }
-    sb.append(getRandomWord());
+    sb.append(getRandomJavaIdentifier());
     return sb.toString();
   }
 

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/TextFileUtil.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/TextFileUtil.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpus/TextFileUtil.java	Thu Mar  5 20:55:12 2009
@@ -1,17 +1,21 @@
 package ch.qos.logback.classic.corpus;
 
 import java.io.BufferedReader;
+import java.io.FileReader;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
 public class TextFileUtil {
 
-  //FileReader fr = new FileReader(filename);
-  //BufferedReader br = new BufferedReader(fr);
+
+  public static List<String> toWords(String filename) throws IOException {
+    FileReader fr = new FileReader(filename);
+    BufferedReader br = new BufferedReader(fr);
+    return toWords(br);
+  }
   
   public static List<String> toWords(BufferedReader br) throws IOException {
-   
 
     // (\\d+)$
     //String regExp = "^(\\d+) "+ msg +  " ([\\dabcdef-]+)$";

Copied: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/TextFileUtilTest.java (from r2179, /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/FileToWord.java)
==============================================================================
--- /logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/FileToWord.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/corpusTest/TextFileUtilTest.java	Thu Mar  5 20:55:12 2009
@@ -11,7 +11,7 @@
 
 import ch.qos.logback.classic.corpus.TextFileUtil;
 
-public class FileToWord {
+public class TextFileUtilTest {
 
   @Test
   public void smoke() throws IOException {

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/CallerDataTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/CallerDataTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/CallerDataTest.java	Thu Mar  5 20:55:12 2009
@@ -14,7 +14,7 @@
     Throwable t = new Throwable();
     StackTraceElement[] steArray = t.getStackTrace();
     
-    CallerData[] cda = CallerData.extract(t, CallerDataTest.class.getName());
+    CallerData[] cda = CallerData.extract(t, CallerDataTest.class.getName(), 50);
     assertNotNull(cda);
     assertTrue(cda.length > 0);
     assertEquals(steArray.length - 1, cda.length);
@@ -27,7 +27,7 @@
    */
   @Test
   public void testDeferredProcessing() {
-    CallerData[] cda = CallerData.extract(new Throwable(), "com.inexistent.foo");
+    CallerData[] cda = CallerData.extract(new Throwable(), "com.inexistent.foo", 10);
     assertNotNull(cda);
     assertEquals(0, cda.length);
   }

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/PubLoggingEventVO.java	Thu Mar  5 20:55:12 2009
@@ -0,0 +1,227 @@
+package ch.qos.logback.classic.spi;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.Map;
+
+import org.slf4j.Marker;
+import org.slf4j.helpers.MessageFormatter;
+
+import ch.qos.logback.classic.Level;
+
+/**
+ * A read/write  and serializable implementation of {@link ILoggingEvent}.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+
+ */
+public class PubLoggingEventVO implements ILoggingEvent, Serializable {
+
+
+  private static final long serialVersionUID = -3385765861078946218L;
+
+  private static final int NULL_ARGUMENT_ARRAY = -1;
+  private static final String NULL_ARGUMENT_ARRAY_ELEMENT = "NULL_ARGUMENT_ARRAY_ELEMENT";
+
+  public String threadName;
+  public String loggerName;
+  public LoggerContextVO loggerContextVO;
+
+  public transient Level level;
+  public String message;
+
+  private transient String formattedMessage;
+
+  public Object[] argumentArray;
+
+  public ThrowableProxyVO throwableProxy;
+  public CallerData[] callerDataArray;
+  public Marker marker;
+  public Map<String, String> mdcPropertyMap;
+  public long timeStamp;
+
+
+
+  public String getThreadName() {
+    return threadName;
+  }
+
+  public LoggerContextVO getLoggerContextVO() {
+    return loggerContextVO;
+  }
+
+  public String getLoggerName() {
+    return loggerName;
+  }
+
+  public Level getLevel() {
+    return level;
+  }
+
+  public String getMessage() {
+    return message;
+  }
+
+  public String getFormattedMessage() {
+    if (formattedMessage != null) {
+      return formattedMessage;
+    }
+
+    if (argumentArray != null) {
+      formattedMessage = MessageFormatter.arrayFormat(message, argumentArray);
+    } else {
+      formattedMessage = message;
+    }
+
+    return formattedMessage;
+  }
+
+  public Object[] getArgumentArray() {
+    return argumentArray;
+  }
+
+  public IThrowableProxy getThrowableProxy() {
+    return throwableProxy;
+  }
+
+  public CallerData[] getCallerData() {
+    return callerDataArray;
+  }
+
+  public boolean hasCallerData() {
+    return callerDataArray != null;
+  }
+
+  public Marker getMarker() {
+    return marker;
+  }
+
+  public long getTimeStamp() {
+    return timeStamp;
+  }
+
+
+
+  public long getContextBirthTime() {
+    return loggerContextVO.getBirthTime();
+  }
+
+  public LoggerContextVO getContextLoggerRemoteView() {
+    return loggerContextVO;
+  }
+
+  public Map<String, String> getMDCPropertyMap() {
+    return mdcPropertyMap;
+  }
+
+  public void prepareForDeferredProcessing() {
+  }
+
+  private void writeObject(ObjectOutputStream out) throws IOException {
+    out.defaultWriteObject();
+    out.writeInt(level.levelInt);
+    if (argumentArray != null) {
+      int len = argumentArray.length;
+      out.writeInt(len);
+      for (int i = 0; i < argumentArray.length; i++) {
+        if (argumentArray[i] != null) {
+          out.writeObject(argumentArray[i].toString());
+        } else {
+          out.writeObject(NULL_ARGUMENT_ARRAY_ELEMENT);
+        }
+      }
+    } else {
+      out.writeInt(NULL_ARGUMENT_ARRAY);
+    }
+
+  }
+
+  private void readObject(ObjectInputStream in) throws IOException,
+      ClassNotFoundException {
+    in.defaultReadObject();
+    int levelInt = in.readInt();
+    level = Level.toLevel(levelInt);
+
+    int argArrayLen = in.readInt();
+    if (argArrayLen != NULL_ARGUMENT_ARRAY) {
+      argumentArray = new String[argArrayLen];
+      for (int i = 0; i < argArrayLen; i++) {
+        Object val = in.readObject();
+        if (!NULL_ARGUMENT_ARRAY_ELEMENT.equals(val)) {
+          argumentArray[i] = val;
+        }
+      }
+    }
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result + ((message == null) ? 0 : message.hashCode());
+    result = prime * result
+        + ((threadName == null) ? 0 : threadName.hashCode());
+    result = prime * result + (int) (timeStamp ^ (timeStamp >>> 32));
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    final PubLoggingEventVO other = (PubLoggingEventVO) obj;
+    if (message == null) {
+      if (other.message != null)
+        return false;
+    } else if (!message.equals(other.message))
+      return false;
+
+    if (loggerName == null) {
+      if (other.loggerName != null)
+        return false;
+    } else if (!loggerName.equals(other.loggerName))
+      return false;
+
+    if (threadName == null) {
+      if (other.threadName != null)
+        return false;
+    } else if (!threadName.equals(other.threadName))
+      return false;
+    if (timeStamp != other.timeStamp)
+      return false;
+
+    if (marker == null) {
+      if (other.marker != null)
+        return false;
+    } else if (!marker.equals(other.marker))
+      return false;
+
+    if (mdcPropertyMap == null) {
+      if (other.mdcPropertyMap != null)
+        return false;
+    } else if (!mdcPropertyMap.equals(other.mdcPropertyMap))
+      return false;
+    return true;
+  }
+  
+  public String toString() {
+    StringBuilder sb = new StringBuilder();
+    sb.append(timeStamp);
+    sb.append(" ");
+    sb.append(level);
+    sb.append(" [");
+    sb.append(threadName);
+    sb.append("] ");
+    sb.append(loggerName);
+    sb.append(" - ");
+    sb.append(getFormattedMessage());
+    return sb.toString();
+  }
+
+}


More information about the logback-dev mailing list