* @author niki
*/
public class TraceHandler {
- private boolean showErrors;
- private boolean showTraces;
- private boolean showErrorDetails;
+ private final boolean showErrors;
+ private final boolean showErrorDetails;
+ private final int traceLevel;
/**
* Create a default {@link TraceHandler} that will print errors on stderr
* @param showErrorDetails
* show more details when printing errors
* @param showTraces
- * show traces on stdout
+ * show level 1 traces on stdout, or no traces at all
*/
public TraceHandler(boolean showErrors, boolean showErrorDetails,
boolean showTraces) {
+ this(showErrors, showErrorDetails, showTraces ? 1 : 0);
+ }
+
+ /**
+ * Create a default {@link TraceHandler}.
+ *
+ * @param showErrors
+ * show errors on stderr
+ * @param showErrorDetails
+ * show more details when printing errors
+ * @param traceLevel
+ * show traces of this level or lower (0 means "no traces",
+ * higher means more traces)
+ */
+ public TraceHandler(boolean showErrors, boolean showErrorDetails,
+ int traceLevel) {
this.showErrors = showErrors;
this.showErrorDetails = showErrorDetails;
- this.showTraces = showTraces;
+ this.traceLevel = Math.max(traceLevel, 0);
+ }
+
+ /**
+ * The trace level of this {@link TraceHandler}.
+ *
+ * @return the level
+ */
+ public int getTraceLevel() {
+ return traceLevel;
}
/**
/**
* A trace happened, show it.
* <p>
- * Will only be effective if {@link TraceHandler#showTraces} is true.
+ * By default, will only be effective if {@link TraceHandler#showTraces} is
+ * true
+ * <p>
+ * A call to this method is equivalent to a call to
+ * {@link TraceHandler#trace(String, int)} with a level of 1.
*
* @param message
* the trace message
*/
public void trace(String message) {
- if (showTraces) {
- System.out.println(message);
- }
- }
-
- // old stuff:
-
- /**
- * Use the parameters in the constructor instead.
- *
- * @param showTraces
- * show the traces
- */
- @Deprecated
- public void setShowTraces(boolean showTraces) {
- this.showTraces = showTraces;
+ trace(message, 1);
}
/**
- * Use the parameters in the constructor instead.
+ * A trace happened, show it.
+ * <p>
+ * By default, will only be effective if {@link TraceHandler#showTraces} is
+ * true and the level is lower or equal to {@link TraceHandler#traceLevel}.
*
- * @param showErrorDetails
- * show the details on errors
+ * @param message
+ * the trace message
+ * @param level
+ * the trace level
*/
- @Deprecated
- public void setShowErrorDetails(boolean showErrorDetails) {
- this.showErrorDetails = showErrorDetails;
+ public void trace(String message, int level) {
+ if (traceLevel > 0 && level <= traceLevel) {
+ System.out.println(message);
+ }
}
}