1 package nmp.util; 2 3 import java.util.*; 4 import java.io.*; 5 6 /*** 7 * Class for logging. 8 * There is a defaultLog, but application may have their own instance of the log. 9 * It is supposed to be used in all projects. It is expected to evolve over time. 10 */ 11 public class Log 12 { 13 static Log defaultLog; 14 15 static { 16 defaultLog = new Log(); 17 } 18 19 Vector v; 20 21 public Log() { 22 v = new Vector(); 23 v.addElement( new PrintWriter( System.err)); 24 } 25 26 public void cleanSinkP() { 27 v.removeAllElements(); 28 } 29 30 public PrintWriter getWriterP() { 31 return new PrintWriter( new Writer() { 32 public void close() { 33 for( int i = 0; i < v.size(); i++) { 34 PrintWriter pw = (PrintWriter)v.elementAt( i); 35 pw.close(); 36 } 37 } 38 public void flush() { 39 for( int i = 0; i < v.size(); i++) { 40 PrintWriter pw = (PrintWriter)v.elementAt( i); 41 pw.flush(); 42 } 43 } 44 public void write(char[] cbuf, int off, int len) { 45 for( int i = 0; i < v.size(); i++) { 46 PrintWriter pw = (PrintWriter)v.elementAt( i); 47 pw.write( cbuf, off, len ); 48 pw.flush(); 49 } 50 } 51 }); 52 } 53 54 public void addSinkP( PrintWriter writer) { 55 v.addElement( writer); 56 } 57 58 public void addSinkP( String filename, boolean append) throws IOException { 59 addSinkP( new PrintWriter( new FileWriter( filename, append))); 60 } 61 62 public void addSinkP( String filename) throws IOException { 63 addSinkP( filename, false); 64 } 65 66 /*** 67 * Dums the error message. To be used only internally. 68 */ 69 private void messageP( String msg) { 70 for( int i = 0; i < v.size(); i++) { 71 PrintWriter pw = (PrintWriter)v.elementAt( i); 72 pw.println( msg); 73 pw.flush(); 74 } 75 } 76 77 private void messageP( Throwable th) { 78 for( int i = 0; i < v.size(); i++) { 79 PrintWriter pw = (PrintWriter)v.elementAt( i); 80 th.printStackTrace( pw); 81 pw.flush(); 82 } 83 } 84 85 /*** 86 * Dumps a log message. 87 */ 88 public void logP( String msg) { 89 messageP( msg); 90 } 91 /*** 92 * Dumps a log message. 93 */ 94 public void logTraceP( String msg) { 95 messageP( msg); 96 messageP( new Throwable()); 97 } 98 99 100 /*** 101 * Dumps an wanring message. 102 */ 103 public void warningP( String msg) { 104 messageP( "WARN:" + msg); 105 } 106 107 /*** 108 * Dumps an error message. Throws the given exception (if given). 109 */ 110 public void errorP( String msg) { 111 messageP( "ERROR:" + msg); 112 } 113 114 public void errorP( Throwable th) { 115 messageP( "ERROR:" + th.getMessage()); 116 } 117 118 public void errorP( String msg, Throwable th) { 119 errorP( msg); 120 errorP( th); 121 } 122 123 public void errorDumpP( Throwable th) { 124 errorP( th); 125 messageP( th); 126 } 127 128 public void errorDumpP( String msg, Throwable th) { 129 errorP( msg); 130 messageP( th); 131 } 132 133 public void errorThP( Throwable th) throws Throwable { 134 messageP( "ERROR:" + th.getMessage()); 135 throw th; 136 } 137 138 /*** 139 * Dumps an internal error message. Throws the given exception (if given). 140 */ 141 public void internalErrorP( String msg) { 142 messageP( "INTERNAL ERROR:" + msg); 143 } 144 145 public void internalErrorP( Throwable th) { 146 messageP( "INTERNAL ERROR:" + th.getMessage()); 147 } 148 149 public void internalErrorDumpP( Throwable th) { 150 internalErrorP( th); 151 messageP( th); 152 } 153 154 public void internalErrorThP( Throwable th) throws Throwable { 155 messageP( "INTERNAL ERROR:" + th.getMessage()); 156 throw th; 157 } 158 159 /*** 160 * Dumps a fatal error message. Exits virtual machine. 161 */ 162 public void fatalErrorP( String msg) { 163 messageP( "FATAL ERROR:" + msg); 164 System.exit( 0); 165 } 166 167 public void fatalErrorP( Throwable th) { 168 messageP( th); 169 System.exit( 0); 170 } 171 172 public static void cleanSink() { 173 defaultLog.cleanSinkP(); 174 } 175 176 public static PrintWriter getWriter() { 177 return defaultLog.getWriterP(); 178 } 179 180 public static void addSink( PrintWriter writer) { 181 defaultLog.addSinkP( writer); 182 } 183 184 public static void addSink( String filename, boolean append) throws IOException { 185 defaultLog.addSinkP( filename, append); 186 } 187 188 public static void addSink( String filename) throws IOException { 189 defaultLog.addSinkP( filename); 190 } 191 192 /*** 193 * Dumps a log message. 194 */ 195 public static void log( String msg) { 196 defaultLog.logP( msg); 197 } 198 public static void logTrace( String msg) { 199 defaultLog.logTraceP( msg); 200 } 201 202 203 /*** 204 * Dumps an wanring message. 205 */ 206 public static void warning( String msg) { 207 defaultLog.warningP( msg); 208 } 209 210 /*** 211 * Dumps an error message. Throws the given exception (if given). 212 */ 213 public static void error( String msg) { 214 defaultLog.errorP( msg); 215 } 216 217 public static void error( String msg, Throwable th) { 218 defaultLog.errorP( msg, th); 219 } 220 221 public static void error( Throwable th) { 222 defaultLog.errorP( th); 223 } 224 225 public static void errorDump( String msg, Throwable th) { 226 defaultLog.errorDumpP( msg, th); 227 } 228 229 public static void errorDump( Throwable th) { 230 defaultLog.errorDumpP( th); 231 } 232 233 public static void errorTh( Throwable th) throws Throwable { 234 defaultLog.errorThP( th); 235 } 236 237 /*** 238 * Dumps an internal error message. Throws the given exception (if given). 239 */ 240 public static void internalError( String msg) { 241 defaultLog.internalErrorP( msg); 242 } 243 244 public static void internalError( Throwable th) { 245 defaultLog.internalErrorP( th); 246 } 247 248 public static void internalErrorDump( Throwable th) { 249 defaultLog.internalErrorDumpP( th); 250 } 251 252 public static void internalErrorTh( Throwable th) throws Throwable { 253 defaultLog.internalErrorThP( th); 254 } 255 256 /*** 257 * Dumps a fatal error message. Exits virtual machine. 258 */ 259 public static void fatalError( String msg) { 260 defaultLog.fatalErrorP( msg); 261 } 262 263 public static void fatalError( Throwable th) { 264 defaultLog.fatalErrorP( th); 265 } 266 }

This page was automatically generated by Maven