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  package org.caleigo.toolkit.log;
19  
20  import java.util.*;
21  
22  /*** The log class acts as a gateway for log messages.
23  *
24  * @author Dennis Zikovic
25  * @version 1.00
26  * 
27  *//* 
28  *
29  * WHEN        WHO				WHY & WHAT
30  * ------------------------------------------------------------------------------
31  * 2001-04-01  Dennis Zikovic    Creation
32  */
33  public class Log
34  {
35      // Static data members -----------------------------------------------------
36      private static List sReceiverList = null;
37      private static final Properties sLogProperties = new Properties();
38      
39      // Static methods ----------------------------------------------------------
40      static
41      {
42          addReceiver(new StreamLogReceiver(System.out));
43      }
44      
45      public static void print(Object source, Object msg)
46      {
47          if(sReceiverList!=null)
48              logMsg(ILogReceiver.INFO, source, msg, null);
49      }
50      
51      public static void print(Object source, Object msg, Throwable exception)
52      {
53          if(sReceiverList!=null)
54              logMsg(ILogReceiver.INFO, source, msg, exception);
55      }
56      
57      public static void printWarning(Object source, Object msg)
58      {
59          if(sReceiverList!=null)
60              logMsg(ILogReceiver.WARNING, source, msg, null);
61      }
62      
63      public static void printWarning(Object source, Object msg, Throwable exception)
64      {
65          if(sReceiverList!=null)
66              logMsg(ILogReceiver.WARNING, source, msg, exception);
67      }
68      
69      public static void printError(Object source, Object msg)
70      {
71          if(sReceiverList!=null)
72              logMsg(ILogReceiver.ERROR, source, msg, null);
73      }
74      
75      public static void printError(Object source, Object msg, Throwable exception)
76      {
77          if(sReceiverList!=null)
78              logMsg(ILogReceiver.ERROR, source, msg, exception);
79      }
80          
81      public static boolean isLogging(Object source)
82      {
83          if(sReceiverList==null)
84              return false;
85          boolean logging = false;
86          for(int j=0; !logging && j<sReceiverList.size(); j++)
87              logging = ((ILogReceiver)sReceiverList.get(j)).isLogging(source);
88          return logging;
89      }
90      
91      // Action methods ----------------------------------------------------------
92  //    public static void config(ILogConfigurator configurator)
93  //    {
94  //    }
95      
96      public static void addReceiver(ILogReceiver theReceiver)
97      {
98          if(sReceiverList==null)
99              sReceiverList = new Vector();
100         sReceiverList.add(theReceiver);
101     }
102     
103     public static void removeReceiver(ILogReceiver theReceiver)
104     {
105         sReceiverList.remove(theReceiver);
106         theReceiver.close();
107     }
108     
109     public static void clearAllReceivers()
110     {
111         while(!sReceiverList.isEmpty())
112         {
113             ILogReceiver receiver = (ILogReceiver)sReceiverList.get(sReceiverList.size()-1);
114             sReceiverList.remove(sReceiverList.size()-1);
115             receiver.close();
116         }
117         sReceiverList = null;
118     }
119     
120     // Access methods ----------------------------------------------------------
121     public static void setProperty(String key, String value)
122     {
123         sLogProperties.setProperty(key, value);
124     }
125     
126     public static String getProperty(String key)
127     {
128         return sLogProperties.getProperty(key);
129     }
130     
131     // Help methods ------------------------------------------------------------
132     private static void logMsg(int msgType, Object source, Object msg, Throwable exception)
133     {
134         if(sReceiverList==null)
135             return;
136         String textMsg = msg.toString();
137         for(int j=0; j<sReceiverList.size(); j++)
138             ((ILogReceiver)sReceiverList.get(j)).log(msgType, source, textMsg, exception);
139     }
140 }