[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.util; |
---|
| 9 | import java.io.*; |
---|
| 10 | import java.util.Vector; |
---|
| 11 | import java.util.Enumeration; |
---|
| 12 | |
---|
| 13 | /* |
---|
| 14 | * Output.java |
---|
| 15 | * Created: Sat Aug 7 15:30:54 1999 |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * <p>Outputs and logs system messages, errors, and other various |
---|
| 21 | * items printed as a result of a run. |
---|
| 22 | * |
---|
| 23 | * <p> Output maintains zero or more logs, which contain Writers which |
---|
| 24 | * write out stuff. Each log has an associated verbosity; if request |
---|
| 25 | * is made to write text to a log, and the text's maximal verbosity |
---|
| 26 | * is lower than the verbosity of the log, the log will not write it. |
---|
| 27 | * Each Output instance also has an instance-level global verbosity; |
---|
| 28 | * incoming requests to write text are additionally subject to this |
---|
| 29 | * verbosity test. Lastly, the Output class itself has a global |
---|
| 30 | * verbosity as well. This last verbosity is useful for shutting |
---|
| 31 | * down writing to all logs in the entire system in a simple way. |
---|
| 32 | * |
---|
| 33 | * <p>When the system fails for some reason and must be started back |
---|
| 34 | * up from a checkpoint, Output's log files may be overwritten. Output |
---|
| 35 | * offers three approaches here. First, Output can clear the log file |
---|
| 36 | * and overwrite it. Second, Output can append to the existing log file; |
---|
| 37 | * because checkpoints are only done occasionally, this may result in |
---|
| 38 | * duplicate outputs to a file, so keep this in mind. Third, Output can |
---|
| 39 | * keep certain written text, typically <i>announcements</i>, in memory; |
---|
| 40 | * this text gets written out into the checkpoint file, and so it is sound. |
---|
| 41 | * |
---|
| 42 | * <p>There are several kinds of announcements, in different levels |
---|
| 43 | * of importance. |
---|
| 44 | * |
---|
| 45 | * <ol> |
---|
| 46 | * <li> FATAL ERRORs. These errors cause the system to exit(1) immediately. |
---|
| 47 | * <li> Simple ERRORs. These errors set the "errors" flag to true; at |
---|
| 48 | * the end of a stream of simple errors, the system in general is expected |
---|
| 49 | * to exit with a fatal error due to the flag being set. That's the |
---|
| 50 | * protocol anyway. On restart from a checkpoint, if there were any |
---|
| 51 | * simple errors, the system ends with a fatal error automatically. |
---|
| 52 | * <li> WARNINGs. These errors do not cause the system to exit under any |
---|
| 53 | * circumstances. |
---|
| 54 | * <li> MESSAGEs. Useful facts printed out for the benefit of the user. |
---|
| 55 | * <li> SYSTEM MESSAGEs. Useful system-level facts printed out for the |
---|
| 56 | * benefit of the user. |
---|
| 57 | * </ol> |
---|
| 58 | * |
---|
| 59 | <!-- |
---|
| 60 | * <p>The default verbosity values for different kinds of announcements are |
---|
| 61 | * given below: |
---|
| 62 | * |
---|
| 63 | <table><tr><td>0</td><td>V_VERBOSE</td><td>(totally verbose)</td> |
---|
| 64 | </tr><tr><td>1000</td><td>V_NO_MESSAGES</td><td>(don't print messages or system messages)</td> |
---|
| 65 | </tr><tr><td>2000</td><td>V_NO_WARNINGS</td><td>(don't print warnings, messages, or system messages)</td> |
---|
| 66 | </tr><tr><td>3000</td><td>V_NO_GENERAL</td><td>(don't print warnings, messages, system messages, or other "general info" stuff that might come along (like statistics maybe))</td> |
---|
| 67 | </tr><tr><td>4000</td><td>V_NO_ERRORS</td><td>(don't even print errors)</td> |
---|
| 68 | </tr><tr><td>5000</td><td>V_TOTALLY_SILENT</td><td>(be totally silent)</td> |
---|
| 69 | </tr></table> |
---|
| 70 | * |
---|
| 71 | --> |
---|
| 72 | |
---|
| 73 | * <p>Output will also store all announcements in memory by default so as to reproduce |
---|
| 74 | * them if it's restarted from a checkpoint. You can change this behavior also by |
---|
| 75 | * |
---|
| 76 | * @author Sean Luke |
---|
| 77 | * @version 1.0 |
---|
| 78 | */ |
---|
| 79 | |
---|
| 80 | public class Output implements Serializable |
---|
| 81 | { |
---|
| 82 | boolean errors; |
---|
| 83 | Vector logs = new Vector(); |
---|
| 84 | Vector announcements = new Vector(); |
---|
| 85 | // boolean flush = true; |
---|
| 86 | boolean store = true; |
---|
| 87 | String filePrefix = ""; |
---|
| 88 | |
---|
| 89 | public static final int ALL_LOGS = -1; |
---|
| 90 | |
---|
| 91 | /** Total verbosity */ |
---|
| 92 | public static final int V_VERBOSE = 0; |
---|
| 93 | /** Don't print messages */ |
---|
| 94 | public static final int V_NO_MESSAGES = 1000; |
---|
| 95 | /** Don't print warnings or messages */ |
---|
| 96 | public static final int V_NO_WARNINGS = 2000; |
---|
| 97 | /** The standard verbosity to use if you don't want common reporting (like statistics) */ |
---|
| 98 | public static final int V_NO_GENERAL = 3000; |
---|
| 99 | /** Don't print warnings, messages, or simple errors */ |
---|
| 100 | public static final int V_NO_ERRORS = 4000; |
---|
| 101 | /** No verbosity at all, not even system messages or fatal errors*/ |
---|
| 102 | public static final int V_TOTALLY_SILENT = 5000; |
---|
| 103 | |
---|
| 104 | public void setFilePrefix(String filePrefix) { |
---|
| 105 | this.filePrefix = filePrefix; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | protected void finalize() throws Throwable |
---|
| 109 | { |
---|
| 110 | // flush the logs |
---|
| 111 | close(); |
---|
| 112 | |
---|
| 113 | // do super.finalize, just for good style |
---|
| 114 | super.finalize(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | private void exitWithError() |
---|
| 118 | { |
---|
| 119 | // flush logs first |
---|
| 120 | close(); |
---|
| 121 | |
---|
| 122 | // exit |
---|
| 123 | System.exit(1); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** Closes the logs -- ONLY call this if you are preparing to quit */ |
---|
| 127 | public synchronized void close() |
---|
| 128 | { |
---|
| 129 | // just in case |
---|
| 130 | flush(); |
---|
| 131 | |
---|
| 132 | Enumeration e = logs.elements(); |
---|
| 133 | while(e.hasMoreElements()) |
---|
| 134 | { |
---|
| 135 | Log log = (Log)e.nextElement(); |
---|
| 136 | if (!log.isLoggingToSystemOut) |
---|
| 137 | log.writer.close(); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** Flushes the logs */ |
---|
| 142 | public synchronized void flush() |
---|
| 143 | { |
---|
| 144 | Enumeration e = logs.elements(); |
---|
| 145 | while(e.hasMoreElements()) |
---|
| 146 | { |
---|
| 147 | Log log = (Log)e.nextElement(); |
---|
| 148 | log.writer.flush(); |
---|
| 149 | } |
---|
| 150 | // just in case... |
---|
| 151 | System.out.flush(); |
---|
| 152 | System.err.flush(); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** Creates a new, verbose, empty Output object. |
---|
| 156 | @deprecated Verbosity no longer has an effect. |
---|
| 157 | */ |
---|
| 158 | public Output(boolean storeAnnouncementsInMemory, int _verbosity) |
---|
| 159 | { |
---|
| 160 | this(storeAnnouncementsInMemory); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** Creates a new, verbose, empty Output object. */ |
---|
| 164 | public Output(boolean storeAnnouncementsInMemory) |
---|
| 165 | { |
---|
| 166 | errors = false; |
---|
| 167 | store = storeAnnouncementsInMemory; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** Sets whether the Output flushes its announcements. |
---|
| 171 | @deprecated We now always flush |
---|
| 172 | */ |
---|
| 173 | public synchronized void setFlush(boolean v) |
---|
| 174 | { |
---|
| 175 | // flush = v; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* Returns the Output's flushing behavior. |
---|
| 179 | @deprecated We now always flush |
---|
| 180 | */ |
---|
| 181 | public synchronized boolean getFlush() |
---|
| 182 | { |
---|
| 183 | // return flush; |
---|
| 184 | return true; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** Sets whether the Output stores its announcements.*/ |
---|
| 188 | public synchronized void setStore(boolean v) |
---|
| 189 | { |
---|
| 190 | store = v; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /* Returns the Output's storing behavior. */ |
---|
| 194 | public synchronized boolean getStore() |
---|
| 195 | { |
---|
| 196 | return store; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** Sets the Output object's general verbosity to <i>v</i>. |
---|
| 200 | @deprecated Verbosity no longer has an effect. |
---|
| 201 | */ |
---|
| 202 | public synchronized void setVerbosity(int v) |
---|
| 203 | { |
---|
| 204 | // verbosity = v; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** Returns the Output object's general verbosity |
---|
| 208 | @deprecated Verbosity no longer has an effect. |
---|
| 209 | */ |
---|
| 210 | public synchronized int getVerbosity() |
---|
| 211 | { |
---|
| 212 | return V_VERBOSE; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** Creates a new log of minimal verbosity <i>verbosity</i> and adds it |
---|
| 216 | to Output. This log will write to the file <i>file</i>, and may |
---|
| 217 | or may not <i>post announcements</i> to the log. If the log must be |
---|
| 218 | reset upon restarting from a checkpoint, it will append to the file |
---|
| 219 | or erase the file and start over depending on <i>appendOnRestart</i>. |
---|
| 220 | If <i>appendOnRestart</i> is false and <i>postAnnouncements</i> is |
---|
| 221 | true, then this log will repost all the announcements on restarting |
---|
| 222 | from a checkpoint. Returns the position of the log in Output's |
---|
| 223 | collection of logs -- you should use this to access the log always; |
---|
| 224 | never store the log itself, which may go away upon a system restart. |
---|
| 225 | The log can be compressed with gzip, but you cannot appendOnRestart |
---|
| 226 | and compress at the same time. |
---|
| 227 | @deprecated Verbosity no longer has an effect. |
---|
| 228 | */ |
---|
| 229 | |
---|
| 230 | public synchronized int addLog(File file, |
---|
| 231 | int _verbosity, |
---|
| 232 | boolean postAnnouncements, |
---|
| 233 | boolean appendOnRestart, |
---|
| 234 | boolean gzip) throws IOException |
---|
| 235 | { |
---|
| 236 | if (filePrefix != null && filePrefix.length()>0) |
---|
| 237 | file = new File(file.getParent(),filePrefix+file.getName()); |
---|
| 238 | logs.addElement(new Log(file,postAnnouncements,appendOnRestart,gzip)); |
---|
| 239 | return logs.size()-1; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** Creates a new log of minimal verbosity <i>verbosity</i> and adds it |
---|
| 243 | to Output. This log will write to the file <i>file</i>, and may |
---|
| 244 | or may not <i>post announcements</i> to the log. If the log must be |
---|
| 245 | reset upon restarting from a checkpoint, it will append to the file |
---|
| 246 | or erase the file and start over depending on <i>appendOnRestart</i>. |
---|
| 247 | If <i>appendOnRestart</i> is false and <i>postAnnouncements</i> is |
---|
| 248 | true, then this log will repost all the announcements on restarting |
---|
| 249 | from a checkpoint. Returns the position of the log in Output's |
---|
| 250 | collection of logs -- you should use this to access the log always; |
---|
| 251 | never store the log itself, which may go away upon a system restart. |
---|
| 252 | @deprecated Verbosity no longer has an effect |
---|
| 253 | */ |
---|
| 254 | |
---|
| 255 | public synchronized int addLog(File file, |
---|
| 256 | int _verbosity, |
---|
| 257 | boolean postAnnouncements, |
---|
| 258 | boolean appendOnRestart) throws IOException |
---|
| 259 | { |
---|
| 260 | return addLog(file, postAnnouncements, appendOnRestart, false); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | /** Creates a new log <!-- of minimal verbosity V_NO_GENERAL-1 --> and adds it |
---|
| 265 | to Output. This log will write to the file <i>file</i>, and may |
---|
| 266 | or may not <i>post announcements</i> to the log. If the log must be |
---|
| 267 | reset upon restarting from a checkpoint, it will append to the file |
---|
| 268 | or erase the file and start over depending on <i>appendOnRestart</i>. |
---|
| 269 | If <i>appendOnRestart</i> is false and <i>postAnnouncements</i> is |
---|
| 270 | true, then this log will repost all the announcements on restarting |
---|
| 271 | from a checkpoint. Returns the position of the log in Output's |
---|
| 272 | collection of logs -- you should use this to access the log always; |
---|
| 273 | never store the log itself, which may go away upon a system restart. |
---|
| 274 | The log can be compressed with gzip, but you cannot appendOnRestart |
---|
| 275 | and compress at the same time. |
---|
| 276 | */ |
---|
| 277 | |
---|
| 278 | public synchronized int addLog(File file, |
---|
| 279 | boolean postAnnouncements, |
---|
| 280 | boolean appendOnRestart, |
---|
| 281 | boolean gzip) throws IOException |
---|
| 282 | { |
---|
| 283 | return addLog(file, V_VERBOSE, postAnnouncements, appendOnRestart, gzip); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | /** Creates a new log <!-- of minimal verbosity V_NO_GENERAL-1 --> and adds it |
---|
| 288 | to Output. This log will write to the file <i>file</i>, and you may |
---|
| 289 | not post announcements to the log. If the log must be |
---|
| 290 | reset upon restarting from a checkpoint, it will append to the file |
---|
| 291 | or erase the file and start over depending on <i>appendOnRestart</i>. |
---|
| 292 | Returns the position of the log in Output's |
---|
| 293 | collection of logs -- you should use this to access the log always; |
---|
| 294 | never store the log itself, which may go away upon a system restart. |
---|
| 295 | The log can be compressed with gzip, but you cannot appendOnRestart |
---|
| 296 | and compress at the same time.*/ |
---|
| 297 | |
---|
| 298 | public synchronized int addLog(File file, |
---|
| 299 | boolean appendOnRestart, |
---|
| 300 | boolean gzip) throws IOException |
---|
| 301 | { |
---|
| 302 | return addLog(file, false, appendOnRestart, gzip); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | /** Creates a new log <!-- of minimal verbosity V_NO_GENERAL-1 --> and adds it |
---|
| 306 | to Output. This log will write to the file <i>file</i>, and you may |
---|
| 307 | not post announcements to the log. If the log must be |
---|
| 308 | reset upon restarting from a checkpoint, it will append to the file |
---|
| 309 | or erase the file and start over depending on <i>appendOnRestart</i>. |
---|
| 310 | Returns the position of the log in Output's |
---|
| 311 | collection of logs -- you should use this to access the log always; |
---|
| 312 | never store the log itself, which may go away upon a system restart. */ |
---|
| 313 | |
---|
| 314 | public synchronized int addLog(File file, |
---|
| 315 | boolean appendOnRestart) throws IOException |
---|
| 316 | { |
---|
| 317 | return addLog(file, false, appendOnRestart, false); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | /** Creates a new log of minimal verbosity <i>verbosity</i> and adds it |
---|
| 322 | to Output. This log will write to stdout (descriptor == Log.D_STDOUT) |
---|
| 323 | or stderr (descriptor == Log.D_STDERR), and may or may not |
---|
| 324 | <i>post announcements</i> to the log. Returns the position of the |
---|
| 325 | log in Output's |
---|
| 326 | collection of logs -- you should use this to access the log always; |
---|
| 327 | never store the log itself, which may go away upon a system restart. |
---|
| 328 | @deprecated Verbosity no longer has an effect |
---|
| 329 | */ |
---|
| 330 | |
---|
| 331 | public synchronized int addLog(int descriptor, |
---|
| 332 | int _verbosity, |
---|
| 333 | boolean postAnnouncements) |
---|
| 334 | { |
---|
| 335 | logs.addElement(new Log(descriptor,postAnnouncements)); |
---|
| 336 | return logs.size()-1; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | /** Creates a new log and adds it |
---|
| 340 | to Output. This log will write to stdout (descriptor == Log.D_STDOUT) |
---|
| 341 | or stderr (descriptor == Log.D_STDERR), and may or may not |
---|
| 342 | <i>post announcements</i> to the log. Returns the position of the |
---|
| 343 | log in Output's |
---|
| 344 | collection of logs -- you should use this to access the log always; |
---|
| 345 | never store the log itself, which may go away upon a system restart. |
---|
| 346 | */ |
---|
| 347 | |
---|
| 348 | public synchronized int addLog(int descriptor, |
---|
| 349 | boolean postAnnouncements) |
---|
| 350 | { |
---|
| 351 | return addLog(descriptor, V_VERBOSE, postAnnouncements); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | /** Creates a new log of minimal verbosity <i>verbosity</i> and adds it |
---|
| 355 | to Output. This log may or may not <i>post announcements</i> to |
---|
| 356 | the log, and if it does, it additionally may or may not <i>repost</i> |
---|
| 357 | all of its announcements to the log upon a restart. The log |
---|
| 358 | writes to <i>writer</i>, which is reset upon system restart by |
---|
| 359 | <i>restarter</i>. Returns the position of the log in Output's |
---|
| 360 | collection of logs -- you should use this to access the log always; |
---|
| 361 | never store the log itself, which may go away upon a system restart. |
---|
| 362 | @deprecated Verbosity no longer has an effect |
---|
| 363 | */ |
---|
| 364 | |
---|
| 365 | public synchronized int addLog(Writer writer, |
---|
| 366 | LogRestarter restarter, |
---|
| 367 | int _verbosity, |
---|
| 368 | boolean postAnnouncements, |
---|
| 369 | boolean repostAnnouncements) |
---|
| 370 | { |
---|
| 371 | logs.addElement(new Log(writer,restarter,postAnnouncements,repostAnnouncements)); |
---|
| 372 | return logs.size()-1; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | /** Creates a new log <!-- of minimal verbosity V_NO_GENERAL-1 --> and adds it |
---|
| 376 | to Output. This log may or may not <i>post announcements</i> to |
---|
| 377 | the log, and if it does, it additionally may or may not <i>repost</i> |
---|
| 378 | all of its announcements to the log upon a restart. The log |
---|
| 379 | writes to <i>writer</i>, which is reset upon system restart by |
---|
| 380 | <i>restarter</i>. Returns the position of the log in Output's |
---|
| 381 | collection of logs -- you should use this to access the log always; |
---|
| 382 | never store the log itself, which may go away upon a system restart. |
---|
| 383 | */ |
---|
| 384 | |
---|
| 385 | public synchronized int addLog(Writer writer, |
---|
| 386 | LogRestarter restarter, |
---|
| 387 | boolean postAnnouncements, |
---|
| 388 | boolean repostAnnouncements) |
---|
| 389 | { |
---|
| 390 | logs.addElement(new Log(writer,restarter,postAnnouncements,repostAnnouncements)); |
---|
| 391 | return logs.size()-1; |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | /** Adds the given log to Output. In general you shouldn't use this |
---|
| 396 | method unless you really <i>really</i> need something custom. |
---|
| 397 | Returns the position of the log in Output's |
---|
| 398 | collection of logs -- you should use this to access the log always; |
---|
| 399 | never store the log itself, which may go away upon a system restart. */ |
---|
| 400 | |
---|
| 401 | public synchronized int addLog(Log l) |
---|
| 402 | { |
---|
| 403 | logs.addElement(l); |
---|
| 404 | return logs.size()-1; |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | /** Returns the number of logs currently posted. */ |
---|
| 408 | public synchronized int numLogs() |
---|
| 409 | { |
---|
| 410 | return logs.size(); |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | /** Returns the given log. */ |
---|
| 414 | public synchronized Log log(int x) |
---|
| 415 | { |
---|
| 416 | return (Log)logs.elementAt(x); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | /** Removes the given log. */ |
---|
| 420 | public synchronized Log removeLog(int x) |
---|
| 421 | { |
---|
| 422 | Log l = log(x); |
---|
| 423 | logs.removeElementAt(x); |
---|
| 424 | return l; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | /** Prints an initial error to System.err. This is only to |
---|
| 428 | be used by ec.Evolve in starting up the system. */ |
---|
| 429 | public static void initialError(String s) |
---|
| 430 | { |
---|
| 431 | System.err.println("STARTUP ERROR:\n" + s); |
---|
| 432 | |
---|
| 433 | // just in case... |
---|
| 434 | System.out.flush(); |
---|
| 435 | System.err.flush(); |
---|
| 436 | System.exit(1); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | /** Prints an initial error to System.err. This is only to |
---|
| 440 | be used by ec.Evolve in starting up the system. */ |
---|
| 441 | public static void initialError(String s, Parameter p1) |
---|
| 442 | { |
---|
| 443 | System.err.println("STARTUP ERROR:\n" + s); |
---|
| 444 | if (p1!=null) System.err.println("PARAMETER: " + p1); |
---|
| 445 | |
---|
| 446 | // just in case... |
---|
| 447 | System.out.flush(); |
---|
| 448 | System.err.flush(); |
---|
| 449 | System.exit(1); |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | /** Prints an initial error to System.err. This is only to |
---|
| 453 | be used by ec.Evolve in starting up the system. */ |
---|
| 454 | public static void initialError(String s, Parameter p1, Parameter p2) |
---|
| 455 | { |
---|
| 456 | System.err.println("STARTUP ERROR:\n" + s); |
---|
| 457 | if (p1!=null) System.err.println("PARAMETER: " + p1); |
---|
| 458 | if (p2!=null && p1!=null) System.err.println(" ALSO: " + p2); |
---|
| 459 | |
---|
| 460 | // just in case... |
---|
| 461 | System.out.flush(); |
---|
| 462 | System.err.flush(); |
---|
| 463 | System.exit(1); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | /** Prints an initial message to System.err. This is only to |
---|
| 467 | be used by ec.Evolve in starting up the system. These messages are not logged. */ |
---|
| 468 | public static void initialMessage(String s) |
---|
| 469 | { |
---|
| 470 | System.err.println(s); |
---|
| 471 | System.err.flush(); |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | /** Posts a system message. */ |
---|
| 475 | public synchronized void systemMessage(String s) |
---|
| 476 | { |
---|
| 477 | println(s, V_NO_MESSAGES ,ALL_LOGS, true); |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | /** Posts a fatal error. This causes the system to exit. */ |
---|
| 481 | public synchronized void fatal(String s) |
---|
| 482 | { |
---|
| 483 | println("FATAL ERROR:\n"+s, ALL_LOGS, true); |
---|
| 484 | exitWithError(); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | /** Posts a fatal error. This causes the system to exit. */ |
---|
| 488 | public synchronized void fatal(String s, Parameter p1) |
---|
| 489 | { |
---|
| 490 | println("FATAL ERROR:\n"+s, ALL_LOGS, true); |
---|
| 491 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 492 | exitWithError(); |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | /** Posts a fatal error. This causes the system to exit. */ |
---|
| 496 | public synchronized void fatal(String s, Parameter p1, Parameter p2) |
---|
| 497 | { |
---|
| 498 | println("FATAL ERROR:\n"+s, ALL_LOGS, true); |
---|
| 499 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 500 | if (p2!=null && p1!=null) println(" ALSO: " + p2, ALL_LOGS, true); |
---|
| 501 | else println("PARAMETER: " + p2, ALL_LOGS, true); |
---|
| 502 | exitWithError(); |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | /** Posts a simple error. This causes the error flag to be raised as well. */ |
---|
| 506 | public synchronized void error(String s) |
---|
| 507 | { |
---|
| 508 | println("ERROR:\n"+s, ALL_LOGS, true); |
---|
| 509 | errors = true; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | /** Posts a simple error. This causes the error flag to be raised as well. */ |
---|
| 513 | public synchronized void error(String s, Parameter p1) |
---|
| 514 | { |
---|
| 515 | println("ERROR:\n"+s, ALL_LOGS, true); |
---|
| 516 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 517 | errors = true; |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | /** Posts a simple error. This causes the error flag to be raised as well. */ |
---|
| 521 | public synchronized void error(String s, Parameter p1, Parameter p2) |
---|
| 522 | { |
---|
| 523 | println("ERROR:\n"+s, ALL_LOGS, true); |
---|
| 524 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 525 | if (p2!=null && p1!=null) println(" ALSO: " + p2, ALL_LOGS, true); |
---|
| 526 | else println("PARAMETER: " + p2, ALL_LOGS, true); |
---|
| 527 | errors = true; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | /** Posts a warning. */ |
---|
| 531 | public synchronized void warning(String s, Parameter p1, Parameter p2) |
---|
| 532 | { |
---|
| 533 | println("WARNING:\n"+s, ALL_LOGS, true); |
---|
| 534 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 535 | if (p2!=null && p1!=null) println(" ALSO: " + p2, ALL_LOGS, true); |
---|
| 536 | else println("PARAMETER: " + p2, ALL_LOGS, true); |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | /** Posts a warning. */ |
---|
| 540 | public synchronized void warning(String s, Parameter p1) |
---|
| 541 | { |
---|
| 542 | println("WARNING:\n"+s, ALL_LOGS, true); |
---|
| 543 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | /** Posts a warning. */ |
---|
| 547 | public synchronized void warning(String s) |
---|
| 548 | { |
---|
| 549 | println("WARNING:\n"+s, ALL_LOGS, true); |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | java.util.HashSet oneTimeWarnings = new java.util.HashSet(); |
---|
| 553 | /** Posts a warning one time only. */ |
---|
| 554 | public synchronized void warnOnce(String s) |
---|
| 555 | { |
---|
| 556 | if (!oneTimeWarnings.contains(s)) |
---|
| 557 | { |
---|
| 558 | oneTimeWarnings.add(s); |
---|
| 559 | println("ONCE-ONLY WARNING:\n"+s, ALL_LOGS, true); |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | public synchronized void warnOnce(String s, Parameter p1) |
---|
| 564 | { |
---|
| 565 | if (!oneTimeWarnings.contains(s)) |
---|
| 566 | { |
---|
| 567 | oneTimeWarnings.add(s); |
---|
| 568 | println("ONCE-ONLY WARNING:\n"+s, ALL_LOGS, true); |
---|
| 569 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 570 | } |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | public synchronized void warnOnce(String s, Parameter p1, Parameter p2) |
---|
| 574 | { |
---|
| 575 | if (!oneTimeWarnings.contains(s)) |
---|
| 576 | { |
---|
| 577 | oneTimeWarnings.add(s); |
---|
| 578 | println("ONCE-ONLY WARNING:\n"+s, ALL_LOGS, true); |
---|
| 579 | if (p1!=null) println("PARAMETER: " + p1, ALL_LOGS, true); |
---|
| 580 | if (p2!=null && p1!=null) println(" ALSO: " + p2, ALL_LOGS, true); |
---|
| 581 | else println("PARAMETER: " + p2, ALL_LOGS, true); |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | |
---|
| 585 | |
---|
| 586 | /** Posts a message. */ |
---|
| 587 | public synchronized void message(String s) |
---|
| 588 | { |
---|
| 589 | println(s, ALL_LOGS, true); |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | /** Forces a file-based log to reopen, erasing its previous contents. |
---|
| 593 | non-file logs ignore this. */ |
---|
| 594 | |
---|
| 595 | public synchronized void reopen(int _log) throws IOException |
---|
| 596 | { |
---|
| 597 | Log oldlog = (Log)logs.elementAt(_log); |
---|
| 598 | logs.setElementAt(oldlog.reopen(),_log); |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | /** Forces one or more file-based logs to reopen, erasing |
---|
| 602 | their previous contents. non-file logs ignore this. */ |
---|
| 603 | |
---|
| 604 | public synchronized void reopen(int[] _logs) throws IOException |
---|
| 605 | { |
---|
| 606 | for(int x=0;x<_logs.length;x++) |
---|
| 607 | { |
---|
| 608 | Log oldlog = (Log)logs.elementAt(_logs[x]); |
---|
| 609 | logs.setElementAt(oldlog.reopen(),_logs[x]); |
---|
| 610 | } |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | |
---|
| 614 | /** Prints a message to a given log, with a certain verbosity. |
---|
| 615 | <i>_announcement</i> indicates that the message is an announcement. |
---|
| 616 | @deprecated Verbosity no longer has an effect |
---|
| 617 | */ |
---|
| 618 | |
---|
| 619 | synchronized void println(String s, |
---|
| 620 | int _verbosity, |
---|
| 621 | Log log, |
---|
| 622 | boolean _announcement, |
---|
| 623 | boolean _reposting) throws OutputException |
---|
| 624 | { |
---|
| 625 | if (log.writer==null) throw new OutputException("Log with a null writer: " + log); |
---|
| 626 | if (!log.postAnnouncements && _announcement) return; // don't write it |
---|
| 627 | // if (log.verbosity >= _verbosity) return; // don't write it |
---|
| 628 | // if (verbosity >= _verbosity) return; // don't write it |
---|
| 629 | // now write it |
---|
| 630 | log.writer.println(s); |
---|
| 631 | // if (flush) |
---|
| 632 | // always flush |
---|
| 633 | log.writer.flush(); |
---|
| 634 | //...and stash it in memory maybe |
---|
| 635 | if (store && _announcement && !_reposting) |
---|
| 636 | announcements.addElement(new Announcement(s)); |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | |
---|
| 640 | /** Prints a message to a given log, |
---|
| 641 | with a certain verbosity. If log==ALL_LOGS, posted to all logs which accept announcements. |
---|
| 642 | @deprecated Verbosity no longer has an effect |
---|
| 643 | */ |
---|
| 644 | synchronized void println(String s, |
---|
| 645 | int _verbosity, |
---|
| 646 | int log, |
---|
| 647 | boolean _announcement) throws OutputException |
---|
| 648 | { |
---|
| 649 | if (log==ALL_LOGS) for (int x = 0; x<logs.size();x++) |
---|
| 650 | { |
---|
| 651 | Log l = (Log) logs.elementAt(x); |
---|
| 652 | if (l==null) throw new OutputException("Unknown log number" + l); |
---|
| 653 | println(s,_verbosity,l,_announcement,false); |
---|
| 654 | } |
---|
| 655 | else |
---|
| 656 | { |
---|
| 657 | Log l = (Log) logs.elementAt(log); |
---|
| 658 | if (l==null) throw new OutputException("Unknown log number" + l); |
---|
| 659 | println(s,_verbosity,l,_announcement,false); |
---|
| 660 | } |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | /** Prints a message to a given log. If log==ALL_LOGS, posted to all logs which accept announcements. |
---|
| 664 | */ |
---|
| 665 | public synchronized void println(String s, |
---|
| 666 | int log, |
---|
| 667 | boolean _announcement) throws OutputException |
---|
| 668 | { |
---|
| 669 | println(s, V_VERBOSE, log, _announcement); |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | |
---|
| 673 | /** Prints a non-announcement message to the given logs, |
---|
| 674 | with a certain verbosity. |
---|
| 675 | @deprecated Verbosity no longer has an effect |
---|
| 676 | */ |
---|
| 677 | public synchronized void println(String s, |
---|
| 678 | int _verbosity, |
---|
| 679 | int[] _logs) throws OutputException |
---|
| 680 | { |
---|
| 681 | for(int x=0;x<_logs.length;x++) |
---|
| 682 | println(s,V_VERBOSE,(Log)(logs.elementAt(_logs[x])),false,false); |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | |
---|
| 686 | /** Prints a non-announcement message to the given logs, |
---|
| 687 | with a certain verbosity. |
---|
| 688 | @deprecated Verbosity no longer has an effect |
---|
| 689 | */ |
---|
| 690 | public synchronized void println(String s, |
---|
| 691 | int _verbosity, |
---|
| 692 | int log) throws OutputException |
---|
| 693 | { |
---|
| 694 | println(s,V_VERBOSE,(Log)(logs.elementAt(log)),false,false); |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | |
---|
| 698 | /** Prints a non-announcement message to the given logs, with a verbosity of V_NO_GENERAL. */ |
---|
| 699 | public synchronized void println(String s, |
---|
| 700 | int log) throws OutputException |
---|
| 701 | { |
---|
| 702 | println(s,V_VERBOSE,log); |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | |
---|
| 706 | /** Prints a non-announcement message to a given log, with a |
---|
| 707 | certain verbosity. No '\n' is printed. */ |
---|
| 708 | protected synchronized void print(String s, |
---|
| 709 | int _verbosity, |
---|
| 710 | Log log) throws OutputException |
---|
| 711 | { |
---|
| 712 | if (log.writer==null) throw new OutputException("Log with a null writer: " + log); |
---|
| 713 | //if (log.verbosity >= _verbosity) return; // don't write it |
---|
| 714 | //if (verbosity >= _verbosity) return; // don't write it |
---|
| 715 | // now write it |
---|
| 716 | log.writer.print(s); |
---|
| 717 | // do not flush until you get a println |
---|
| 718 | //if (flush) log.writer.flush(); |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | /** Prints a non-announcement message to a given log, with a |
---|
| 722 | certain verbosity. If log==ALL_LOGS, posted to all logs which accept announcements. |
---|
| 723 | No '\n' is printed. */ |
---|
| 724 | public synchronized void print(String s, |
---|
| 725 | int _verbosity, |
---|
| 726 | int log) throws OutputException |
---|
| 727 | { |
---|
| 728 | if (log==ALL_LOGS) for (int x = 0; x<logs.size();x++) |
---|
| 729 | { |
---|
| 730 | Log l = (Log) logs.elementAt(x); |
---|
| 731 | if (l==null) throw new OutputException("Unknown log number" + l); |
---|
| 732 | print(s,V_VERBOSE,l); |
---|
| 733 | } |
---|
| 734 | else |
---|
| 735 | { |
---|
| 736 | Log l = (Log) logs.elementAt(log); |
---|
| 737 | if (l==null) throw new OutputException("Unknown log number" + l); |
---|
| 738 | print(s,V_VERBOSE,l); |
---|
| 739 | } |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | /** Prints a non-announcement message to a given log<!--, with a verbosity of V_NO_GENERAL-->. |
---|
| 743 | If log==ALL_LOGS, posted to all logs which accept announcements. No '\n' is printed. */ |
---|
| 744 | public synchronized void print(String s, |
---|
| 745 | int log) throws OutputException |
---|
| 746 | { |
---|
| 747 | print(s, V_VERBOSE, log); |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | /** Prints a non-announcement message to the given logs, |
---|
| 751 | with a certain verbosity. No '\n' is printed. |
---|
| 752 | @deprecated Verbosity no longer has any effect |
---|
| 753 | */ |
---|
| 754 | public synchronized void print(String s, |
---|
| 755 | int _verbosity, |
---|
| 756 | int[] _logs) throws OutputException |
---|
| 757 | { |
---|
| 758 | for(int x=0;x<_logs.length;x++) |
---|
| 759 | print(s,_logs[x]); |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | /** Prints a non-announcement message to the given logs, |
---|
| 763 | with a certain verbosity. No '\n' is printed. |
---|
| 764 | */ |
---|
| 765 | public synchronized void print(String s, |
---|
| 766 | int[] _logs) throws OutputException |
---|
| 767 | { |
---|
| 768 | print(s, V_VERBOSE, _logs); |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | /** Exits with a fatal error if the error flag has been raised. */ |
---|
| 772 | public synchronized void exitIfErrors() |
---|
| 773 | { |
---|
| 774 | if (errors) |
---|
| 775 | { |
---|
| 776 | println("SYSTEM EXITING FROM ERRORS\n",ALL_LOGS, true); |
---|
| 777 | exitWithError(); |
---|
| 778 | } |
---|
| 779 | } |
---|
| 780 | |
---|
| 781 | /** Clears the error flag. */ |
---|
| 782 | public synchronized void clearErrors() |
---|
| 783 | { |
---|
| 784 | errors = false; |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | |
---|
| 788 | /** Clears out announcements. Note that this will cause these |
---|
| 789 | announcements to be unavailable for reposting after a restart! */ |
---|
| 790 | public synchronized void clearAnnouncements() |
---|
| 791 | { |
---|
| 792 | if (announcements!=null) |
---|
| 793 | announcements = new Vector(); |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | public synchronized void restart() throws IOException |
---|
| 797 | { |
---|
| 798 | // restart logs, then repost announcements to them |
---|
| 799 | int ls = logs.size(); |
---|
| 800 | for(int x=0;x<ls;x++) |
---|
| 801 | { |
---|
| 802 | Log l = (Log)(logs.elementAt(x)); |
---|
| 803 | logs.setElementAt(l = l.restarter.restart(l),x); |
---|
| 804 | if (l.repostAnnouncementsOnRestart && store) |
---|
| 805 | { |
---|
| 806 | int as = announcements.size(); |
---|
| 807 | for (int y=0;y<as;y++) |
---|
| 808 | { |
---|
| 809 | Announcement a = (Announcement)(announcements.elementAt(y)); |
---|
| 810 | println(a.text,V_VERBOSE,l,true,true); |
---|
| 811 | } |
---|
| 812 | } |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | // exit with a fatal error if the errors flag is set. |
---|
| 816 | exitIfErrors(); |
---|
| 817 | } |
---|
| 818 | |
---|
| 819 | /** Returns a compressing input stream using JZLib (http://www.jcraft.com/jzlib/). If JZLib is not available on your system, this method will return null. */ |
---|
| 820 | public static InputStream makeCompressingInputStream(InputStream in) |
---|
| 821 | { |
---|
| 822 | // to do this, we're going to use reflection. But here's the equivalent code: |
---|
| 823 | /* |
---|
| 824 | return new com.jcraft.jzlib.ZInputStream(in); |
---|
| 825 | */ |
---|
| 826 | try |
---|
| 827 | { |
---|
| 828 | return (InputStream)(Class.forName("com.jcraft.jzlib.ZInputStream").getConstructor(new Class[] { InputStream.class } ).newInstance(new Object[] { in })); |
---|
| 829 | } |
---|
| 830 | catch (Exception e) { return null; } // failed, probably doesn't have JZLib on the system |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | /** Returns a compressing output stream using JZLib (http://www.jcraft.com/jzlib/). If JZLib is not available on your system, this method will return null. */ |
---|
| 834 | public static OutputStream makeCompressingOutputStream(OutputStream out) |
---|
| 835 | { |
---|
| 836 | // to do this, we're going to use reflection. But here's the equivalent code: |
---|
| 837 | /* |
---|
| 838 | com.jcraft.jzlib.ZOutputStream stream = new com.jcraft.jzlib.ZOutputStream(out, com.jcraft.jzlib.JZlib.Z_BEST_SPEED); |
---|
| 839 | stream.setFlushMode(com.jcraft.jzlib.JZlib.Z_SYNC_FLUSH); |
---|
| 840 | return stream; |
---|
| 841 | */ |
---|
| 842 | try |
---|
| 843 | { |
---|
| 844 | Class outz = Class.forName("com.jcraft.jzlib.JZlib"); |
---|
| 845 | int Z_BEST_SPEED = outz.getField("Z_BEST_SPEED").getInt(null); |
---|
| 846 | int Z_SYNC_FLUSH = outz.getField("Z_SYNC_FLUSH").getInt(null); |
---|
| 847 | |
---|
| 848 | Class outc = Class.forName("com.jcraft.jzlib.ZOutputStream"); |
---|
| 849 | Object outi = outc.getConstructor(new Class[] { OutputStream.class, Integer.TYPE }).newInstance(new Object[] { out, new Integer(Z_BEST_SPEED) }); |
---|
| 850 | outc.getMethod("setFlushMode", new Class[] { Integer.TYPE }).invoke(outi, new Object[] { new Integer(Z_SYNC_FLUSH) }); |
---|
| 851 | return (OutputStream) outi; |
---|
| 852 | } |
---|
| 853 | catch (Exception e) { return null; } // failed, probably doesn't have JZLib on the system |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | |
---|
| 857 | class Announcement implements Serializable |
---|
| 858 | { |
---|
| 859 | /** The announcement's...anouncement.*/ |
---|
| 860 | public String text; |
---|
| 861 | |
---|
| 862 | /** Creates a new announcement with text <i>t</i> and verbosity value <i>v</i> */ |
---|
| 863 | public Announcement (String t) |
---|
| 864 | { |
---|
| 865 | text = t; |
---|
| 866 | } |
---|
| 867 | } |
---|
| 868 | |
---|
| 869 | } |
---|