| 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 boolean showErrorDetails; |
| 11 | private boolean showTraces; |
| 12 | |
| 13 | /** |
| 14 | * Show more details (usually equivalent to the value of DEBUG). |
| 15 | * |
| 16 | * @return TRUE or FALSE |
| 17 | */ |
| 18 | public boolean isShowErrorDetails() { |
| 19 | return showErrorDetails; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Show more details (usually equivalent to the value of DEBUG). |
| 24 | * |
| 25 | * @param showErrorDetails |
| 26 | * TRUE or FALSE |
| 27 | */ |
| 28 | public void setShowErrorDetails(boolean showErrorDetails) { |
| 29 | this.showErrorDetails = showErrorDetails; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Show DEBUG traces. |
| 34 | * |
| 35 | * @return TRUE or FALSE |
| 36 | */ |
| 37 | public boolean isShowTraces() { |
| 38 | return showTraces; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Show DEBUG traces. |
| 43 | * |
| 44 | * @param showTraces |
| 45 | * TRUE or FALSE |
| 46 | */ |
| 47 | public void setShowTraces(boolean showTraces) { |
| 48 | this.showTraces = showTraces; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * An exception happened, log it. |
| 53 | * |
| 54 | * @param e |
| 55 | * the exception |
| 56 | */ |
| 57 | public void error(Exception e) { |
| 58 | if (isShowErrorDetails()) { |
| 59 | e.printStackTrace(); |
| 60 | } else { |
| 61 | error(e.getMessage()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * An error happened, log it. |
| 67 | * |
| 68 | * @param message |
| 69 | * the error message |
| 70 | */ |
| 71 | public void error(String message) { |
| 72 | System.err.println(message); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * A trace happened, show it. |
| 77 | * <p> |
| 78 | * Will only be effective if {@link TraceHandler#isShowTraces()} is true. |
| 79 | * |
| 80 | * @param message |
| 81 | * the trace message |
| 82 | */ |
| 83 | public void trace(String message) { |
| 84 | if (isShowTraces()) { |
| 85 | System.out.println(message); |
| 86 | } |
| 87 | } |
| 88 | } |