Commit | Line | Data |
---|---|---|
530d4062 NR |
1 | package be.nikiroo.utils; |
2 | ||
3 | /** | |
4 | * A handler when a trace message is sent or when a recoverable exception was | |
5 | * caught by the program. | |
6 | * | |
7 | * @author niki | |
8 | */ | |
9 | public class TraceHandler { | |
8537d55a NR |
10 | private final boolean showErrors; |
11 | private final boolean showErrorDetails; | |
12 | private final int traceLevel; | |
217a3310 | 13 | private final int maxPrintSize; |
530d4062 NR |
14 | |
15 | /** | |
08a58812 NR |
16 | * Create a default {@link TraceHandler} that will print errors on stderr |
17 | * (without details) and no traces. | |
530d4062 | 18 | */ |
08a58812 NR |
19 | public TraceHandler() { |
20 | this(true, false, false); | |
530d4062 NR |
21 | } |
22 | ||
23 | /** | |
08a58812 | 24 | * Create a default {@link TraceHandler}. |
530d4062 | 25 | * |
08a58812 NR |
26 | * @param showErrors |
27 | * show errors on stderr | |
530d4062 | 28 | * @param showErrorDetails |
08a58812 | 29 | * show more details when printing errors |
530d4062 | 30 | * @param showTraces |
e361afe4 | 31 | * show level 1 traces on stderr, or no traces at all |
530d4062 | 32 | */ |
08a58812 NR |
33 | public TraceHandler(boolean showErrors, boolean showErrorDetails, |
34 | boolean showTraces) { | |
8537d55a NR |
35 | this(showErrors, showErrorDetails, showTraces ? 1 : 0); |
36 | } | |
37 | ||
38 | /** | |
39 | * Create a default {@link TraceHandler}. | |
40 | * | |
41 | * @param showErrors | |
42 | * show errors on stderr | |
43 | * @param showErrorDetails | |
44 | * show more details when printing errors | |
45 | * @param traceLevel | |
46 | * show traces of this level or lower (0 means "no traces", | |
47 | * higher means more traces) | |
48 | */ | |
49 | public TraceHandler(boolean showErrors, boolean showErrorDetails, | |
50 | int traceLevel) { | |
217a3310 NR |
51 | this(showErrors, showErrorDetails, traceLevel, -1); |
52 | } | |
53 | ||
54 | /** | |
55 | * Create a default {@link TraceHandler}. | |
56 | * | |
57 | * @param showErrors | |
58 | * show errors on stderr | |
59 | * @param showErrorDetails | |
60 | * show more details when printing errors | |
61 | * @param traceLevel | |
62 | * show traces of this level or lower (0 means "no traces", | |
63 | * higher means more traces) | |
64 | * @param maxPrintSize | |
65 | * the maximum size at which to truncate traces data (or -1 for | |
66 | * "no limit") | |
67 | */ | |
68 | public TraceHandler(boolean showErrors, boolean showErrorDetails, | |
69 | int traceLevel, int maxPrintSize) { | |
08a58812 NR |
70 | this.showErrors = showErrors; |
71 | this.showErrorDetails = showErrorDetails; | |
8537d55a | 72 | this.traceLevel = Math.max(traceLevel, 0); |
217a3310 | 73 | this.maxPrintSize = maxPrintSize; |
8537d55a NR |
74 | } |
75 | ||
76 | /** | |
77 | * The trace level of this {@link TraceHandler}. | |
78 | * | |
79 | * @return the level | |
80 | */ | |
81 | public int getTraceLevel() { | |
82 | return traceLevel; | |
530d4062 NR |
83 | } |
84 | ||
85 | /** | |
86 | * An exception happened, log it. | |
87 | * | |
88 | * @param e | |
89 | * the exception | |
90 | */ | |
91 | public void error(Exception e) { | |
08a58812 NR |
92 | if (showErrors) { |
93 | if (showErrorDetails) { | |
94 | e.printStackTrace(); | |
95 | } else { | |
96 | error(e.getMessage()); | |
97 | } | |
530d4062 NR |
98 | } |
99 | } | |
100 | ||
101 | /** | |
102 | * An error happened, log it. | |
103 | * | |
104 | * @param message | |
105 | * the error message | |
106 | */ | |
107 | public void error(String message) { | |
08a58812 NR |
108 | if (showErrors) { |
109 | System.err.println(message); | |
110 | } | |
530d4062 NR |
111 | } |
112 | ||
113 | /** | |
114 | * A trace happened, show it. | |
115 | * <p> | |
217a3310 NR |
116 | * By default, will only be effective if {@link TraceHandler#traceLevel} is |
117 | * not 0. | |
8537d55a NR |
118 | * <p> |
119 | * A call to this method is equivalent to a call to | |
120 | * {@link TraceHandler#trace(String, int)} with a level of 1. | |
530d4062 NR |
121 | * |
122 | * @param message | |
123 | * the trace message | |
124 | */ | |
125 | public void trace(String message) { | |
8537d55a NR |
126 | trace(message, 1); |
127 | } | |
128 | ||
129 | /** | |
130 | * A trace happened, show it. | |
131 | * <p> | |
217a3310 NR |
132 | * By default, will only be effective if {@link TraceHandler#traceLevel} is |
133 | * not 0 and the level is lower or equal to it. | |
8537d55a NR |
134 | * |
135 | * @param message | |
136 | * the trace message | |
137 | * @param level | |
138 | * the trace level | |
139 | */ | |
140 | public void trace(String message, int level) { | |
141 | if (traceLevel > 0 && level <= traceLevel) { | |
217a3310 | 142 | if (maxPrintSize > 0 && message.length() > maxPrintSize) { |
e361afe4 | 143 | System.err |
217a3310 NR |
144 | .println(message.substring(0, maxPrintSize) + "[...]"); |
145 | } else { | |
e361afe4 | 146 | System.err.println(message); |
217a3310 | 147 | } |
530d4062 NR |
148 | } |
149 | } | |
150 | } |