Debian Codegraph @

src/dr/util/ErrorLogHandler.java

raw symbols SCIP
1 /*
2 * ErrorLogHandler.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 // Line separator string. This is the value of the line.separator
71 // property at the moment that the SimpleFormatter was created.
72 private final String lineSeparator = System.getProperty("line.separator");
73
74 // AR - is there a reason why this was used? It causes warnings at compile
75 // private final String lineSeparator = (String) java.security.AccessController.doPrivileged(
76 // new sun.security.action.GetPropertyAction("line.separator"));
77
78 /**
79 * Format the given LogRecord.
80 *
81 * @param record the log record to be formatted.
82 * @return a formatted log record
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 }