| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.util; |
| 27 |
|
| 28 |
import java.util.logging.Formatter; |
| 29 |
import java.util.logging.Level; |
| 30 |
import java.util.logging.LogRecord; |
| 31 |
import java.util.logging.StreamHandler; |
| 32 |
|
| 33 |
public class ErrorLogHandler extends StreamHandler { |
| 34 |
|
| 35 |
public ErrorLogHandler(int maxErrorCount) { |
| 36 |
setOutputStream(System.err); |
| 37 |
setFormatter(new MessageLogFormatter()); |
| 38 |
|
| 39 |
this.maxErrorCount = maxErrorCount; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
public void publish(LogRecord record) { |
| 44 |
super.publish(record); |
| 45 |
flush(); |
| 46 |
|
| 47 |
if (record.getLevel() == Level.SEVERE) { |
| 48 |
errorCount++; |
| 49 |
|
| 50 |
if (errorCount > maxErrorCount) { |
| 51 |
if (errorCount > 1) { |
| 52 |
throw new RuntimeException("ErrorLog: Maximum number of errors (" + (maxErrorCount + 1) + ") reached. Terminating BEAST"); |
| 53 |
} else { |
| 54 |
throw new RuntimeException("An error was encountered. Terminating BEAST"); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public void close() { |
| 61 |
flush(); |
| 62 |
} |
| 63 |
|
| 64 |
public int getErrorCount() { |
| 65 |
return errorCount; |
| 66 |
} |
| 67 |
|
| 68 |
private class MessageLogFormatter extends Formatter { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private final String lineSeparator = System.getProperty("line.separator"); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public synchronized String format(LogRecord record) { |
| 85 |
StringBuffer sb = new StringBuffer(); |
| 86 |
String message = formatMessage(record); |
| 87 |
sb.append(message); |
| 88 |
sb.append(lineSeparator); |
| 89 |
return sb.toString(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
private final int maxErrorCount; |
| 95 |
private int errorCount = 0; |
| 96 |
} |