X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FTraceHandler.java;h=be9beeffd780af92c80f7ea16d389723b863419b;hb=refs%2Ftags%2Fnikiroo-utils-3.1.2;hp=5fa08438c3abe2e1a01e916d37f8d83b9c84ae8f;hpb=530d4062471346d6ececf76d74a0358c91323998;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/TraceHandler.java b/src/be/nikiroo/utils/TraceHandler.java index 5fa0843..be9beef 100644 --- a/src/be/nikiroo/utils/TraceHandler.java +++ b/src/be/nikiroo/utils/TraceHandler.java @@ -7,45 +7,79 @@ package be.nikiroo.utils; * @author niki */ public class TraceHandler { - private boolean showErrorDetails; - private boolean showTraces; + private final boolean showErrors; + private final boolean showErrorDetails; + private final int traceLevel; + private final int maxPrintSize; /** - * Show more details (usually equivalent to the value of DEBUG). + * Create a default {@link TraceHandler} that will print errors on stderr + * (without details) and no traces. + */ + public TraceHandler() { + this(true, false, false); + } + + /** + * Create a default {@link TraceHandler}. * - * @return TRUE or FALSE + * @param showErrors + * show errors on stderr + * @param showErrorDetails + * show more details when printing errors + * @param showTraces + * show level 1 traces on stdout, or no traces at all */ - public boolean isShowErrorDetails() { - return showErrorDetails; + public TraceHandler(boolean showErrors, boolean showErrorDetails, + boolean showTraces) { + this(showErrors, showErrorDetails, showTraces ? 1 : 0); } /** - * Show more details (usually equivalent to the value of DEBUG). + * Create a default {@link TraceHandler}. * + * @param showErrors + * show errors on stderr * @param showErrorDetails - * TRUE or FALSE + * show more details when printing errors + * @param traceLevel + * show traces of this level or lower (0 means "no traces", + * higher means more traces) */ - public void setShowErrorDetails(boolean showErrorDetails) { - this.showErrorDetails = showErrorDetails; + public TraceHandler(boolean showErrors, boolean showErrorDetails, + int traceLevel) { + this(showErrors, showErrorDetails, traceLevel, -1); } /** - * Show DEBUG traces. + * Create a default {@link TraceHandler}. * - * @return TRUE or FALSE + * @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) + * @param maxPrintSize + * the maximum size at which to truncate traces data (or -1 for + * "no limit") */ - public boolean isShowTraces() { - return showTraces; + public TraceHandler(boolean showErrors, boolean showErrorDetails, + int traceLevel, int maxPrintSize) { + this.showErrors = showErrors; + this.showErrorDetails = showErrorDetails; + this.traceLevel = Math.max(traceLevel, 0); + this.maxPrintSize = maxPrintSize; } /** - * Show DEBUG traces. + * The trace level of this {@link TraceHandler}. * - * @param showTraces - * TRUE or FALSE + * @return the level */ - public void setShowTraces(boolean showTraces) { - this.showTraces = showTraces; + public int getTraceLevel() { + return traceLevel; } /** @@ -55,10 +89,12 @@ public class TraceHandler { * the exception */ public void error(Exception e) { - if (isShowErrorDetails()) { - e.printStackTrace(); - } else { - error(e.getMessage()); + if (showErrors) { + if (showErrorDetails) { + e.printStackTrace(); + } else { + error(e.getMessage()); + } } } @@ -69,20 +105,46 @@ public class TraceHandler { * the error message */ public void error(String message) { - System.err.println(message); + if (showErrors) { + System.err.println(message); + } } /** * A trace happened, show it. *
- * Will only be effective if {@link TraceHandler#isShowTraces()} is true. + * By default, will only be effective if {@link TraceHandler#traceLevel} is + * not 0. + *
+ * 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 (isShowTraces()) { - System.out.println(message); + trace(message, 1); + } + + /** + * A trace happened, show it. + *
+ * By default, will only be effective if {@link TraceHandler#traceLevel} is + * not 0 and the level is lower or equal to it. + * + * @param message + * the trace message + * @param level + * the trace level + */ + public void trace(String message, int level) { + if (traceLevel > 0 && level <= traceLevel) { + if (maxPrintSize > 0 && message.length() > maxPrintSize) { + System.out + .println(message.substring(0, maxPrintSize) + "[...]"); + } else { + System.out.println(message); + } } } }