1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36 private static List sReceiverList = null;
37 private static final Properties sLogProperties = new Properties();
38
39
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
92
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
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
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 }