Trace.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.base.trace;
017 
018 import org.apache.commons.logging.Log;
019 import org.apache.commons.logging.LogFactory;
020 
021 
022 /**
023  * Developer trace.
024  *
025  @author  Michael Meyling
026  */
027 public final class Trace {
028 
029     /** Logger for business messages. */
030     private static final Log BUSINESS = LogFactory.getFactory().getInstance("log");
031 
032     /** Is tracing on? If not only fatal errors and business messages are logged. */
033     private static boolean traceOn = false;
034 
035     /**
036      * Constructor.
037      */
038     private Trace() {
039         // don't call me
040     }
041 
042     /**
043      * Set tracing on. If not set only fatal errors and business messages are logged.
044      *
045      @param   on  Set tracing on?
046      */
047     public static void setTraceOn(final boolean on) {
048         traceOn = on;
049     }
050 
051     /**
052      * Trace business log message. The message is logged on "error" level.
053      *
054      @param   message         Business log message.
055      */
056     public static void log(final String message) {
057         BUSINESS.error(message);
058     }
059 
060     /**
061      * Trace business log message. The message is logged on "error" level.
062      *
063      @param   message         Business log message.
064      @param   additional      Extra info for the next line.
065      */
066     public static void log(final String message, final String additional) {
067         if (BUSINESS.isErrorEnabled()) {
068             BUSINESS.error(message);
069             BUSINESS.error("    " + additional);
070         }
071     }
072 
073     /**
074      * Trace business log message. The message is logged on "error" level.
075      *
076      @param   message         Business log message.
077      @param   additional      Extra info for the next line.
078      @param   description     Further description.
079      */
080     public static void log(final String message, final String additional, final String description) {
081         if (BUSINESS.isErrorEnabled()) {
082             BUSINESS.error(message);
083             BUSINESS.error("    " + additional);
084             BUSINESS.error("    " + description);
085         }
086     }
087 
088     /**
089      * Is debug log currently enabled?
090      *
091      @param   tracingClass    Class we want to know the debug logging status for.
092      @return  Debug log enabled.
093      */
094     public static boolean isDebugEnabled(final Class tracingClass) {
095         if (traceOn) {
096             final Log log = LogFactory.getFactory().getInstance(tracingClass);
097             return log.isDebugEnabled();
098         else {
099             return false;
100         }
101     }
102 
103     /**
104      * Trace object.
105      @param   tracingClass    Class that wants to make a trace entry.
106      @param   tracingObject   Instance that wants to make a trace entry.
107      @param   method          Method of that object.
108      @param   object          Object to trace.
109      */
110     public static void trace(final Class tracingClass, final Object tracingObject,
111             final String method, final Object object) {
112         if (traceOn) {
113             final Log log = LogFactory.getFactory().getInstance(tracingClass);
114             if (log.isDebugEnabled()) {
115                 log.debug("." + method + " " + object);
116             }
117         }
118     }
119 
120     /**
121      * Trace object.
122      *
123      @param   tracingClass    Class that wants to make a trace entry.
124      @param   method          Method of that class.
125      @param   object          Object to trace.
126      */
127     public static void trace(final Class tracingClass, final String method,
128             final Object object) {
129         if (traceOn) {
130             final Log log = LogFactory.getFactory().getInstance(tracingClass);
131             if (log.isDebugEnabled()) {
132                 log.debug("." + method + " " + object);
133             }
134         }
135     }
136 
137     /**
138      * Trace throwable.
139      *
140      @param   tracingClass    Class that wants to make a trace entry.
141      @param   tracingObject   Instance that wants to make a trace entry.
142      @param   method          Method of that object.
143      @param   throwable       Throwable to trace.
144      */
145     public static void trace(final Class tracingClass, final Object tracingObject,
146             final String method, final Throwable throwable) {
147         if (traceOn) {
148             final Log log = LogFactory.getFactory().getInstance(tracingClass);
149             if (log.isDebugEnabled()) {
150                 log.debug("." + method + " " + throwable, throwable);
151             }
152         }
153     }
154 
155     /**
156      * Trace throwable.
157      *
158      @param   tracingClass    Class that wants to make a trace entry.
159      @param   method          Method of that class.
160      @param   throwable       Throwable to trace.
161      */
162     public static void trace(final Class tracingClass, final String method,
163             final Throwable throwable) {
164         if (traceOn) {
165             final Log log = LogFactory.getFactory().getInstance(tracingClass);
166             if (log.isDebugEnabled()) {
167                 log.debug("." + method + " " + throwable, throwable);
168             }
169         }
170     }
171 
172     /**
173      * Trace fatal throwable and extra description.
174      *
175      @param   tracingClass    Class that wants to make a trace entry.
176      @param   tracingObject   Instance that wants to make a trace entry.
177      @param   method          Method of that object.
178      @param   description     Further information.
179      @param   throwable       Throwable to trace.
180      */
181     public static void fatal(final Class tracingClass, final Object tracingObject,
182             final String method, final String description, final Throwable throwable) {
183         final Log log = LogFactory.getFactory().getInstance(tracingClass);
184         log.fatal("." + method + " " + description, throwable);
185     }
186 
187     /**
188      * Trace fatal throwable and extra description.
189      *
190      @param   tracingClass    Class that wants to make a trace entry.
191      @param   method          Method of that class.
192      @param   description     Further information.
193      @param   throwable       Throwable to trace.
194      */
195     public static void fatal(final Class tracingClass, final String method,
196             final String description, final Throwable throwable) {
197         final Log log = LogFactory.getFactory().getInstance(tracingClass);
198         log.fatal("." + method + " " + description, throwable);
199     }
200 
201     /**
202      * Trace throwable and extra description.
203      *
204      @param   tracingClass    Class that wants to make a trace entry.
205      @param   tracingObject   Instance that wants to make a trace entry.
206      @param   method          Method of that object.
207      @param   description     Further information.
208      @param   throwable       Throwable to trace.
209      */
210     public static void trace(final Class tracingClass, final Object tracingObject,
211             final String method, final String description, final Throwable throwable) {
212         if (traceOn) {
213             final Log log = LogFactory.getFactory().getInstance(tracingClass);
214             if (log.isDebugEnabled()) {
215                 log.debug("." + method + " " + description, throwable);
216             }
217         }
218     }
219 
220     /**
221      * Trace throwable and extra description.
222      *
223      @param   tracingClass    Class that wants to make a trace entry.
224      @param   method          Method of that class.
225      @param   description     Further information.
226      @param   throwable       Throwable to trace.
227      */
228     public static void trace(final Class tracingClass, final String method,
229             final String description, final Throwable throwable) {
230         if (traceOn) {
231             final Log log = LogFactory.getFactory().getInstance(tracingClass);
232             if (log.isDebugEnabled()) {
233                 log.debug("." + method + " " + description, throwable);
234             }
235         }
236     }
237 
238     /**
239      * Trace method begin. Should be followed by an analogous
240      {@link #end(Class, Object, String)} call.
241      *
242      @param   tracingClass    Class that wants to make a trace entry.
243      @param   tracingObject   Instance that wants to make a trace entry.
244      @param   method          Method of that object.
245      */
246     public static void begin(final Class tracingClass, final Object tracingObject,
247             final String method) {
248         if (traceOn) {
249             final Log log = LogFactory.getFactory().getInstance(tracingClass);
250             if (log.isDebugEnabled()) {
251                 log.debug("." + method + " " "begin");
252             }
253         }
254     }
255 
256     /**
257      * Trace method begin. Should be followed by an analogous {@link #end(Class, String)} call.
258      *
259      @param   tracingClass    Class that wants to make a trace entry.
260      @param   method          Method of that class.
261      */
262     public static void begin(final Class tracingClass, final String method) {
263         if (traceOn) {
264             final Log log = LogFactory.getFactory().getInstance(tracingClass);
265             if (log.isDebugEnabled()) {
266                 log.debug("." + method + " " "begin");
267             }
268         }
269     }
270 
271     /**
272      * Trace method end.
273      *
274      @param   tracingClass    Class that wants to make a trace entry.
275      @param   tracingObject   Instance that wants to make a trace entry.
276      @param   method          Method of that object.
277      */
278     public static void end(final Class tracingClass, final Object tracingObject,
279             final String method) {
280         if (traceOn) {
281             final Log log = LogFactory.getFactory().getInstance(tracingClass);
282             if (log.isDebugEnabled()) {
283                 log.debug("." + method + " " "end");
284             }
285         }
286     }
287 
288     /**
289      * Trace method end.
290      *
291      @param   tracingClass    Class that wants to make a trace entry.
292      @param   method          Method of that class.
293      */
294     public static void end(final Class tracingClass, final String method) {
295         if (traceOn) {
296             final Log log = LogFactory.getFactory().getInstance(tracingClass);
297             if (log.isDebugEnabled()) {
298                 log.debug("." + method + " " "end");
299             }
300         }
301     }
302 
303     /**
304      * Trace message.
305      *
306      @param   tracingClass    Class that wants to make a trace entry.
307      @param   tracingObject   Instance that wants to make a trace entry.
308      @param   method          Method of that object.
309      @param   message         Message.
310      */
311     public static void info(final Class tracingClass, final Object tracingObject,
312             final String method, final String message) {
313         if (traceOn) {
314             final Log log = LogFactory.getFactory().getInstance(tracingClass);
315             if (log.isInfoEnabled()) {
316                 log.info("." + method + " " + message);
317             }
318         }
319     }
320 
321     /**
322      * Trace method message.
323      *
324      @param   tracingClass    Class that wants to make a trace entry.
325      @param   method          Method of that class.
326      @param   message         Message.
327      */
328     public static void info(final Class tracingClass, final String method,
329             final String message) {
330         if (traceOn) {
331             final Log log = LogFactory.getFactory().getInstance(tracingClass);
332             if (log.isInfoEnabled()) {
333                 log.info("." + method + " " + message);
334             }
335         }
336     }
337 
338     /**
339      * Trace parameter.
340      *
341      @param   tracingClass    Class that wants to make a trace entry.
342      @param   tracingObject   Instance that wants to make a trace entry.
343      @param   method          Method of that object.
344      @param   param           Parameter to trace.
345      @param   value           Value of parameter.
346      */
347     public static void param(final Class tracingClass, final Object tracingObject,
348             final String method, final String param, final Object value) {
349         if (traceOn) {
350             final Log log = LogFactory.getFactory().getInstance(tracingClass);
351             if (log.isDebugEnabled()) {
352                 log.debug("." + method + " " + param + "=" + value);
353             }
354         }
355     }
356 
357     /**
358      * Trace parameter.
359      *
360      @param   tracingClass    Class that wants to make a trace entry.
361      @param   method          Method of that class.
362      @param   param           Parameter to trace.
363      @param   value           Value of parameter.
364      */
365     public static void param(final Class tracingClass, final String method,
366             final String param, final Object value) {
367         if (traceOn) {
368             final Log log = LogFactory.getFactory().getInstance(tracingClass);
369             if (log.isDebugEnabled()) {
370                 log.debug("." + method + " " + param + "=" + value);
371             }
372         }
373     }
374 
375     /**
376      * Trace parameter.
377      *
378      @param   tracingClass    Class that wants to make a trace entry.
379      @param   tracingObject   Instance that wants to make a trace entry.
380      @param   method          Method of that object.
381      @param   param           Parameter to trace.
382      @param   value           Value of parameter.
383      */
384     public static void param(final Class tracingClass, final Object tracingObject,
385             final String method, final String param, final int value) {
386         if (traceOn) {
387             final Log log = LogFactory.getFactory().getInstance(tracingClass);
388             if (log.isDebugEnabled()) {
389                 log.debug("." + method + " " + param + "=" + value);
390             }
391         }
392     }
393 
394     /**
395      * Trace parameter.
396      *
397      @param   tracingClass    Class that wants to make a trace entry.
398      @param   method          Method of that class.
399      @param   param           Parameter to trace.
400      @param   value           Value of parameter.
401      */
402     public static void param(final Class tracingClass, final String method,
403             final String param, final int value) {
404         if (traceOn) {
405             final Log log = LogFactory.getFactory().getInstance(tracingClass);
406             if (log.isDebugEnabled()) {
407                 log.debug("." + method + " " + param + "=" + value);
408             }
409         }
410     }
411 
412     /**
413      * Trace parameter.
414      *
415      @param   tracingClass    Class that wants to make a trace entry.
416      @param   tracingObject   Instance that wants to make a trace entry.
417      @param   method          Method of that object.
418      @param   param           Parameter to trace.
419      @param   value           Value of parameter.
420      */
421     public static void param(final Class tracingClass, final Object tracingObject,
422             final String method, final String param, final boolean value) {
423         if (traceOn) {
424             final Log log = LogFactory.getFactory().getInstance(tracingClass);
425             if (log.isDebugEnabled()) {
426                 log.debug("." + method + " " + param + "=" + value);
427             }
428         }
429     }
430 
431     /**
432      * Trace parameter.
433      *
434      @param   tracingClass    Class that wants to make a trace entry.
435      @param   method          Method of that class.
436      @param   param           Parameter to trace.
437      @param   value           Value of parameter.
438      */
439     public static void param(final Class tracingClass, final String method,
440             final String param, final boolean value) {
441         if (traceOn) {
442             final Log log = LogFactory.getFactory().getInstance(tracingClass);
443             if (log.isDebugEnabled()) {
444                 log.debug("." + method + " " + param + "=" + value);
445             }
446         }
447     }
448 
449     /**
450      * Write stacktrace into trace if debug level is on.
451      *
452      @param   tracingClass    Class that wants to make a trace entry.
453      @param   tracingObject   Instance that wants to make a trace entry.
454      @param   method          Method of that object.
455      */
456     public static void traceStack(final Class tracingClass, final Object tracingObject,
457             final String method) {
458         if (traceOn) {
459             final Log log = LogFactory.getFactory().getInstance(tracingClass);
460             if (!log.isDebugEnabled()) {
461                 return;
462             }
463             try {
464                 throw new Exception("Stacktrace");
465             catch (Exception e) {
466                 log.debug("." + method + " " + e, e);
467             }
468         }
469     }
470 
471     /**
472      * Write stacktrace into trace if debug level is on.
473      *
474      @param   tracingClass    Class that wants to make a trace entry.
475      @param   method          Method of that class.
476      */
477     public static final void traceStack(final Class tracingClass, final String method) {
478         if (traceOn) {
479             final Log log = LogFactory.getFactory().getInstance(tracingClass);
480             if (!log.isDebugEnabled()) {
481                 return;
482             }
483             try {
484                 throw new Exception("Stacktrace");
485             catch (Exception e) {
486                 log.debug("." + method + " " + e, e);
487             }
488         }
489     }
490 
491     /**
492      * Parameter information.
493      *
494      @param   tracingClass    Class that wants to make a trace entry.
495      @param   tracingObject   Instance that wants to make an info entry.
496      @param   method          Method of that object.
497      @param   param           Parameter to trace.
498      @param   value           Value of parameter.
499      */
500     public static void paramInfo(final Class tracingClass, final Object tracingObject,
501             final String method, final String param, final Object value) {
502         if (traceOn) {
503             final Log log = LogFactory.getFactory().getInstance(tracingClass);
504             if (log.isInfoEnabled()) {
505                 log.info("." + method + " " + param + "=" + value);
506             }
507         }
508     }
509 
510     /**
511      * Parameter information.
512      *
513      @param   tracingClass    Class that wants to make an info entry.
514      @param   method          Method of that class.
515      @param   param           Parameter to trace.
516      @param   value           Value of parameter.
517      */
518     public static void paramInfo(final Class tracingClass, final String method,
519             final String param, final Object value) {
520         if (traceOn) {
521             final Log log = LogFactory.getFactory().getInstance(tracingClass);
522             if (log.isInfoEnabled()) {
523                 log.info("." + method + " " + param + "=" + value);
524             }
525         }
526     }
527 
528     /**
529      * Parameter information.
530      @param   tracingClass    Class that wants to make an info entry.
531      @param   tracingObject   Instance that wants to make an info entry.
532      @param   method          Method of that object.
533      @param   param           Parameter to trace.
534      @param   value           Value of parameter.
535      */
536     public static void paramInfo(final Class tracingClass, final Object tracingObject,
537             final String method, final String param, final int value) {
538         if (traceOn) {
539             final Log log = LogFactory.getFactory().getInstance(tracingClass);
540             if (log.isInfoEnabled()) {
541                 log.info("." + method + " " + param + "=" + value);
542             }
543         }
544     }
545 
546     /**
547      * Parameter information.
548      *
549      @param   tracingClass    Class that wants to make an info entry.
550      @param   method          Method of that class.
551      @param   param           Parameter to trace.
552      @param   value           Value of parameter.
553      */
554     public static void paramInfo(final Class tracingClass, final String method,
555             final String param, final int value) {
556         if (traceOn) {
557             final Log log = LogFactory.getFactory().getInstance(tracingClass);
558             if (log.isInfoEnabled()) {
559                 log.info("." + method + " " + param + "=" + value);
560             }
561         }
562     }
563 
564     /**
565      * Parameter information.
566      @param   tracingClass    Class that wants to make an info entry.
567      @param   tracingObject   Instance that wants to make an info entry.
568      @param   method          Method of that object.
569      @param   param           Parameter to trace.
570      @param   value           Value of parameter.
571      */
572     public static void paramInfo(final Class tracingClass, final Object tracingObject,
573             final String method, final String param, final boolean value) {
574         if (traceOn) {
575             final Log log = LogFactory.getFactory().getInstance(tracingClass);
576             if (log.isInfoEnabled()) {
577                 log.info("." + method + " " + param + "=" + value);
578             }
579         }
580     }
581 
582     /**
583      * Parameter information.
584      *
585      @param   tracingClass    Class that wants to make an info entry.
586      @param   method          Method of that class.
587      @param   param           Parameter to trace.
588      @param   value           Value of parameter.
589      */
590     public static void paramInfo(final Class tracingClass, final String method,
591             final String param, final boolean value) {
592         if (traceOn) {
593             final Log log = LogFactory.getFactory().getInstance(tracingClass);
594             if (log.isInfoEnabled()) {
595                 log.info("." + method + " " + param + "=" + value);
596             }
597         }
598     }
599 
600 }