X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2FTraceHandler.java;h=0a09712d5218013992493838c75ad9aa0ad92527;hb=9991e4e10cd1cb607210ecfda9ed48232574be21;hp=309e8af6d393f31823a4200e719ec051e1570ad3;hpb=f157aed840bdd5b8ef04902d2326d916f71139da;p=fanfix.git diff --git a/src/be/nikiroo/utils/TraceHandler.java b/src/be/nikiroo/utils/TraceHandler.java index 309e8af..0a09712 100644 --- a/src/be/nikiroo/utils/TraceHandler.java +++ b/src/be/nikiroo/utils/TraceHandler.java @@ -7,9 +7,10 @@ package be.nikiroo.utils; * @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; + private final int maxPrintSize; /** * Create a default {@link TraceHandler} that will print errors on stderr @@ -27,13 +28,58 @@ public class TraceHandler { * @param showErrorDetails * show more details when printing errors * @param showTraces - * show traces on stdout + * show level 1 traces on stderr, 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, showErrorDetails, traceLevel, -1); + } + + /** + * 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) + * @param maxPrintSize + * the maximum size at which to truncate traces data (or -1 for + * "no limit") + */ + public TraceHandler(boolean showErrors, boolean showErrorDetails, + int traceLevel, int maxPrintSize) { this.showErrors = showErrors; this.showErrorDetails = showErrorDetails; - this.showTraces = showTraces; + this.traceLevel = Math.max(traceLevel, 0); + this.maxPrintSize = maxPrintSize; + } + + /** + * The trace level of this {@link TraceHandler}. + * + * @return the level + */ + public int getTraceLevel() { + return traceLevel; } /** @@ -45,9 +91,11 @@ public class TraceHandler { public void error(Exception e) { if (showErrors) { if (showErrorDetails) { + long now = System.currentTimeMillis(); + System.err.print(StringUtils.fromTime(now) + ": "); e.printStackTrace(); } else { - error(e.getMessage()); + error(e.toString()); } } } @@ -60,45 +108,49 @@ public class TraceHandler { */ public void error(String message) { if (showErrors) { - System.err.println(message); + long now = System.currentTimeMillis(); + System.err.println(StringUtils.fromTime(now) + ": " + message); } } /** * A trace happened, show it. *

- * Will only be effective if {@link TraceHandler#showTraces} 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 (showTraces) { - System.out.println(message); - } + trace(message, 1); } - // old stuff: - /** - * Use the parameters in the constructor instead. + * 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 showTraces - * show the traces + * @param message + * the trace message + * @param level + * the trace level */ - @Deprecated - public void setShowTraces(boolean showTraces) { - this.showTraces = showTraces; - } + public void trace(String message, int level) { + if (traceLevel > 0 && level <= traceLevel) { + long now = System.currentTimeMillis(); + System.err.print(StringUtils.fromTime(now) + ": "); + if (maxPrintSize > 0 && message.length() > maxPrintSize) { - /** - * Use the parameters in the constructor instead. - * - * @param showErrorDetails - * show the details on errors - */ - @Deprecated - public void setShowErrorDetails(boolean showErrorDetails) { - this.showErrorDetails = showErrorDetails; + System.err + .println(message.substring(0, maxPrintSize) + "[...]"); + } else { + System.err.println(message); + } + } } }