1 package be
.nikiroo
.utils
;
4 * A handler when a trace message is sent or when a recoverable exception was
5 * caught by the program.
9 public class TraceHandler
{
10 private final boolean showErrors
;
11 private final boolean showErrorDetails
;
12 private final int traceLevel
;
15 * Create a default {@link TraceHandler} that will print errors on stderr
16 * (without details) and no traces.
18 public TraceHandler() {
19 this(true, false, false);
23 * Create a default {@link TraceHandler}.
26 * show errors on stderr
27 * @param showErrorDetails
28 * show more details when printing errors
30 * show level 1 traces on stdout, or no traces at all
32 public TraceHandler(boolean showErrors
, boolean showErrorDetails
,
34 this(showErrors
, showErrorDetails
, showTraces ?
1 : 0);
38 * Create a default {@link TraceHandler}.
41 * show errors on stderr
42 * @param showErrorDetails
43 * show more details when printing errors
45 * show traces of this level or lower (0 means "no traces",
46 * higher means more traces)
48 public TraceHandler(boolean showErrors
, boolean showErrorDetails
,
50 this.showErrors
= showErrors
;
51 this.showErrorDetails
= showErrorDetails
;
52 this.traceLevel
= Math
.max(traceLevel
, 0);
56 * The trace level of this {@link TraceHandler}.
60 public int getTraceLevel() {
65 * An exception happened, log it.
70 public void error(Exception e
) {
72 if (showErrorDetails
) {
75 error(e
.getMessage());
81 * An error happened, log it.
86 public void error(String message
) {
88 System
.err
.println(message
);
93 * A trace happened, show it.
95 * By default, will only be effective if {@link TraceHandler#showTraces} is
98 * A call to this method is equivalent to a call to
99 * {@link TraceHandler#trace(String, int)} with a level of 1.
104 public void trace(String message
) {
109 * A trace happened, show it.
111 * By default, will only be effective if {@link TraceHandler#showTraces} is
112 * true and the level is lower or equal to {@link TraceHandler#traceLevel}.
119 public void trace(String message
, int level
) {
120 if (traceLevel
> 0 && level
<= traceLevel
) {
121 System
.out
.println(message
);