View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.base.trace;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  
22  /**
23   * Developer trace.
24   *
25   * @author  Michael Meyling
26   */
27  public final class Trace {
28  
29      /** Logger for business messages. */
30      private static final Log BUSINESS = LogFactory.getFactory().getInstance("log");
31  
32      /** Is tracing on? If not only fatal errors and business messages are logged. */
33      private static boolean traceOn = false;
34  
35      /**
36       * Constructor.
37       */
38      private Trace() {
39          // don't call me
40      }
41  
42      /**
43       * Set tracing on. If not set only fatal errors and business messages are logged.
44       *
45       * @param   on  Set tracing on?
46       */
47      public static void setTraceOn(final boolean on) {
48          traceOn = on;
49      }
50  
51      /**
52       * Is tracing on? If not set only fatal errors and business messages are logged.
53       *
54       * @return  Is tracing on?
55       */
56      public static boolean isTraceOn() {
57          return traceOn;
58      }
59  
60      /**
61       * Trace business log message. The message is logged on "error" level.
62       *
63       * @param   message         Business log message.
64       */
65      public static void log(final String message) {
66          BUSINESS.error(message);
67      }
68  
69      /**
70       * Trace business log message. The message is logged on "error" level.
71       *
72       * @param   message         Business log message.
73       * @param   additional      Extra info for the next line.
74       */
75      public static void log(final String message, final String additional) {
76          if (BUSINESS.isErrorEnabled()) {
77              BUSINESS.error(message);
78              BUSINESS.error("    " + additional);
79          }
80      }
81  
82      /**
83       * Trace business log message. The message is logged on "error" level.
84       *
85       * @param   message         Business log message.
86       * @param   additional      Extra info for the next line.
87       * @param   description     Further description.
88       */
89      public static void log(final String message, final String additional, final String description) {
90          if (BUSINESS.isErrorEnabled()) {
91              BUSINESS.error(message);
92              BUSINESS.error("    " + additional);
93              BUSINESS.error("    " + description);
94          }
95      }
96  
97      /**
98       * Is debug log currently enabled?
99       *
100      * @param   tracingClass    Class we want to know the debug logging status for.
101      * @return  Debug log enabled.
102      */
103     public static boolean isDebugEnabled(final Class tracingClass) {
104         if (traceOn) {
105             final Log log = LogFactory.getFactory().getInstance(tracingClass);
106             return log.isDebugEnabled();
107         }
108         return false;
109     }
110 
111     /**
112      * Trace object.
113      * @param   tracingClass    Class that wants to make a trace entry.
114      * @param   tracingObject   Instance that wants to make a trace entry.
115      * @param   method          Method of that object.
116      * @param   object          Object to trace.
117      */
118     public static void trace(final Class tracingClass, final Object tracingObject,
119             final String method, final Object object) {
120         if (traceOn) {
121             final Log log = LogFactory.getFactory().getInstance(tracingClass);
122             if (log.isDebugEnabled()) {
123                 log.debug("." + method + " " + object);
124             }
125         }
126     }
127 
128     /**
129      * Trace object.
130      *
131      * @param   tracingClass    Class that wants to make a trace entry.
132      * @param   method          Method of that class.
133      * @param   object          Object to trace.
134      */
135     public static void trace(final Class tracingClass, final String method,
136             final Object object) {
137         if (traceOn) {
138             final Log log = LogFactory.getFactory().getInstance(tracingClass);
139             if (log.isDebugEnabled()) {
140                 log.debug("." + method + " " + object);
141             }
142         }
143     }
144 
145     /**
146      * Trace throwable.
147      *
148      * @param   tracingClass    Class that wants to make a trace entry.
149      * @param   tracingObject   Instance that wants to make a trace entry.
150      * @param   method          Method of that object.
151      * @param   throwable       Throwable to trace.
152      */
153     public static void trace(final Class tracingClass, final Object tracingObject,
154             final String method, final Throwable throwable) {
155         if (traceOn) {
156             final Log log = LogFactory.getFactory().getInstance(tracingClass);
157             if (log.isDebugEnabled()) {
158                 log.debug("." + method + " " + throwable, throwable);
159             }
160         }
161     }
162 
163     /**
164      * Trace throwable.
165      *
166      * @param   tracingClass    Class that wants to make a trace entry.
167      * @param   method          Method of that class.
168      * @param   throwable       Throwable to trace.
169      */
170     public static void trace(final Class tracingClass, final String method,
171             final Throwable throwable) {
172         if (traceOn) {
173             final Log log = LogFactory.getFactory().getInstance(tracingClass);
174             if (log.isDebugEnabled()) {
175                 log.debug("." + method + " " + throwable, throwable);
176             }
177         }
178     }
179 
180     /**
181      * Trace fatal throwable and extra description.
182      *
183      * @param   tracingClass    Class that wants to make a trace entry.
184      * @param   tracingObject   Instance that wants to make a trace entry.
185      * @param   method          Method of that object.
186      * @param   description     Further information.
187      * @param   throwable       Throwable to trace.
188      */
189     public static void fatal(final Class tracingClass, final Object tracingObject,
190             final String method, final String description, final Throwable throwable) {
191         final Log log = LogFactory.getFactory().getInstance(tracingClass);
192         log.fatal("." + method + " " + description, throwable);
193     }
194 
195     /**
196      * Trace fatal throwable and extra description.
197      *
198      * @param   tracingClass    Class that wants to make a trace entry.
199      * @param   method          Method of that class.
200      * @param   description     Further information.
201      * @param   throwable       Throwable to trace.
202      */
203     public static void fatal(final Class tracingClass, final String method,
204             final String description, final Throwable throwable) {
205         final Log log = LogFactory.getFactory().getInstance(tracingClass);
206         log.fatal("." + method + " " + description, throwable);
207     }
208 
209     /**
210      * Trace throwable and extra description.
211      *
212      * @param   tracingClass    Class that wants to make a trace entry.
213      * @param   tracingObject   Instance that wants to make a trace entry.
214      * @param   method          Method of that object.
215      * @param   description     Further information.
216      * @param   throwable       Throwable to trace.
217      */
218     public static void trace(final Class tracingClass, final Object tracingObject,
219             final String method, final String description, final Throwable throwable) {
220         if (traceOn) {
221             final Log log = LogFactory.getFactory().getInstance(tracingClass);
222             if (log.isDebugEnabled()) {
223                 log.debug("." + method + " " + description, throwable);
224             }
225         }
226     }
227 
228     /**
229      * Trace throwable and extra description.
230      *
231      * @param   tracingClass    Class that wants to make a trace entry.
232      * @param   method          Method of that class.
233      * @param   description     Further information.
234      * @param   throwable       Throwable to trace.
235      */
236     public static void trace(final Class tracingClass, final String method,
237             final String description, final Throwable throwable) {
238         if (traceOn) {
239             final Log log = LogFactory.getFactory().getInstance(tracingClass);
240             if (log.isDebugEnabled()) {
241                 log.debug("." + method + " " + description, throwable);
242             }
243         }
244     }
245 
246     /**
247      * Trace method begin. Should be followed by an analogous
248      * {@link #end(Class, Object, String)} call.
249      *
250      * @param   tracingClass    Class that wants to make a trace entry.
251      * @param   tracingObject   Instance that wants to make a trace entry.
252      * @param   method          Method of that object.
253      */
254     public static void begin(final Class tracingClass, final Object tracingObject,
255             final String method) {
256         if (traceOn) {
257             final Log log = LogFactory.getFactory().getInstance(tracingClass);
258             if (log.isDebugEnabled()) {
259                 log.debug("." + method + " " + "begin");
260             }
261         }
262     }
263 
264     /**
265      * Trace method begin. Should be followed by an analogous {@link #end(Class, String)} call.
266      *
267      * @param   tracingClass    Class that wants to make a trace entry.
268      * @param   method          Method of that class.
269      */
270     public static void begin(final Class tracingClass, final String method) {
271         if (traceOn) {
272             final Log log = LogFactory.getFactory().getInstance(tracingClass);
273             if (log.isDebugEnabled()) {
274                 log.debug("." + method + " " + "begin");
275             }
276         }
277     }
278 
279     /**
280      * Trace method end.
281      *
282      * @param   tracingClass    Class that wants to make a trace entry.
283      * @param   tracingObject   Instance that wants to make a trace entry.
284      * @param   method          Method of that object.
285      */
286     public static void end(final Class tracingClass, final Object tracingObject,
287             final String method) {
288         if (traceOn) {
289             final Log log = LogFactory.getFactory().getInstance(tracingClass);
290             if (log.isDebugEnabled()) {
291                 log.debug("." + method + " " + "end");
292             }
293         }
294     }
295 
296     /**
297      * Trace method end.
298      *
299      * @param   tracingClass    Class that wants to make a trace entry.
300      * @param   method          Method of that class.
301      */
302     public static void end(final Class tracingClass, final String method) {
303         if (traceOn) {
304             final Log log = LogFactory.getFactory().getInstance(tracingClass);
305             if (log.isDebugEnabled()) {
306                 log.debug("." + method + " " + "end");
307             }
308         }
309     }
310 
311     /**
312      * Trace message.
313      *
314      * @param   tracingClass    Class that wants to make a trace entry.
315      * @param   tracingObject   Instance that wants to make a trace entry.
316      * @param   method          Method of that object.
317      * @param   message         Message.
318      */
319     public static void info(final Class tracingClass, final Object tracingObject,
320             final String method, final String message) {
321         if (traceOn) {
322             final Log log = LogFactory.getFactory().getInstance(tracingClass);
323             if (log.isInfoEnabled()) {
324                 log.info("." + method + " " + message);
325             }
326         }
327     }
328 
329     /**
330      * Trace method message.
331      *
332      * @param   tracingClass    Class that wants to make a trace entry.
333      * @param   method          Method of that class.
334      * @param   message         Message.
335      */
336     public static void info(final Class tracingClass, final String method,
337             final String message) {
338         if (traceOn) {
339             final Log log = LogFactory.getFactory().getInstance(tracingClass);
340             if (log.isInfoEnabled()) {
341                 log.info("." + method + " " + message);
342             }
343         }
344     }
345 
346     /**
347      * Trace parameter.
348      *
349      * @param   tracingClass    Class that wants to make a trace entry.
350      * @param   tracingObject   Instance that wants to make a trace entry.
351      * @param   method          Method of that object.
352      * @param   param           Parameter to trace.
353      * @param   value           Value of parameter.
354      */
355     public static void param(final Class tracingClass, final Object tracingObject,
356             final String method, final String param, final Object value) {
357         if (traceOn) {
358             final Log log = LogFactory.getFactory().getInstance(tracingClass);
359             if (log.isDebugEnabled()) {
360                 log.debug("." + method + " " + param + "=" + value);
361             }
362         }
363     }
364 
365     /**
366      * Trace parameter.
367      *
368      * @param   tracingClass    Class that wants to make a trace entry.
369      * @param   method          Method of that class.
370      * @param   param           Parameter to trace.
371      * @param   value           Value of parameter.
372      */
373     public static void param(final Class tracingClass, final String method,
374             final String param, final Object value) {
375         if (traceOn) {
376             final Log log = LogFactory.getFactory().getInstance(tracingClass);
377             if (log.isDebugEnabled()) {
378                 log.debug("." + method + " " + param + "=" + value);
379             }
380         }
381     }
382 
383     /**
384      * Trace parameter.
385      *
386      * @param   tracingClass    Class that wants to make a trace entry.
387      * @param   tracingObject   Instance that wants to make a trace entry.
388      * @param   method          Method of that object.
389      * @param   param           Parameter to trace.
390      * @param   value           Value of parameter.
391      */
392     public static void param(final Class tracingClass, final Object tracingObject,
393             final String method, final String param, final int value) {
394         if (traceOn) {
395             final Log log = LogFactory.getFactory().getInstance(tracingClass);
396             if (log.isDebugEnabled()) {
397                 log.debug("." + method + " " + param + "=" + value);
398             }
399         }
400     }
401 
402     /**
403      * Trace parameter.
404      *
405      * @param   tracingClass    Class that wants to make a trace entry.
406      * @param   method          Method of that class.
407      * @param   param           Parameter to trace.
408      * @param   value           Value of parameter.
409      */
410     public static void param(final Class tracingClass, final String method,
411             final String param, final int value) {
412         if (traceOn) {
413             final Log log = LogFactory.getFactory().getInstance(tracingClass);
414             if (log.isDebugEnabled()) {
415                 log.debug("." + method + " " + param + "=" + value);
416             }
417         }
418     }
419 
420     /**
421      * Trace parameter.
422      *
423      * @param   tracingClass    Class that wants to make a trace entry.
424      * @param   tracingObject   Instance that wants to make a trace entry.
425      * @param   method          Method of that object.
426      * @param   param           Parameter to trace.
427      * @param   value           Value of parameter.
428      */
429     public static void param(final Class tracingClass, final Object tracingObject,
430             final String method, final String param, final boolean value) {
431         if (traceOn) {
432             final Log log = LogFactory.getFactory().getInstance(tracingClass);
433             if (log.isDebugEnabled()) {
434                 log.debug("." + method + " " + param + "=" + value);
435             }
436         }
437     }
438 
439     /**
440      * Trace parameter.
441      *
442      * @param   tracingClass    Class that wants to make a trace entry.
443      * @param   method          Method of that class.
444      * @param   param           Parameter to trace.
445      * @param   value           Value of parameter.
446      */
447     public static void param(final Class tracingClass, final String method,
448             final String param, final boolean value) {
449         if (traceOn) {
450             final Log log = LogFactory.getFactory().getInstance(tracingClass);
451             if (log.isDebugEnabled()) {
452                 log.debug("." + method + " " + param + "=" + value);
453             }
454         }
455     }
456 
457     /**
458      * Write stacktrace into trace if debug level is on.
459      *
460      * @param   tracingClass    Class that wants to make a trace entry.
461      * @param   tracingObject   Instance that wants to make a trace entry.
462      * @param   method          Method of that object.
463      */
464     public static void traceStack(final Class tracingClass, final Object tracingObject,
465             final String method) {
466         if (traceOn) {
467             final Log log = LogFactory.getFactory().getInstance(tracingClass);
468             if (!log.isDebugEnabled()) {
469                 return;
470             }
471             try {
472                 throw new Exception("Stacktrace");
473             } catch (Exception e) {
474                 log.debug("." + method + " " + e, e);
475             }
476         }
477     }
478 
479     /**
480      * Write stacktrace into trace if debug level is on.
481      *
482      * @param   tracingClass    Class that wants to make a trace entry.
483      * @param   method          Method of that class.
484      */
485     public static final void traceStack(final Class tracingClass, final String method) {
486         if (traceOn) {
487             final Log log = LogFactory.getFactory().getInstance(tracingClass);
488             if (!log.isDebugEnabled()) {
489                 return;
490             }
491             try {
492                 throw new Exception("Stacktrace");
493             } catch (Exception e) {
494                 log.debug("." + method + " " + e, e);
495             }
496         }
497     }
498 
499     /**
500      * Parameter information.
501      *
502      * @param   tracingClass    Class that wants to make a trace entry.
503      * @param   tracingObject   Instance that wants to make an info entry.
504      * @param   method          Method of that object.
505      * @param   param           Parameter to trace.
506      * @param   value           Value of parameter.
507      */
508     public static void paramInfo(final Class tracingClass, final Object tracingObject,
509             final String method, final String param, final Object value) {
510         if (traceOn) {
511             final Log log = LogFactory.getFactory().getInstance(tracingClass);
512             if (log.isInfoEnabled()) {
513                 log.info("." + method + " " + param + "=" + value);
514             }
515         }
516     }
517 
518     /**
519      * Parameter information.
520      *
521      * @param   tracingClass    Class that wants to make an info entry.
522      * @param   method          Method of that class.
523      * @param   param           Parameter to trace.
524      * @param   value           Value of parameter.
525      */
526     public static void paramInfo(final Class tracingClass, final String method,
527             final String param, final Object value) {
528         if (traceOn) {
529             final Log log = LogFactory.getFactory().getInstance(tracingClass);
530             if (log.isInfoEnabled()) {
531                 log.info("." + method + " " + param + "=" + value);
532             }
533         }
534     }
535 
536     /**
537      * Parameter information.
538      * @param   tracingClass    Class that wants to make an info entry.
539      * @param   tracingObject   Instance that wants to make an info entry.
540      * @param   method          Method of that object.
541      * @param   param           Parameter to trace.
542      * @param   value           Value of parameter.
543      */
544     public static void paramInfo(final Class tracingClass, final Object tracingObject,
545             final String method, final String param, final int value) {
546         if (traceOn) {
547             final Log log = LogFactory.getFactory().getInstance(tracingClass);
548             if (log.isInfoEnabled()) {
549                 log.info("." + method + " " + param + "=" + value);
550             }
551         }
552     }
553 
554     /**
555      * Parameter information.
556      *
557      * @param   tracingClass    Class that wants to make an info entry.
558      * @param   method          Method of that class.
559      * @param   param           Parameter to trace.
560      * @param   value           Value of parameter.
561      */
562     public static void paramInfo(final Class tracingClass, final String method,
563             final String param, final int value) {
564         if (traceOn) {
565             final Log log = LogFactory.getFactory().getInstance(tracingClass);
566             if (log.isInfoEnabled()) {
567                 log.info("." + method + " " + param + "=" + value);
568             }
569         }
570     }
571 
572     /**
573      * Parameter information.
574      * @param   tracingClass    Class that wants to make an info entry.
575      * @param   tracingObject   Instance that wants to make an info entry.
576      * @param   method          Method of that object.
577      * @param   param           Parameter to trace.
578      * @param   value           Value of parameter.
579      */
580     public static void paramInfo(final Class tracingClass, final Object tracingObject,
581             final String method, final String param, final boolean value) {
582         if (traceOn) {
583             final Log log = LogFactory.getFactory().getInstance(tracingClass);
584             if (log.isInfoEnabled()) {
585                 log.info("." + method + " " + param + "=" + value);
586             }
587         }
588     }
589 
590     /**
591      * Parameter information.
592      *
593      * @param   tracingClass    Class that wants to make an info entry.
594      * @param   method          Method of that class.
595      * @param   param           Parameter to trace.
596      * @param   value           Value of parameter.
597      */
598     public static void paramInfo(final Class tracingClass, final String method,
599             final String param, final boolean value) {
600         if (traceOn) {
601             final Log log = LogFactory.getFactory().getInstance(tracingClass);
602             if (log.isInfoEnabled()) {
603                 log.info("." + method + " " + param + "=" + value);
604             }
605         }
606     }
607 
608 }