Version 4.4.0 (see changelog)
[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 private final int maxPrintSize;
14
15 /**
16 * Create a default {@link TraceHandler} that will print errors on stderr
17 * (without details) and no traces.
18 */
19 public TraceHandler() {
20 this(true, false, false);
21 }
22
23 /**
24 * Create a default {@link TraceHandler}.
25 *
26 * @param showErrors
27 * show errors on stderr
28 * @param showErrorDetails
29 * show more details when printing errors
30 * @param showTraces
31 * show level 1 traces on stdout, or no traces at all
32 */
33 public TraceHandler(boolean showErrors, boolean showErrorDetails,
34 boolean showTraces) {
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) {
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) {
70 this.showErrors = showErrors;
71 this.showErrorDetails = showErrorDetails;
72 this.traceLevel = Math.max(traceLevel, 0);
73 this.maxPrintSize = maxPrintSize;
74 }
75
76 /**
77 * The trace level of this {@link TraceHandler}.
78 *
79 * @return the level
80 */
81 public int getTraceLevel() {
82 return traceLevel;
83 }
84
85 /**
86 * An exception happened, log it.
87 *
88 * @param e
89 * the exception
90 */
91 public void error(Exception e) {
92 if (showErrors) {
93 if (showErrorDetails) {
94 e.printStackTrace();
95 } else {
96 error(e.getMessage());
97 }
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) {
108 if (showErrors) {
109 System.err.println(message);
110 }
111 }
112
113 /**
114 * A trace happened, show it.
115 * <p>
116 * By default, will only be effective if {@link TraceHandler#traceLevel} is
117 * not 0.
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.
121 *
122 * @param message
123 * the trace message
124 */
125 public void trace(String message) {
126 trace(message, 1);
127 }
128
129 /**
130 * A trace happened, show it.
131 * <p>
132 * By default, will only be effective if {@link TraceHandler#traceLevel} is
133 * not 0 and the level is lower or equal to it.
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) {
142 if (maxPrintSize > 0 && message.length() > maxPrintSize) {
143 System.out
144 .println(message.substring(0, maxPrintSize) + "[...]");
145 } else {
146 System.out.println(message);
147 }
148 }
149 }
150 }