001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2014, 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 * Is tracing on? If not set only fatal errors and business messages are logged.
053 *
054 * @return Is tracing on?
055 */
056 public static boolean isTraceOn() {
057 return traceOn;
058 }
059
060 /**
061 * Trace business log message. The message is logged on "error" level.
062 *
063 * @param message Business log message.
064 */
065 public static void log(final String message) {
066 BUSINESS.error(message);
067 }
068
069 /**
070 * Trace business log message. The message is logged on "error" level.
071 *
072 * @param message Business log message.
073 * @param additional Extra info for the next line.
074 */
075 public static void log(final String message, final String additional) {
076 if (BUSINESS.isErrorEnabled()) {
077 BUSINESS.error(message);
078 BUSINESS.error(" " + additional);
079 }
080 }
081
082 /**
083 * Trace business log message. The message is logged on "error" level.
084 *
085 * @param message Business log message.
086 * @param additional Extra info for the next line.
087 * @param description Further description.
088 */
089 public static void log(final String message, final String additional, final String description) {
090 if (BUSINESS.isErrorEnabled()) {
091 BUSINESS.error(message);
092 BUSINESS.error(" " + additional);
093 BUSINESS.error(" " + description);
094 }
095 }
096
097 /**
098 * Is debug log currently enabled?
099 *
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 }
|