From 1aaf3d8a4e6aab89de5a277dafdd3f4951da8740 Mon Sep 17 00:00:00 2001 From: GuyBaele Date: Fri, 17 May 2024 20:37:47 +0200 Subject: [PATCH] working on logging citations to file --- src/dr/app/beast/BeastMain.java | 28 +++++++----- src/dr/app/beast/BeastParser.java | 12 ++--- src/dr/util/CitationLogHandler.java | 70 +++++++++++++++++++++++++++++ src/dr/xml/XMLParser.java | 4 +- 4 files changed, 97 insertions(+), 17 deletions(-) create mode 100644 src/dr/util/CitationLogHandler.java diff --git a/src/dr/app/beast/BeastMain.java b/src/dr/app/beast/BeastMain.java index 6e057fd088..d3c58c7ddd 100644 --- a/src/dr/app/beast/BeastMain.java +++ b/src/dr/app/beast/BeastMain.java @@ -37,6 +37,7 @@ import dr.inference.mcmcmc.MCMCMC; import dr.inference.mcmcmc.MCMCMCOptions; import dr.math.MathUtils; +import dr.util.CitationLogHandler; import dr.util.ErrorLogHandler; import dr.util.MessageLogHandler; import dr.util.Version; @@ -135,15 +136,18 @@ public boolean isLoggable(LogRecord record) { }); infoLogger.addHandler(errorHandler); - FileOutputStream citationStream = null; - if (System.getProperty("citations.filename") != null) { - citationStream = new FileOutputStream(System.getProperty("citations.filename")); - - } else { - citationStream = new FileOutputStream(fileName.substring(0, fileName.toLowerCase().indexOf(".xml")) + CITATION_FILE_SUFFIX); + if (Boolean.parseBoolean(System.getProperty("output_citations"))) { + FileOutputStream citationStream = null; + if (System.getProperty("citations.filename") != null) { + citationStream = new FileOutputStream(System.getProperty("citations.filename")); + } else { + citationStream = new FileOutputStream(fileName.substring(0, fileName.toLowerCase().indexOf(".xml")) + CITATION_FILE_SUFFIX); + } + //Handler citationHandler = new MessageLogHandler(citationStream); + Handler citationHandler = CitationLogHandler.getHandler(citationStream); + //Logger.getLogger("dr.app.beast").addHandler(citationHandler); + Logger.getLogger("dr.util").addHandler(citationHandler); } - Handler citationHandler = new MessageLogHandler(citationStream); - Logger.getLogger("dr.app.beast").addHandler(citationHandler); logger.setUseParentHandlers(false); @@ -158,7 +162,6 @@ public boolean isLoggable(LogRecord record) { messageHandler.setLevel(Level.WARNING); errorLogger.addHandler(messageHandler); - PluginLoader.loadPlugins(parser); // Install the checkpointer. This creates a factory that returns @@ -417,7 +420,7 @@ public static void main(String[] args) throws java.io.IOException { new Arguments.Option("force_resume", "Force resuming from a saved state"), new Arguments.StringOption("citations_file", "FILENAME", "Specify a filename to write a citation list to"), - //new Arguments.Option("citations_off", "Turn off writing citations to file"), + new Arguments.Option("citations_off", "Turn off writing citations to file"), new Arguments.StringOption("plugins_dir", "FILENAME", "Specify a directory to load plugins from, multiple can be separated with ':' "), new Arguments.Option("version", "Print the version and credits and stop"), @@ -700,6 +703,11 @@ public void uncaughtException(Thread t, Throwable e) { arguments.getStringOption("plugins_dir")+":"+System.getProperty("beast.plugins.dir")); } + if (arguments.hasOption("citations_off")) { + System.setProperty("output_citations", Boolean.FALSE.toString()); + } else { + System.setProperty("output_citations", Boolean.TRUE.toString()); + } if (!usingSMC) { // ignore these other options diff --git a/src/dr/app/beast/BeastParser.java b/src/dr/app/beast/BeastParser.java index f8ab36f9e1..ff88255a53 100644 --- a/src/dr/app/beast/BeastParser.java +++ b/src/dr/app/beast/BeastParser.java @@ -25,7 +25,6 @@ package dr.app.beast; -import beagle.BeagleInfo; import dr.util.Citation; import dr.util.Pair; import dr.util.Version; @@ -209,7 +208,8 @@ private void loadProperties(Class c, String parsersFile, boolean verbose, boolea @Override protected void executingRunnable() { - Logger.getLogger("dr.app.beast").info("\nCitations for this analysis: "); + //Logger.getLogger("dr.app.beast").info("\nCitations for this analysis: "); + Logger.getLogger("dr.util").info("\nCitations for this analysis: "); Map>> categoryMap = new LinkedHashMap>>(); @@ -226,14 +226,14 @@ protected void executingRunnable() { } for (String category : categoryMap.keySet()) { - Logger.getLogger("dr.app.beast").info("\n"+category.toUpperCase()); + Logger.getLogger("dr.util").info("\n"+category.toUpperCase()); Set> pairSet = categoryMap.get(category); for (PairkeyPair : pairSet) { - Logger.getLogger("dr.app.beast").info(keyPair.second + ":"); + Logger.getLogger("dr.util").info(keyPair.second + ":"); for (Citation citation : getCitationStore().get(keyPair)) { - Logger.getLogger("dr.app.beast").info("\t" + citation.toString()); + Logger.getLogger("dr.util").info("\t" + citation.toString()); } } } @@ -241,7 +241,7 @@ protected void executingRunnable() { // clear the citation store so all the same citations don't get cited again getCitationStore().clear(); - Logger.getLogger("dr.app.beast").info("\n"); + Logger.getLogger("dr.util").info("\n"); } diff --git a/src/dr/util/CitationLogHandler.java b/src/dr/util/CitationLogHandler.java new file mode 100644 index 0000000000..24ed9a2f05 --- /dev/null +++ b/src/dr/util/CitationLogHandler.java @@ -0,0 +1,70 @@ +/* + * ErrorLogHandler.java + * + * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard + * + * This file is part of BEAST. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership and licensing. + * + * BEAST is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * BEAST is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with BEAST; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +package dr.util; + +import java.io.OutputStream; +import java.util.logging.Formatter; +import java.util.logging.LogRecord; +import java.util.logging.StreamHandler; + +public class CitationLogHandler extends StreamHandler { + private static final CitationLogHandler INSTANCE = new CitationLogHandler(); + private final MessageLogFormatter citationFormatter = new MessageLogFormatter(); + + public static CitationLogHandler getHandler(OutputStream stream) { + INSTANCE.setFormatter(INSTANCE.citationFormatter); + INSTANCE.setOutputStream(stream); + return INSTANCE; + } + + public static void closeHandler() { + INSTANCE.close(); + } + + private class MessageLogFormatter extends Formatter { + + // Line separator string. This is the value of the line.separator + // property at the moment that the SimpleFormatter was created. + private final String lineSeparator = System.getProperty("line.separator"); + + // AR - is there a reason why this was used? It causes warnings at compile +// private final String lineSeparator = (String) java.security.AccessController.doPrivileged( +// new sun.security.action.GetPropertyAction("line.separator")); + + /** + * Format the given LogRecord. + * @param record the log record to be formatted. + * @return a formatted log record + */ + public synchronized String format(LogRecord record) { + final StringBuffer sb = new StringBuffer(); + sb.append(formatMessage(record)); + sb.append(lineSeparator); + return sb.toString(); + } + } + +} diff --git a/src/dr/xml/XMLParser.java b/src/dr/xml/XMLParser.java index 25390ff4e7..14fe331037 100644 --- a/src/dr/xml/XMLParser.java +++ b/src/dr/xml/XMLParser.java @@ -262,7 +262,6 @@ private Object convert(Element e, Class target, XMLObject parent, boolean run, b if (verbose) System.out.println(" Restoring idref=" + idref); - return new Reference(restoredXMLObject); } else { @@ -379,6 +378,9 @@ private Object convert(Element e, Class target, XMLObject parent, boolean run, b executingRunnable(); + //close citationHandler + CitationLogHandler.closeHandler(); + if (obj instanceof Spawnable && !((Spawnable) obj).getSpawnable()) { ((Spawnable) obj).run(); } else {