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
;
13 private final int maxPrintSize
;
16 * Create a default {@link TraceHandler} that will print errors on stderr
17 * (without details) and no traces.
19 public TraceHandler() {
20 this(true, false, false);
24 * Create a default {@link TraceHandler}.
27 * show errors on stderr
28 * @param showErrorDetails
29 * show more details when printing errors
31 * show level 1 traces on stderr, or no traces at all
33 public TraceHandler(boolean showErrors
, boolean showErrorDetails
,
35 this(showErrors
, showErrorDetails
, showTraces ?
1 : 0);
39 * Create a default {@link TraceHandler}.
42 * show errors on stderr
43 * @param showErrorDetails
44 * show more details when printing errors
46 * show traces of this level or lower (0 means "no traces",
47 * higher means more traces)
49 public TraceHandler(boolean showErrors
, boolean showErrorDetails
,
51 this(showErrors
, showErrorDetails
, traceLevel
, -1);
55 * Create a default {@link TraceHandler}.
58 * show errors on stderr
59 * @param showErrorDetails
60 * show more details when printing errors
62 * show traces of this level or lower (0 means "no traces",
63 * higher means more traces)
65 * the maximum size at which to truncate traces data (or -1 for
68 public TraceHandler(boolean showErrors
, boolean showErrorDetails
,
69 int traceLevel
, int maxPrintSize
) {
70 this.showErrors
= showErrors
;
71 this.showErrorDetails
= showErrorDetails
;
72 this.traceLevel
= Math
.max(traceLevel
, 0);
73 this.maxPrintSize
= maxPrintSize
;
77 * The trace level of this {@link TraceHandler}.
81 public int getTraceLevel() {
86 * An exception happened, log it.
91 public void error(Exception e
) {
93 if (showErrorDetails
) {
94 long now
= System
.currentTimeMillis();
95 System
.err
.print(StringUtils
.fromTime(now
) + ": ");
104 * An error happened, log it.
109 public void error(String message
) {
111 long now
= System
.currentTimeMillis();
112 System
.err
.println(StringUtils
.fromTime(now
) + ": " + message
);
117 * A trace happened, show it.
119 * By default, will only be effective if {@link TraceHandler#traceLevel} is
122 * A call to this method is equivalent to a call to
123 * {@link TraceHandler#trace(String, int)} with a level of 1.
128 public void trace(String message
) {
133 * A trace happened, show it.
135 * By default, will only be effective if {@link TraceHandler#traceLevel} is
136 * not 0 and the level is lower or equal to it.
143 public void trace(String message
, int level
) {
144 if (traceLevel
> 0 && level
<= traceLevel
) {
145 long now
= System
.currentTimeMillis();
146 System
.err
.print(StringUtils
.fromTime(now
) + ": ");
147 if (maxPrintSize
> 0 && message
.length() > maxPrintSize
) {
150 .println(message
.substring(0, maxPrintSize
) + "[...]");
152 System
.err
.println(message
);