1 6 7 package SOFA.SOFAnet.Core; 8 9 import java.io.*; 10 11 15 public class Reporter 16 { 17 private static PrintStream errorStream = System.err; 18 private static PrintStream warningStream = System.err; 19 private static PrintStream infoStream = System.out; 20 private static boolean showErrors = true; 21 private static boolean showWarnings = true; 22 private static boolean showInfos = true; 23 private static boolean showDetailedExceptions = false; 24 25 26 private Reporter() 27 { 28 } 29 30 public static void setErrorStream(PrintStream printStream) 31 { 32 errorStream = printStream; 33 } 34 35 public static void setErrorStream(OutputStream outputStream) 36 { 37 errorStream = new PrintStream(outputStream); 38 } 39 40 public static void setWarningStream(PrintStream printStream) 41 { 42 warningStream = printStream; 43 } 44 45 public static void setWarningStream(OutputStream outputStream) 46 { 47 warningStream = new PrintStream(outputStream); 48 } 49 50 public static void setInfoStream(PrintStream printStream) 51 { 52 infoStream = printStream; 53 } 54 55 public static void setInfoStream(OutputStream outputStream) 56 { 57 infoStream = new PrintStream(outputStream); 58 } 59 60 public static void error(String message) 61 { 62 printErrorWarning(true, message, null); 63 } 64 65 public static void error(String message, Throwable cause) 66 { 67 printErrorWarning(true, message, cause); 68 } 69 70 public static void error(Throwable cause) 71 { 72 printErrorWarning(true, null, cause); 73 } 74 75 public static void warning(String message) 76 { 77 printErrorWarning(false, message, null); 78 } 79 80 public static void warning(String message, Throwable cause) 81 { 82 printErrorWarning(false, message, cause); 83 } 84 85 public static void warning(Throwable cause) 86 { 87 printErrorWarning(false, null, cause); 88 } 89 90 public static void info(String message) 91 { 92 printInfo(message); 93 } 94 95 public static void startInfo(String message) 96 { 97 printInfo("+" + message); 98 } 99 100 public static void stopInfo(String message) 101 { 102 printInfo("-" + message); 103 } 104 105 public static void setShowErrors(boolean showErrors) 106 { 107 Reporter.showErrors = showErrors; 108 } 109 110 public static void setShowWarnings(boolean showWarnings) 111 { 112 Reporter.showWarnings = showWarnings; 113 } 114 115 public static void setShowInfos(boolean showInfos) 116 { 117 Reporter.showInfos = showInfos; 118 } 119 120 public static void setShowDetailedExceptions(boolean showDetailedExceptions) 121 { 122 Reporter.showDetailedExceptions = showDetailedExceptions; 123 } 124 125 private static void printErrorWarning(boolean error, String message, Throwable cause) 126 { 127 String header; 128 PrintStream stream; 129 if (error) 130 { 131 if (!showErrors) return; 132 stream = errorStream; 133 header = "Error"; 134 } 135 else 136 { 137 if (!showWarnings) return; 138 stream = warningStream; 139 header = "Warning"; 140 } 141 142 if (message != null) 143 { 144 stream.println(header + ": " + message); 145 146 if (cause != null) 147 { 148 stream.println(" cause(exc): " + cause.toString()); 149 if (showDetailedExceptions) 150 { 151 stream.println("++++++++"); 152 cause.printStackTrace(errorStream); 153 stream.println("--------"); 154 } 155 } 156 } 157 else 158 { 159 if (cause != null) 160 { 161 stream.println(header + "(exc.): " + cause.toString()); 162 if (showDetailedExceptions) 163 { 164 stream.println("++++++++"); 165 cause.printStackTrace(errorStream); 166 stream.println("--------"); 167 } 168 } 169 } 170 } 171 172 private static void printInfo(String message) 173 { 174 if (!showInfos) return; 175 176 if (message != null) infoStream.println(message); 177 } 178 } 179 | Popular Tags |