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; | |
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 |
8537d55a | 30 | * show level 1 traces on stdout, or no traces at all |
530d4062 | 31 | */ |
08a58812 NR |
32 | public TraceHandler(boolean showErrors, boolean showErrorDetails, |
33 | boolean showTraces) { | |
8537d55a NR |
34 | this(showErrors, showErrorDetails, showTraces ? 1 : 0); |
35 | } | |
36 | ||
37 | /** | |
38 | * Create a default {@link TraceHandler}. | |
39 | * | |
40 | * @param showErrors | |
41 | * show errors on stderr | |
42 | * @param showErrorDetails | |
43 | * show more details when printing errors | |
44 | * @param traceLevel | |
45 | * show traces of this level or lower (0 means "no traces", | |
46 | * higher means more traces) | |
47 | */ | |
48 | public TraceHandler(boolean showErrors, boolean showErrorDetails, | |
49 | int traceLevel) { | |
08a58812 NR |
50 | this.showErrors = showErrors; |
51 | this.showErrorDetails = showErrorDetails; | |
8537d55a NR |
52 | this.traceLevel = Math.max(traceLevel, 0); |
53 | } | |
54 | ||
55 | /** | |
56 | * The trace level of this {@link TraceHandler}. | |
57 | * | |
58 | * @return the level | |
59 | */ | |
60 | public int getTraceLevel() { | |
61 | return traceLevel; | |
530d4062 NR |
62 | } |
63 | ||
64 | /** | |
65 | * An exception happened, log it. | |
66 | * | |
67 | * @param e | |
68 | * the exception | |
69 | */ | |
70 | public void error(Exception e) { | |
08a58812 NR |
71 | if (showErrors) { |
72 | if (showErrorDetails) { | |
73 | e.printStackTrace(); | |
74 | } else { | |
75 | error(e.getMessage()); | |
76 | } | |
530d4062 NR |
77 | } |
78 | } | |
79 | ||
80 | /** | |
81 | * An error happened, log it. | |
82 | * | |
83 | * @param message | |
84 | * the error message | |
85 | */ | |
86 | public void error(String message) { | |
08a58812 NR |
87 | if (showErrors) { |
88 | System.err.println(message); | |
89 | } | |
530d4062 NR |
90 | } |
91 | ||
92 | /** | |
93 | * A trace happened, show it. | |
94 | * <p> | |
8537d55a NR |
95 | * By default, will only be effective if {@link TraceHandler#showTraces} is |
96 | * true | |
97 | * <p> | |
98 | * A call to this method is equivalent to a call to | |
99 | * {@link TraceHandler#trace(String, int)} with a level of 1. | |
530d4062 NR |
100 | * |
101 | * @param message | |
102 | * the trace message | |
103 | */ | |
104 | public void trace(String message) { | |
8537d55a NR |
105 | trace(message, 1); |
106 | } | |
107 | ||
108 | /** | |
109 | * A trace happened, show it. | |
110 | * <p> | |
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}. | |
113 | * | |
114 | * @param message | |
115 | * the trace message | |
116 | * @param level | |
117 | * the trace level | |
118 | */ | |
119 | public void trace(String message, int level) { | |
120 | if (traceLevel > 0 && level <= traceLevel) { | |
530d4062 NR |
121 | System.out.println(message); |
122 | } | |
123 | } | |
124 | } |