[LOGBack-dev] svn commit: r569 - in logback/trunk: logback-classic/src/main/java/ch/qos/logback/classic/helpers logback-classic/src/main/java/ch/qos/logback/classic/html logback-classic/src/main/java/ch/qos/logback/classic/net logback-core/src/main/java/ch/qos/logback/core logback-core/src/main/java/ch/qos/logback/core/net

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Sep 12 22:39:21 CEST 2006


Author: ceki
Date: Tue Sep 12 22:39:21 2006
New Revision: 569

Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/helpers/CyclicBuffer.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/Layout.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/LayoutBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java

Log:
ongoing work

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/helpers/CyclicBuffer.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/helpers/CyclicBuffer.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/helpers/CyclicBuffer.java	Tue Sep 12 22:39:21 2006
@@ -7,43 +7,41 @@
  * the terms of the GNU Lesser General Public License as published by the Free
  * Software Foundation.
  */
-     
+
 package ch.qos.logback.classic.helpers;
 
 import ch.qos.logback.classic.spi.LoggingEvent;
 
-
 /**
-
-   CyclicBuffer is used by other appenders to hold {@link LoggingEvent
-   LoggingEvents} for immediate or differed display.
-   
-   <p>This buffer gives read access to any element in the buffer not
-   just the first or last element.
-
-   @author Ceki G&uuml;lc&uuml;
-   @since 0.9.0
-
+ * 
+ * CyclicBuffer is used by other appenders to hold
+ * {@link LoggingEvent LoggingEvents} for immediate or differed display.
+ * <p>
+ * This buffer gives read access to any element in the buffer not just the first
+ * or last element.
+ * 
+ * @author Ceki G&uuml;lc&uuml;
  */
 public class CyclicBuffer {
-  
-  LoggingEvent[] ea;
-  int first; 
-  int last; 
+
+  Object[] ea;
+  int first;
+  int last;
   int numElems;
   int maxSize;
 
   /**
-     Instantiate a new CyclicBuffer of at most <code>maxSize</code> events.
-
-     The <code>maxSize</code> argument must a positive integer.
-
-     @param maxSize The maximum number of elements in the buffer.
-  */
+   * Instantiate a new CyclicBuffer of at most <code>maxSize</code> events.
+   * 
+   * The <code>maxSize</code> argument must a positive integer.
+   * 
+   * @param maxSize
+   *          The maximum number of elements in the buffer.
+   */
   public CyclicBuffer(int maxSize) throws IllegalArgumentException {
-    if(maxSize < 1) {
-      throw new IllegalArgumentException("The maxSize argument ("+maxSize+
-			    ") is not a positive integer.");
+    if (maxSize < 1) {
+      throw new IllegalArgumentException("The maxSize argument (" + maxSize
+          + ") is not a positive integer.");
     }
     this.maxSize = maxSize;
     ea = new LoggingEvent[maxSize];
@@ -51,94 +49,86 @@
     last = 0;
     numElems = 0;
   }
-    
-  /**
-     Add an <code>event</code> as the last event in the buffer.
 
+  /**
+   * Add an <code>event</code> as the last event in the buffer.
+   * 
    */
-  public
-  void add(LoggingEvent event) {    
-    ea[last] = event;    
-    if(++last == maxSize)
+  public void add(LoggingEvent event) {
+    ea[last] = event;
+    if (++last == maxSize)
       last = 0;
 
-    if(numElems < maxSize)
+    if (numElems < maxSize)
       numElems++;
-    else if(++first == maxSize)
+    else if (++first == maxSize)
       first = 0;
   }
 
-
   /**
-     Get the <i>i</i>th oldest event currently in the buffer. If
-     <em>i</em> is outside the range 0 to the number of elements
-     currently in the buffer, then <code>null</code> is returned.
-
-
-  */
-  public
-  LoggingEvent get(int i) {
-    if(i < 0 || i >= numElems)
+   * Get the <i>i</i>th oldest event currently in the buffer. If <em>i</em>
+   * is outside the range 0 to the number of elements currently in the buffer,
+   * then <code>null</code> is returned.
+   */
+  public Object get(int i) {
+    if (i < 0 || i >= numElems)
       return null;
 
     return ea[(first + i) % maxSize];
   }
 
-  public 
-  int getMaxSize() {
+  public int getMaxSize() {
     return maxSize;
   }
 
+
   /**
-     Get the oldest (first) element in the buffer. The oldest element
-     is removed from the buffer.
-  */
-  public
-  LoggingEvent get() {
-    LoggingEvent r = null;
-    if(numElems > 0) {
+   * Get the oldest (first) element in the buffer. The oldest element is removed
+   * from the buffer.
+   */
+  public Object get() {
+    Object r = null;
+    if (numElems > 0) {
       numElems--;
       r = ea[first];
       ea[first] = null;
-      if(++first == maxSize)
-	first = 0;
-    } 
+      if (++first == maxSize)
+        first = 0;
+    }
     return r;
   }
-  
+
   /**
-     Get the number of elements in the buffer. This number is
-     guaranteed to be in the range 0 to <code>maxSize</code>
-     (inclusive).
-  */
-  public
-  int length() {
+   * Get the number of elements in the buffer. This number is guaranteed to be
+   * in the range 0 to <code>maxSize</code> (inclusive).
+   */
+  public int length() {
     return numElems;
-  } 
+  }
 
   /**
-     Resize the cyclic buffer to <code>newSize</code>.
-
-     @throws IllegalArgumentException if <code>newSize</code> is negative.
+   * Resize the cyclic buffer to <code>newSize</code>.
+   * 
+   * @throws IllegalArgumentException
+   *           if <code>newSize</code> is negative.
    */
-  public 
-  void resize(int newSize) {
-    if(newSize < 0) {
-      throw new IllegalArgumentException("Negative array size ["+newSize+
-					 "] not allowed.");
+  public void resize(int newSize) {
+    if (newSize < 0) {
+      throw new IllegalArgumentException("Negative array size [" + newSize
+          + "] not allowed.");
     }
-    if(newSize == numElems)
+    if (newSize == numElems)
       return; // nothing to do
-    
-    LoggingEvent[] temp = new  LoggingEvent[newSize];
+
+    Object[] temp = new Object[newSize];
 
     int loopLen = newSize < numElems ? newSize : numElems;
-    
-    for(int i = 0; i < loopLen; i++) {
+
+    for (int i = 0; i < loopLen; i++) {
       temp[i] = ea[first];
       ea[first] = null;
-      if(++first == numElems) 
-	first = 0;
+      if (++first == numElems)
+        first = 0;
     }
     ea = temp;
     first = 0;

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/html/HTMLLayout.java	Tue Sep 12 22:39:21 2006
@@ -120,6 +120,7 @@
     while (c != null) {
       if (c instanceof ThrowableHandlingConverter) {
         chainHandlesThrowable = true;
+        return;
       }
       c = c.getNext();
     }
@@ -147,6 +148,7 @@
   /**
    * Returns the content type output by this layout, i.e "text/html".
    */
+  @Override
   public String getContentType() {
     return "text/html";
   }
@@ -172,8 +174,9 @@
    */
   public String getHeader() {
     StringBuffer sbuf = new StringBuffer();
-    sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"");
-    sbuf.append(" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
+
+    sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
+    sbuf.append(" SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
     sbuf.append(LINE_SEP);
     sbuf.append("<html>");
     sbuf.append(LINE_SEP);
@@ -217,6 +220,10 @@
     sbuf.append("<tr class=\"header\">");
     sbuf.append(LINE_SEP);
     while (c != null) {
+      if (c instanceof ThrowableHandlingConverter) {
+        c = c.getNext();
+        continue;
+      }
       name = computeConverterName(c);
       if (name == null) {
         c = c.getNext();
@@ -249,14 +256,6 @@
     return sbuf.toString();
   }
 
-  /**
-   * The HTML layout handles the throwable contained in logging events. Hence,
-   * this method return <code>false</code>.
-   */
-  public boolean ignoresThrowable() {
-    return false;
-  }
-
   public String doLayout(Object event) {
     return doLayout((LoggingEvent) event);
   }
@@ -289,8 +288,9 @@
         ThrowableHandlingConverter converter = (ThrowableHandlingConverter)c;
         if (converter.onNewLine(event)) {
           buf.append("</tr>");
-          buf.append("<tr>");
-          appendEventToBuffer(buf, c, event);
+          buf.append("<tr colspan=\"6\">");
+          appendThrowableAsHTML(event.getThrowableInformation().getThrowableStrRep(), buf);
+          //appendEventToBuffer(buf, c, event);
           if (c.getNext() != null) {
             //here we assume that when we exist the while loop,
             //a </tr> tag is added.

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java	Tue Sep 12 22:39:21 2006
@@ -13,8 +13,10 @@
 import java.io.File;
 
 import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.PatternLayout;
 import ch.qos.logback.classic.helpers.CyclicBuffer;
 import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.Layout;
 import ch.qos.logback.core.net.SMTPAppenderBase;
 import ch.qos.logback.core.rolling.TriggeringPolicy;
 
@@ -35,6 +37,8 @@
  */
 public class SMTPAppender extends SMTPAppenderBase {
 
+  static final String DEFAULT_SUBJECT_PATTERN = "%m";
+  
   private int bufferSize = 512;
   protected CyclicBuffer cb = new CyclicBuffer(bufferSize);
 
@@ -72,16 +76,8 @@
     int len = cb.length();
     for (int i = 0; i < len; i++) {
       // sbuf.append(MimeUtility.encodeText(layout.format(cb.get())));
-      LoggingEvent event = cb.get();
+      Object event = cb.get();
       sbuf.append(layout.doLayout(event));
-      // if (layout.ignoresThrowable()) {
-      // String[] s = event.getThrowableStrRep();
-      // if (s != null) {
-      // for (int j = 0; j < s.length; j++) {
-      // sbuf.append(s[j]);
-      // }
-      // }
-      // }
     }
   }
 
@@ -103,6 +99,17 @@
   public int getBufferSize() {
     return bufferSize;
   }
+
+  @Override
+  protected Layout makeSubjectLayout(String subjectStr) {
+    if(subjectStr == null) {
+      subjectStr = DEFAULT_SUBJECT_PATTERN;
+    }
+    PatternLayout pl = new PatternLayout();
+    pl.setPattern(subjectStr);
+    pl.start();
+    return pl;
+  }
 }
 
 class DefaultEvaluator implements TriggeringPolicy {

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/Layout.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/Layout.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/Layout.java	Tue Sep 12 22:39:21 2006
@@ -42,4 +42,11 @@
    */
   String getFooter();
   
+  /**
+   * Returns the content type as appropriate for the implementation.
+   *  
+   * @return
+   */
+  String getContentType();
+  
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/LayoutBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/LayoutBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/LayoutBase.java	Tue Sep 12 22:39:21 2006
@@ -47,6 +47,10 @@
     return footer;
   }
 
+  public String getContentType() {
+    return "text/plain";
+  }
+  
   public void setHeader(String header) {
     this.header = header;
   }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java	Tue Sep 12 22:39:21 2006
@@ -38,11 +38,14 @@
  * 
  */
 public abstract class SMTPAppenderBase extends AppenderBase {
+  
+  
   protected Layout layout;
+  protected Layout subjectLayout;
 
   private String to;
   private String from;
-  private String subject;
+  private String subjectStr = null;
   private String smtpHost;
 
   protected Message msg;
@@ -50,6 +53,17 @@
   protected TriggeringPolicy evaluator;
 
   /**
+   * return a layout for the subjet string as appropriate for the
+   * module. If the subjectStr parameter is null, then a default
+   * value for subjectStr should be used.
+   * 
+   * @param subjectStr
+   * 
+   * @return a layout as appropriate for the module
+   */
+  abstract protected Layout makeSubjectLayout(String subjectStr);
+  
+  /**
    * Start the appender
    */
   public void start() {
@@ -70,10 +84,9 @@
       }
 
       msg.setRecipients(Message.RecipientType.TO, parseAddress(to));
-      if (subject != null) {
-        msg.setSubject(subject);
-      }
-
+      
+      subjectLayout = makeSubjectLayout(subjectStr);
+      
       started = true;
 
     } catch (MessagingException e) {
@@ -94,7 +107,7 @@
     subAppend(eventObject);
 
     if (evaluator.isTriggeringEvent(null, eventObject)) {
-      sendBuffer();
+      sendBuffer(eventObject);
     }
   }
   
@@ -158,7 +171,7 @@
   /**
    * Send the contents of the cyclic buffer as an e-mail message.
    */
-  protected void sendBuffer() {
+  protected void sendBuffer(Object lastEventObject) {
 
     // Note: this code already owns the monitor for this
     // appender. This frees us from needing to synchronize on 'cb'.
@@ -166,12 +179,22 @@
       MimeBodyPart part = new MimeBodyPart();
 
       StringBuffer sbuf = new StringBuffer();
-      String t = layout.getHeader();
+      
+      String header = layout.getHeader();
+      if (header != null) {
+        sbuf.append(header);
+      }
       fillBuffer(sbuf);
-      t = layout.getFooter();
-      if (t != null)
-        sbuf.append(t);
-      part.setContent(sbuf.toString(), "text/plain");
+      String footer = layout.getFooter();
+      if (footer != null) {
+        sbuf.append(footer);
+      }
+      
+      if (subjectLayout != null) {
+        msg.setSubject(subjectLayout.doLayout(lastEventObject));
+      }
+      
+      part.setContent(sbuf.toString(), layout.getContentType());
 
       Multipart mp = new MimeMultipart();
       mp.addBodyPart(part);
@@ -204,9 +227,9 @@
    * Returns value of the <b>Subject</b> option.
    */
   public String getSubject() {
-    return subject;
+    return subjectStr;
   }
-
+  
   /**
    * The <b>From</b> option takes a string value which should be a e-mail
    * address of the sender.
@@ -220,9 +243,9 @@
    * subject of the e-mail message.
    */
   public void setSubject(String subject) {
-    this.subject = subject;
+    this.subjectStr = subject;
   }
-
+  
   /**
    * The <b>SMTPHost</b> option takes a string value which should be a the host
    * name of the SMTP server that will send the e-mail message.



More information about the logback-dev mailing list