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 { | |
08a58812 | 10 | private boolean showErrors; |
530d4062 | 11 | private boolean showTraces; |
08a58812 | 12 | private boolean showErrorDetails; |
530d4062 NR |
13 | |
14 | /** | |
08a58812 NR |
15 | * Create a default {@link TraceHandler} that will print errors on stderr |
16 | * (without details) and no traces. | |
530d4062 | 17 | */ |
08a58812 NR |
18 | public TraceHandler() { |
19 | this(true, false, false); | |
530d4062 NR |
20 | } |
21 | ||
22 | /** | |
08a58812 | 23 | * Create a default {@link TraceHandler}. |
530d4062 | 24 | * |
08a58812 NR |
25 | * @param showErrors |
26 | * show errors on stderr | |
530d4062 | 27 | * @param showErrorDetails |
08a58812 | 28 | * show more details when printing errors |
530d4062 | 29 | * @param showTraces |
08a58812 | 30 | * show traces on stdout |
530d4062 | 31 | */ |
08a58812 NR |
32 | public TraceHandler(boolean showErrors, boolean showErrorDetails, |
33 | boolean showTraces) { | |
34 | this.showErrors = showErrors; | |
35 | this.showErrorDetails = showErrorDetails; | |
530d4062 NR |
36 | this.showTraces = showTraces; |
37 | } | |
38 | ||
39 | /** | |
40 | * An exception happened, log it. | |
41 | * | |
42 | * @param e | |
43 | * the exception | |
44 | */ | |
45 | public void error(Exception e) { | |
08a58812 NR |
46 | if (showErrors) { |
47 | if (showErrorDetails) { | |
48 | e.printStackTrace(); | |
49 | } else { | |
50 | error(e.getMessage()); | |
51 | } | |
530d4062 NR |
52 | } |
53 | } | |
54 | ||
55 | /** | |
56 | * An error happened, log it. | |
57 | * | |
58 | * @param message | |
59 | * the error message | |
60 | */ | |
61 | public void error(String message) { | |
08a58812 NR |
62 | if (showErrors) { |
63 | System.err.println(message); | |
64 | } | |
530d4062 NR |
65 | } |
66 | ||
67 | /** | |
68 | * A trace happened, show it. | |
69 | * <p> | |
f157aed8 | 70 | * Will only be effective if {@link TraceHandler#showTraces} is true. |
530d4062 NR |
71 | * |
72 | * @param message | |
73 | * the trace message | |
74 | */ | |
75 | public void trace(String message) { | |
08a58812 | 76 | if (showTraces) { |
530d4062 NR |
77 | System.out.println(message); |
78 | } | |
79 | } | |
f157aed8 NR |
80 | |
81 | // old stuff: | |
82 | ||
83 | /** | |
84 | * Use the parameters in the constructor instead. | |
85 | * | |
86 | * @param showTraces | |
87 | * show the traces | |
88 | */ | |
89 | @Deprecated | |
90 | public void setShowTraces(boolean showTraces) { | |
91 | this.showTraces = showTraces; | |
92 | } | |
93 | ||
94 | /** | |
95 | * Use the parameters in the constructor instead. | |
96 | * | |
97 | * @param showErrorDetails | |
98 | * show the details on errors | |
99 | */ | |
100 | @Deprecated | |
101 | public void setShowErrorDetails(boolean showErrorDetails) { | |
102 | this.showErrorDetails = showErrorDetails; | |
103 | } | |
530d4062 | 104 | } |