Version 3.1.0: ServerBridge
[nikiroo-utils.git] / src / be / nikiroo / utils / TraceHandler.java
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 {
10 private final boolean showErrors;
11 private final boolean showErrorDetails;
12 private final int traceLevel;
13
14 /**
15 * Create a default {@link TraceHandler} that will print errors on stderr
16 * (without details) and no traces.
17 */
18 public TraceHandler() {
19 this(true, false, false);
20 }
21
22 /**
23 * Create a default {@link TraceHandler}.
24 *
25 * @param showErrors
26 * show errors on stderr
27 * @param showErrorDetails
28 * show more details when printing errors
29 * @param showTraces
30 * show level 1 traces on stdout, or no traces at all
31 */
32 public TraceHandler(boolean showErrors, boolean showErrorDetails,
33 boolean showTraces) {
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) {
50 this.showErrors = showErrors;
51 this.showErrorDetails = showErrorDetails;
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;
62 }
63
64 /**
65 * An exception happened, log it.
66 *
67 * @param e
68 * the exception
69 */
70 public void error(Exception e) {
71 if (showErrors) {
72 if (showErrorDetails) {
73 e.printStackTrace();
74 } else {
75 error(e.getMessage());
76 }
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) {
87 if (showErrors) {
88 System.err.println(message);
89 }
90 }
91
92 /**
93 * A trace happened, show it.
94 * <p>
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.
100 *
101 * @param message
102 * the trace message
103 */
104 public void trace(String message) {
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) {
121 System.out.println(message);
122 }
123 }
124 }