View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.toolkit.log;
20  
21  import java.util.*;
22  import java.text.*;
23  import java.io.*;
24  
25  /*** <Description of AbstractLogSubscriber>
26   *
27   * @author  Dennis Zikovic
28   * @version 1.00
29   * 
30   *//* 
31   *
32   * WHEN        WHO               WHY & WHAT
33   * -----------------------------------------------------------------------------
34   * 2001-08-01  Dennis Zikovic    Creation
35   */
36  public abstract class AbstractLogReceiver implements ILogReceiver
37  {
38      // Static data members -----------------------------------------------------
39      private static final SimpleDateFormat sDateFormater;
40      private static final SimpleDateFormat sTimeFormater;    
41      
42      // Static methods ----------------------------------------------------------
43      static
44      {
45          sDateFormater = new SimpleDateFormat("yyyy'-'MM'-'dd");
46          sTimeFormater = new SimpleDateFormat("HH':'mm':'ss'.'SSS");
47      }
48      
49      // Constructors ------------------------------------------------------------
50      
51      /*** Default constructor for AbstractLogSubscriber.
52       */
53      public AbstractLogReceiver() 
54      {
55      }
56      
57      // Abstact methods ---------------------------------------------------------
58      protected abstract void logString(String msg);
59      
60      // ILogSubscriber implementation -------------------------------------------
61      public boolean isLogging(Object source)
62      {
63          return true;
64      }
65      
66      public void log(int msgType, Object source, String msg, Throwable exception)
67      {
68          StringBuffer logMsg = new StringBuffer(512);
69          Calendar now = Calendar.getInstance();
70          
71          // Prepare header.
72          if(true)
73          {
74              if(msgType==ERROR)
75                  logMsg.append("ERROR");
76              else if(msgType==WARNING) 
77                  logMsg.append("WARNING");
78          }
79          if(true)
80          {
81              if(logMsg.length()>0)
82                  logMsg.append("|");
83              if(source==null)
84                  logMsg.append("(Static)");
85              else
86                  logMsg.append(source.getClass().getName());                
87          }
88                  
89          // Add the actual message to the buffer.
90          if(true)
91          {
92              if(logMsg.length()>0)
93                  logMsg.append(">\t");
94              logMsg.append(msg);
95          }
96          
97          // Convert callstack to string if any exists.
98          if(exception!=null)
99          {
100             StringWriter stackWriter = new StringWriter(512);
101             exception.printStackTrace(new PrintWriter(stackWriter));
102             logMsg.append("\n");
103             logMsg.append(stackWriter.toString());
104         }
105         
106         // Log the msg ussing subclass logString method.
107         this.logString(logMsg.toString());
108     }
109     
110     public void close()
111     {
112         // Does nothing as default.
113     }
114 }