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) { | |
96c3f629 NR |
94 | long now = System.currentTimeMillis(); |
95 | System.err.print(StringUtils.fromTime(now) + ": "); | |
08a58812 NR |
96 | e.printStackTrace(); |
97 | } else { | |
d5026c09 | 98 | error(e.toString()); |
08a58812 | 99 | } |
530d4062 NR |
100 | } |
101 | } | |
102 | ||
103 | /** | |
104 | * An error happened, log it. | |
105 | * | |
106 | * @param message | |
107 | * the error message | |
108 | */ | |
109 | public void error(String message) { | |
08a58812 | 110 | if (showErrors) { |
96c3f629 NR |
111 | long now = System.currentTimeMillis(); |
112 | System.err.println(StringUtils.fromTime(now) + ": " + message); | |
08a58812 | 113 | } |
530d4062 NR |
114 | } |
115 | ||
116 | /** | |
117 | * A trace happened, show it. | |
118 | * <p> | |
217a3310 NR |
119 | * By default, will only be effective if {@link TraceHandler#traceLevel} is |
120 | * not 0. | |
8537d55a NR |
121 | * <p> |
122 | * A call to this method is equivalent to a call to | |
123 | * {@link TraceHandler#trace(String, int)} with a level of 1. | |
530d4062 NR |
124 | * |
125 | * @param message | |
126 | * the trace message | |
127 | */ | |
128 | public void trace(String message) { | |
8537d55a NR |
129 | trace(message, 1); |
130 | } | |
131 | ||
132 | /** | |
133 | * A trace happened, show it. | |
134 | * <p> | |
217a3310 NR |
135 | * By default, will only be effective if {@link TraceHandler#traceLevel} is |
136 | * not 0 and the level is lower or equal to it. | |
8537d55a NR |
137 | * |
138 | * @param message | |
139 | * the trace message | |
140 | * @param level | |
141 | * the trace level | |
142 | */ | |
143 | public void trace(String message, int level) { | |
144 | if (traceLevel > 0 && level <= traceLevel) { | |
96c3f629 NR |
145 | long now = System.currentTimeMillis(); |
146 | System.err.print(StringUtils.fromTime(now) + ": "); | |
217a3310 | 147 | if (maxPrintSize > 0 && message.length() > maxPrintSize) { |
96c3f629 | 148 | |
e361afe4 | 149 | System.err |
217a3310 NR |
150 | .println(message.substring(0, maxPrintSize) + "[...]"); |
151 | } else { | |
e361afe4 | 152 | System.err.println(message); |
217a3310 | 153 | } |
530d4062 NR |
154 | } |
155 | } | |
156 | } |