KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > util > Debug


1 /*
2  * Copyright (C) 2002 - 2005 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Original developer: ScalAgent Distributed Technologies
20  * Contributor(s): Douglas S. Jackson
21  */

22 package fr.dyade.aaa.util;
23
24 import java.io.*;
25 import java.util.*;
26
27 import org.objectweb.util.monolog.api.*;
28 import org.objectweb.util.monolog.wrapper.remote.lib.MonologFactoryMBeanImpl;
29 import fr.dyade.aaa.util.management.MXWrapper;
30
31 /**
32  * This class handles the debug traces.
33  */

34 public class Debug {
35   /** flag used to remove huge logging */
36   public final static boolean debug = true;
37
38   /**
39    * Property name for A3 debug configuration directory.
40    * If not defined, the configuration file is searched from the search
41    * path used to load classes.
42    */

43   public final static String JavaDoc DEBUG_DIR_PROPERTY = "fr.dyade.aaa.DEBUG_DIR";
44   /** Default value for A3 debug configuration directory. */
45   public final static String JavaDoc DEFAULT_DEBUG_DIR = ".";
46   /** Property name for A3 debug configuration filename */
47   public final static String JavaDoc DEBUG_FILE_PROPERTY = "fr.dyade.aaa.DEBUG_FILE";
48   /** Default filename for A3 debug configuration */
49   public final static String JavaDoc DEFAULT_DEBUG_FILE = "a3debug.cfg";
50
51   /** */
52   protected static LoggerFactory factory;
53
54   /**
55    * Name of the directory where the debug configuration
56    * file can be found.
57    */

58   private static String JavaDoc debugDir = null;
59
60   /**
61    * Name of the debug configuration file.
62    */

63   private static String JavaDoc debugFileName = null;
64
65   public static void setDebugDir(String JavaDoc debugDir) {
66     Debug.debugDir = debugDir;
67   }
68
69   public static void setDebugFileName(String JavaDoc debugFileName) {
70     Debug.debugFileName = debugFileName;
71   }
72
73   public static void reinit() throws Exception JavaDoc {
74     initialize();
75   }
76
77   protected static void init() throws Exception JavaDoc {
78     try {
79       initialize();
80     } catch(Exception JavaDoc exc) {
81       System.err.println("Monolog configuration file not found, use defaults");
82       try {
83         ((MonologFactory) factory).configure(null);
84         Logger[] loggers = factory.getLoggers();
85         for (int i=0; i<loggers.length; i++) {
86           loggers[i].setIntLevel(BasicLevel.ERROR);
87         }
88       } catch (Exception JavaDoc e) {
89         System.err.println("Unable to configure monolog wrapper");
90         throw new Exception JavaDoc("Unable to configure monolog wrapper");
91       }
92     }
93
94     // Be careful, initialize first the monolog factory before trying to
95
// register the monolog MBean (see MXWrapper.init).
96

97 // try {
98
// System.out.println("try to register");
99
// MXWrapper.init();
100
// MXWrapper.registerMBean(
101
// new MonologFactoryMBeanImpl((MonologFactory) factory),
102
// "logging", "name=monolog");
103
// System.out.println("registered");
104
// } catch (Exception exc) {
105
// System.err.println("Unable to register monolog MBean");
106
// exc.printStackTrace();
107
// }
108

109   }
110
111   private static PrivateLogger logger = null;
112
113   /**
114    * Initializes the package.
115    */

116   private static void initialize() throws Exception JavaDoc {
117     String JavaDoc ldebugDir = debugDir;
118     if (ldebugDir == null)
119       ldebugDir = System.getProperty(DEBUG_DIR_PROPERTY);
120     String JavaDoc ldebugFileName = debugFileName;
121     if (ldebugFileName == null)
122       ldebugFileName = System.getProperty(DEBUG_FILE_PROPERTY,
123                                           DEFAULT_DEBUG_FILE);
124     if (ldebugDir != null) {
125       File debugFile = new File(ldebugDir, ldebugFileName);
126       try {
127         if ((debugFile != null) &&
128             debugFile.exists() &&
129             debugFile.isFile() &&
130             (debugFile.length() != 0)) {
131           ldebugFileName = debugFile.getPath();
132         } else {
133           throw new IOException();
134         }
135       } catch (IOException exc) {
136         // debug configuration file seems not exist, search it from the
137
// search path used to load classes.
138
System.err.println("Unable to find \"" + debugFile.getPath() + "\".");
139         ldebugDir = null;
140       }
141     }
142
143     try {
144       System.setProperty(org.objectweb.util.monolog.Monolog.MONOLOG_FILE_NAME,
145                          ldebugFileName);
146       factory = org.objectweb.util.monolog.Monolog.init();
147       if (factory == null) {
148         System.err.println("Error in Monolog initialization: null factory");
149       } else {
150         Logger dl = factory.getLogger("fr.dyade.aaa.util.debug");
151         dl.log(BasicLevel.INFO, "Debug.initialize() - " + ldebugFileName);
152       }
153     } catch(Throwable JavaDoc exc) {
154       System.err.println("Unable to instantiate monolog wrapper");
155       exc.printStackTrace();
156     }
157   }
158
159   public static Logger getLogger(String JavaDoc topic) {
160     try {
161       if (factory == null) init();
162       return factory.getLogger(topic);
163     } catch (Exception JavaDoc exc) {
164     }
165     if (logger == null)
166       logger = new PrivateLogger();
167     return logger;
168   }
169
170   public static void setLoggerLevel(String JavaDoc topic, int level) {
171     getLogger(topic).setIntLevel(level);
172   }
173
174   /**
175    * Set the monolog Loggerfactory
176    * @param loggerFactory the monolog LoggerFactory
177    */

178   public static void setLoggerFactory(LoggerFactory loggerFactory) {
179     factory = loggerFactory;
180   }
181
182   private static class PrivateLogger implements Logger {
183     PrivateLogger() {
184       BasicLevel.FATAL = 1000;
185       BasicLevel.ERROR = 800;
186       BasicLevel.WARN = 600;
187       BasicLevel.INFO = 400;
188       BasicLevel.DEBUG = 200;
189     }
190
191     // Interface Handler
192

193     /**
194      * It retrieves the name of the handler
195      */

196     public String JavaDoc getName() {
197       return toString();
198     }
199
200     /**
201      * It assigns the name of the handler
202      */

203     public void setName(String JavaDoc name) {}
204
205     /**
206      * It retrieves the Handler type
207      */

208     public String JavaDoc getType() {
209       return "console";
210     }
211
212     /**
213      * It retrieves the attributes of the handler
214      */

215     public String JavaDoc[] getAttributeNames() {
216       return null;
217     }
218
219     /**
220      * It retrieves the value of an attribute value of the handler.
221      * @param key is an attribute name
222      */

223     public Object JavaDoc getAttribute(String JavaDoc name) {
224       return null;
225     }
226
227     /**
228      * It assigns an attributte to the handler.
229      * @param key is the attribute name
230      * @param value is the attribute value
231      * @return the old value is the attribute was already defined
232      */

233     public Object JavaDoc setAttribute(String JavaDoc name, Object JavaDoc value) {
234       return null;
235     }
236
237     // Interface Logger
238

239     /**
240      * Sets the IntLevel attribute of the LoggerImpl object
241      *
242      * @param l The new IntLevel value
243      */

244     public void setIntLevel(int l) {}
245
246     /**
247      * Sets the Level attribute of the LoggerImpl object
248      *
249      * @param l The new Level value
250      */

251     public void setLevel(Level l) {}
252
253     /**
254      * Gets the CurrentIntLevel attribute of the LoggerImpl object
255      *
256      * @return The CurrentIntLevel value
257      */

258     public int getCurrentIntLevel() {
259       return BasicLevel.ERROR;
260     }
261
262     /**
263      * Gets the CurrentLevel attribute of the LoggerImpl object
264      *
265      * @return The CurrentLevel value
266      */

267     public Level getCurrentLevel() {
268       return null;
269     }
270
271     /**
272      * Gets the Loggable attribute of the LoggerImpl object
273      *
274      * @param l Description of Parameter
275      * @return The Loggable value
276      */

277     public boolean isLoggable(int level) {
278       if (level >= BasicLevel.ERROR)
279         return true;
280       return false;
281     }
282
283     /**
284      * Gets the Loggable attribute of the LoggerImpl object
285      *
286      * @param l Description of Parameter
287      * @return The Loggable value
288      */

289     public boolean isLoggable(Level l) {
290       if (l.getIntValue() >= BasicLevel.ERROR)
291         return true;
292       return false;
293     }
294
295     /**
296      * Gets the On attribute of the LoggerImpl object
297      *
298      * @return The On value
299      */

300     public boolean isOn() {
301       return true;
302     }
303     
304     /**
305      * Log a message, with no arguments.
306      * If the logger is currently enabled for the given message level then the
307      * given message is treated
308      */

309     public void log(int level, Object JavaDoc o) {
310       if (level >= BasicLevel.ERROR)
311         System.err.println(o.toString());
312     }
313     /**
314      * Log a message, with no arguments.
315      * If the logger is currently enabled for the given message level then the
316      * given message is treated
317      */

318     public void log(Level l, Object JavaDoc o) {
319       if (l.getIntValue() >= BasicLevel.ERROR)
320         System.err.println(o.toString());
321     }
322
323     /**
324      * Log a message, with a throwable arguments which can represent an
325      * error or a context..
326      */

327     public void log(int level, Object JavaDoc o, Throwable JavaDoc t) {
328       if (level >= BasicLevel.ERROR)
329         System.err.println(o.toString() + ":" + t.toString());
330     }
331     /**
332      * Log a message, with a throwable arguments which can represent an
333      * error or a context..
334      */

335     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t) {
336       if (l.getIntValue() >= BasicLevel.ERROR)
337         System.err.println(o.toString() + ":" + t.toString());
338     }
339
340
341     /**
342      * Log a message, with a location and method arguments. The location
343      * parameter can be the object instance which logs the event, or a string
344      * representation of the object.
345      * The method argument can be a java.lang.reflect.Method or a string which
346      * represents the method name.
347      */

348     public void log(int level, Object JavaDoc o, Object JavaDoc location, Object JavaDoc method) {
349       if (level >= BasicLevel.ERROR)
350         System.err.println(location.toString() + "." + method.toString() +
351                            "(...) :" + o.toString());
352     }
353     /**
354      * Log a message, with a location and method arguments. The location
355      * parameter can be the object instance which logs the event, or a string
356      * representation of the object.
357      * The method argument can be a java.lang.reflect.Method or a string which
358      * represents the method name.
359      */

360     public void log(Level l, Object JavaDoc o, Object JavaDoc location,
361                     Object JavaDoc method) {
362       if (l.getIntValue() >= BasicLevel.ERROR)
363         System.err.println(location.toString() + "." + method.toString() +
364                            "(...) :" + o.toString());
365     }
366
367     /**
368      * Log a message, with a location, method and throwable arguments.
369      * The location parameter can be the object instance which logs the
370      * event, or a string representation of the object..
371      * The method argument can be a java.lang.reflect.Method or a string which
372      * represents the method name.
373      * The throwable parameter permits to log an Exception.
374      */

375     public void log(int level, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location, Object JavaDoc method) {
376       if (level >= BasicLevel.ERROR)
377         System.err.println(location.toString() + "." + method.toString() +
378                            "(...) :" + o.toString() + " " + t.toString());
379     }
380     /**
381      * Log a message, with a location, method and throwable arguments.
382      * The location parameter can be the object instance which logs the
383      * event, or a string representation of the object..
384      * The method argument can be a java.lang.reflect.Method or a string which
385      * represents the method name.
386      * The throwable parameter permits to log an Exception.
387      */

388     public void log(Level l, Object JavaDoc o, Throwable JavaDoc t, Object JavaDoc location,
389                     Object JavaDoc method) {
390       if (l.getIntValue() >= BasicLevel.ERROR)
391         System.err.println(location.toString() + "." + method.toString() +
392                            "(...) :" + o.toString() + " " + t.toString());
393     }
394
395     /** Enables this logger */
396     public void turnOn() {}
397
398     /** Disables this logger */
399     public void turnOff() {}
400
401     public String JavaDoc toString() {
402       return "Private ScalAgent D.T. default implementation";
403     }
404   }
405 }
406
Popular Tags