Merge branch 'subtree'
[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 stderr, 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 long now = System.currentTimeMillis();
95 System.err.print(StringUtils.fromTime(now) + ": ");
96 e.printStackTrace();
97 } else {
98 error(e.toString());
99 }
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) {
110 if (showErrors) {
111 long now = System.currentTimeMillis();
112 System.err.println(StringUtils.fromTime(now) + ": " + message);
113 }
114 }
115
116 /**
117 * A trace happened, show it.
118 * <p>
119 * By default, will only be effective if {@link TraceHandler#traceLevel} is
120 * not 0.
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.
124 *
125 * @param message
126 * the trace message
127 */
128 public void trace(String message) {
129 trace(message, 1);
130 }
131
132 /**
133 * A trace happened, show it.
134 * <p>
135 * By default, will only be effective if {@link TraceHandler#traceLevel} is
136 * not 0 and the level is lower or equal to it.
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) {
145 long now = System.currentTimeMillis();
146 System.err.print(StringUtils.fromTime(now) + ": ");
147 if (maxPrintSize > 0 && message.length() > maxPrintSize) {
148
149 System.err
150 .println(message.substring(0, maxPrintSize) + "[...]");
151 } else {
152 System.err.println(message);
153 }
154 }
155 }
156 }