[logback-dev] svn commit: r705 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/status main/java/ch/qos/logback/core/util test/java/ch/qos/logback/core/status

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Oct 17 23:35:53 CEST 2006


Author: ceki
Date: Tue Oct 17 23:35:53 2006
New Revision: 705

Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/Status.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/StatusBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusBaseTest.java

Log:
StatusPrinter now does a better job when printing nested stati.

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/Status.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/Status.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/Status.java	Tue Oct 17 23:35:53 2006
@@ -1,18 +1,18 @@
 /**
- * LOGBack: the reliable, fast and flexible logging library for Java.
- *
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
  * Copyright (C) 1999-2006, QOS.ch
- *
- * This library is free software, you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation.
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
  */
 package ch.qos.logback.core.status;
 
 import java.util.Iterator;
 
 
-public interface Status {
+public interface Status  {
 
   public final int INFO = 0;
   public final int WARN = 1;
@@ -27,6 +27,6 @@
   public boolean hasChildren();
   public void add(Status child);
   public boolean remove(Status child);
-  public Iterator iterator();
+  public Iterator<Status> iterator();
 
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/StatusBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/StatusBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/status/StatusBase.java	Tue Oct 17 23:35:53 2006
@@ -10,20 +10,19 @@
 package ch.qos.logback.core.status;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
-
 abstract public class StatusBase implements Status {
 
+  static private final List<Status> EMPTY_LIST = new ArrayList<Status>(0);
+  
   int level;
   final String message;
   final Object origin;
   List<Status> childrenList;
   Throwable throwable;
 
-  
   StatusBase(int level, String msg, Object origin) {
     this(level, msg, origin, null);
   }
@@ -37,25 +36,23 @@
 
   public synchronized void add(Status child) {
     if (child == null) {
-      throw new NullPointerException(
-        "Null values are not valid Status.");
+      throw new NullPointerException("Null values are not valid Status.");
     }
     if (childrenList == null) {
       childrenList = new ArrayList<Status>();
     }
     childrenList.add(child);
   }
-  
 
   public synchronized boolean hasChildren() {
     return ((childrenList != null) && (childrenList.size() > 0));
   }
 
-  public synchronized Iterator iterator() {
+  public synchronized Iterator<Status> iterator() {
     if (childrenList != null) {
       return childrenList.iterator();
     } else {
-      return Collections.EMPTY_LIST.iterator();
+      return EMPTY_LIST.iterator();
     }
   }
 
@@ -64,7 +61,7 @@
       return false;
     }
 
-    //TODO also search in childrens' childrens
+    // TODO also search in childrens' childrens
     return childrenList.remove(statusToRemove);
 
   }
@@ -72,22 +69,22 @@
   public int getLevel() {
     return level;
   }
-  
+
   public int getEffectiveLevel() {
-  	int result = level;
-  	int effLevel;
-  	
-  	Iterator it = iterator();
-  	Status s;
-  	while(it.hasNext()) {
-  		s = (Status)it.next();
-  		effLevel = s.getEffectiveLevel();
-  		if (effLevel > result) {
-  			result = effLevel;
-  		}
-  	}
-  	
-  	return result;
+    int result = level;
+    int effLevel;
+
+    Iterator it = iterator();
+    Status s;
+    while (it.hasNext()) {
+      s = (Status) it.next();
+      effLevel = s.getEffectiveLevel();
+      if (effLevel > result) {
+        result = effLevel;
+      }
+    }
+
+    return result;
   }
 
   public String getMessage() {
@@ -101,24 +98,24 @@
   public Throwable getThrowable() {
     return throwable;
   }
-  
+
   /**
    * @Override
    */
   public String toString() {
     StringBuffer buf = new StringBuffer();
-    switch(level) {
-    case INFO: 
+    switch (level) {
+    case INFO:
       buf.append("INFO");
       break;
-    case WARN: 
+    case WARN:
       buf.append("WARN");
       break;
-    case ERROR: 
+    case ERROR:
       buf.append("ERROR");
       break;
     }
-    if(origin != null) {
+    if (origin != null) {
       buf.append(" in ");
       buf.append(origin);
       buf.append(" -");
@@ -126,14 +123,13 @@
 
     buf.append(" ");
     buf.append(message);
-    
-    if(throwable != null) {
+
+    if (throwable != null) {
       buf.append(" ");
       buf.append(throwable);
     }
-    
+
     return buf.toString();
   }
-  
-  
+
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/StatusPrinter.java	Tue Oct 17 23:35:53 2006
@@ -37,10 +37,30 @@
     Iterator it = sm.iterator();
     while (it.hasNext()) {
       Status s = (Status) it.next();
-      System.out.println(s);
-      if (s.getThrowable() != null) {
-        s.getThrowable().printStackTrace(System.out);
+      print("", s);
+     
+    }
+  }
+  
+  private static void print(String indentation, Status s) {
+    String prefix;
+    if(s.hasChildren()) {
+       prefix = indentation + "+ ";
+    } else {
+      prefix = indentation + "|-";
+    }
+    System.out.println(prefix+s);
+    if (s.getThrowable() != null) {
+      s.getThrowable().printStackTrace(System.out);
+    }
+    if(s.hasChildren()) {
+      Iterator<Status> ite = s.iterator();
+      while(ite.hasNext()) {
+        Status child = ite.next();
+        print(indentation+"  ", child);
       }
+      
     }
   }
+  
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusBaseTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusBaseTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/status/StatusBaseTest.java	Tue Oct 17 23:35:53 2006
@@ -1,3 +1,12 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 1999-2006, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
 package ch.qos.logback.core.status;
 
 import java.util.Iterator;
@@ -6,72 +15,75 @@
 
 public class StatusBaseTest extends TestCase {
 
-	public void testAddStatus() {
-		{
-			InfoStatus status = new InfoStatus("testing", this);
-			status.add(new ErrorStatus("error", this));
-			Iterator it = status.iterator();
-			assertTrue("No status was added", it.hasNext());
-			assertTrue("hasChilden method reported wrong result", status
-					.hasChildren());
-		}
-		{
-			InfoStatus status = new InfoStatus("testing", this);
-			try {
-				status.add(null);
-				fail("method should have thrown an Exception");
-			} catch (NullPointerException ex) {
-			}
-		}
-	}
-
-	public void testRemoveStatus() {
-		{
-			InfoStatus status = new InfoStatus("testing", this);
-			ErrorStatus error = new ErrorStatus("error", this);
-			status.add(error);
-			boolean result = status.remove(error);
-			Iterator it = status.iterator();
-			assertTrue("Remove failed", result);
-			assertFalse("No status was removed", it.hasNext());
-			assertFalse("hasChilden method reported wrong result", status
-					.hasChildren());
-		}
-		{
-			InfoStatus status = new InfoStatus("testing", this);
-			ErrorStatus error = new ErrorStatus("error", this);
-			status.add(error);
-			boolean result = status.remove(null);
-			assertFalse("Remove result was not false", result);
-		}
-	}
-	
-	public void testEffectiveLevel() {
-		{
-			//effective level = 0 level deep
-			ErrorStatus status = new ErrorStatus("error", this);
-			WarnStatus warn = new WarnStatus("warning", this);
-			status.add(warn);
-			assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.ERROR);
-		}
-		
-		{
-			//effective level = 1 level deep
-			InfoStatus status = new InfoStatus("info", this);
-			WarnStatus warn = new WarnStatus("warning", this);
-			status.add(warn);
-			assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.WARN);
-		}
-		
-		{
-			//effective level = 2 levels deep
-			InfoStatus status = new InfoStatus("info", this);
-			WarnStatus warn = new WarnStatus("warning", this);
-			ErrorStatus error = new ErrorStatus("error", this);
-			status.add(warn);
-			warn.add(error);
-			assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.ERROR);
-		}
-	}
+  public void testAddStatus() {
+    {
+      InfoStatus status = new InfoStatus("testing", this);
+      status.add(new ErrorStatus("error", this));
+      Iterator it = status.iterator();
+      assertTrue("No status was added", it.hasNext());
+      assertTrue("hasChilden method reported wrong result", status
+          .hasChildren());
+    }
+    {
+      InfoStatus status = new InfoStatus("testing", this);
+      try {
+        status.add(null);
+        fail("method should have thrown an Exception");
+      } catch (NullPointerException ex) {
+      }
+    }
+  }
+
+  public void testRemoveStatus() {
+    {
+      InfoStatus status = new InfoStatus("testing", this);
+      ErrorStatus error = new ErrorStatus("error", this);
+      status.add(error);
+      boolean result = status.remove(error);
+      Iterator it = status.iterator();
+      assertTrue("Remove failed", result);
+      assertFalse("No status was removed", it.hasNext());
+      assertFalse("hasChilden method reported wrong result", status
+          .hasChildren());
+    }
+    {
+      InfoStatus status = new InfoStatus("testing", this);
+      ErrorStatus error = new ErrorStatus("error", this);
+      status.add(error);
+      boolean result = status.remove(null);
+      assertFalse("Remove result was not false", result);
+    }
+  }
+
+  public void testEffectiveLevel() {
+    {
+      // effective level = 0 level deep
+      ErrorStatus status = new ErrorStatus("error", this);
+      WarnStatus warn = new WarnStatus("warning", this);
+      status.add(warn);
+      assertEquals("effective level misevaluated", status.getEffectiveLevel(),
+          Status.ERROR);
+    }
+
+    {
+      // effective level = 1 level deep
+      InfoStatus status = new InfoStatus("info", this);
+      WarnStatus warn = new WarnStatus("warning", this);
+      status.add(warn);
+      assertEquals("effective level misevaluated", status.getEffectiveLevel(),
+          Status.WARN);
+    }
+
+    {
+      // effective level = 2 levels deep
+      InfoStatus status = new InfoStatus("info", this);
+      WarnStatus warn = new WarnStatus("warning", this);
+      ErrorStatus error = new ErrorStatus("error", this);
+      status.add(warn);
+      warn.add(error);
+      assertEquals("effective level misevaluated", status.getEffectiveLevel(),
+          Status.ERROR);
+    }
+  }
 
 }



More information about the logback-dev mailing list