1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * Logger.java |
---|
19 | * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.core.logging; |
---|
23 | |
---|
24 | import weka.core.RevisionHandler; |
---|
25 | import weka.core.Utils; |
---|
26 | |
---|
27 | import java.io.PrintWriter; |
---|
28 | import java.io.StringWriter; |
---|
29 | import java.text.SimpleDateFormat; |
---|
30 | import java.util.Properties; |
---|
31 | |
---|
32 | /** |
---|
33 | * Abstract superclass for all loggers. |
---|
34 | * |
---|
35 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
36 | * @version $Revision: 4716 $ |
---|
37 | */ |
---|
38 | public abstract class Logger |
---|
39 | implements RevisionHandler { |
---|
40 | |
---|
41 | /** the properties file. */ |
---|
42 | public final static String PROPERTIES_FILE = "weka/core/logging/Logging.props"; |
---|
43 | |
---|
44 | /** |
---|
45 | * The logging level. |
---|
46 | * |
---|
47 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
48 | * @version $Revision: 4716 $ |
---|
49 | */ |
---|
50 | public enum Level { |
---|
51 | /** logs all messages. */ |
---|
52 | ALL(0), |
---|
53 | /** FINEST level. */ |
---|
54 | FINEST(1), |
---|
55 | /** FINEST level. */ |
---|
56 | FINER(2), |
---|
57 | /** FINER level. */ |
---|
58 | FINE(3), |
---|
59 | /** FINE level. */ |
---|
60 | INFO(4), |
---|
61 | /** WARNING level. */ |
---|
62 | WARNING(5), |
---|
63 | /** SEVERE level. */ |
---|
64 | SEVERE(6), |
---|
65 | /** turns logging off. */ |
---|
66 | OFF(10); |
---|
67 | |
---|
68 | /** the order of the level. */ |
---|
69 | private int m_Order; |
---|
70 | |
---|
71 | /** |
---|
72 | * Initializes the level. |
---|
73 | * |
---|
74 | * @param order the order of the level |
---|
75 | */ |
---|
76 | private Level(int order) { |
---|
77 | m_Order = order; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Returns the order of this level. |
---|
82 | * |
---|
83 | * @return the order |
---|
84 | */ |
---|
85 | public int getOrder() { |
---|
86 | return m_Order; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /** the minimum level of log events to have in order to end up in the log. */ |
---|
91 | protected Level m_MinLevel; |
---|
92 | |
---|
93 | /** the singleton instance of the logger. */ |
---|
94 | protected static Logger m_Singleton; |
---|
95 | |
---|
96 | /** the properties file. */ |
---|
97 | protected static Properties m_Properties; |
---|
98 | |
---|
99 | /** for formatting the dates. */ |
---|
100 | protected static SimpleDateFormat m_DateFormat; |
---|
101 | |
---|
102 | static { |
---|
103 | try { |
---|
104 | m_Properties = Utils.readProperties(PROPERTIES_FILE); |
---|
105 | } |
---|
106 | catch (Exception e) { |
---|
107 | System.err.println( |
---|
108 | "Error reading the logging properties '" + PROPERTIES_FILE + "': " + e); |
---|
109 | m_Properties = new Properties(); |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Initializes the logger. |
---|
115 | */ |
---|
116 | public Logger() { |
---|
117 | super(); |
---|
118 | |
---|
119 | initialize(); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Initializes the logger. |
---|
124 | */ |
---|
125 | protected void initialize() { |
---|
126 | m_MinLevel = Level.valueOf(m_Properties.getProperty("MinLevel", "INFO")); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Returns the minimum level log messages must have in order to appear in |
---|
131 | * the log. |
---|
132 | * |
---|
133 | * @return the level |
---|
134 | */ |
---|
135 | public Level getMinLevel() { |
---|
136 | return m_MinLevel; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Returns the location the logging happened. |
---|
141 | * |
---|
142 | * @return the classname (= [0]), the method (= [1]) and the |
---|
143 | * line number (= [2]) that generated the logging event |
---|
144 | */ |
---|
145 | protected static String[] getLocation() { |
---|
146 | String[] result; |
---|
147 | Throwable t; |
---|
148 | StackTraceElement[] trace; |
---|
149 | int i; |
---|
150 | |
---|
151 | result = new String[3]; |
---|
152 | |
---|
153 | t = new Throwable(); |
---|
154 | t.fillInStackTrace(); |
---|
155 | trace = t.getStackTrace(); |
---|
156 | |
---|
157 | for (i = 0; i < trace.length; i++) { |
---|
158 | // skip the Logger class |
---|
159 | if (trace[i].getClassName().equals(Logger.class.getName())) |
---|
160 | continue; |
---|
161 | |
---|
162 | if (trace[i].getClassName().equals(weka.gui.LogPanel.class.getName())) |
---|
163 | continue; |
---|
164 | |
---|
165 | // fill in result |
---|
166 | result[0] = trace[i].getClassName(); |
---|
167 | result[1] = trace[i].getMethodName(); |
---|
168 | result[2] = "" + trace[i].getLineNumber(); |
---|
169 | break; |
---|
170 | } |
---|
171 | |
---|
172 | return result; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Performs the actual logging. |
---|
177 | * Actual logger implementations must override this method. |
---|
178 | * |
---|
179 | * @param level the level of the message |
---|
180 | * @param msg the message to log |
---|
181 | * @param cls the classname originating the log event |
---|
182 | * @param method the method originating the log event |
---|
183 | * @param lineno the line number originating the log event |
---|
184 | */ |
---|
185 | protected abstract void doLog(Level level, String msg, String cls, String method, int lineno); |
---|
186 | |
---|
187 | /** |
---|
188 | * Returns the singleton instance of the logger. |
---|
189 | * |
---|
190 | * @return the logger instance |
---|
191 | */ |
---|
192 | public static Logger getSingleton() { |
---|
193 | String classname; |
---|
194 | |
---|
195 | if (m_Singleton == null) { |
---|
196 | // logger |
---|
197 | classname = m_Properties.getProperty("Logger", ConsoleLogger.class.getName()); |
---|
198 | try { |
---|
199 | m_Singleton = (Logger) Class.forName(classname).newInstance(); |
---|
200 | } |
---|
201 | catch (Exception e) { |
---|
202 | e.printStackTrace(); |
---|
203 | } |
---|
204 | |
---|
205 | // date format |
---|
206 | m_DateFormat = new SimpleDateFormat(m_Properties.getProperty("DateFormat", "yyyy-MM-dd HH:mm:ss")); |
---|
207 | } |
---|
208 | |
---|
209 | return m_Singleton; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Logs the given message under the given level. |
---|
214 | * |
---|
215 | * @param level the level of the message |
---|
216 | * @param msg the message to log |
---|
217 | */ |
---|
218 | public static void log(Level level, String msg) { |
---|
219 | Logger logger; |
---|
220 | boolean log; |
---|
221 | String[] location; |
---|
222 | |
---|
223 | logger = getSingleton(); |
---|
224 | if (logger == null) |
---|
225 | return; |
---|
226 | |
---|
227 | synchronized(logger) { |
---|
228 | log = false; |
---|
229 | if (logger.getMinLevel() == Level.ALL) |
---|
230 | log = true; |
---|
231 | else if (level.getOrder() >= logger.getMinLevel().getOrder()) |
---|
232 | log = true; |
---|
233 | if (!log) |
---|
234 | return; |
---|
235 | location = getLocation(); |
---|
236 | logger.doLog(level, msg, location[0], location[1], Integer.parseInt(location[2])); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * Logs the given message under the given level. |
---|
242 | * |
---|
243 | * @param level the level of the message |
---|
244 | * @param t the throwable to log |
---|
245 | */ |
---|
246 | public static void log(Level level, Throwable t) { |
---|
247 | StringWriter swriter; |
---|
248 | PrintWriter pwriter; |
---|
249 | |
---|
250 | swriter = new StringWriter(); |
---|
251 | pwriter = new PrintWriter(swriter); |
---|
252 | t.printStackTrace(pwriter); |
---|
253 | pwriter.close(); |
---|
254 | |
---|
255 | log(level, swriter.toString()); |
---|
256 | } |
---|
257 | } |
---|