Skip to content

Commit

Permalink
working on logging citations to file
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyBaele committed May 17, 2024
1 parent 31e0ba0 commit 1aaf3d8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 17 deletions.
28 changes: 18 additions & 10 deletions src/dr/app/beast/BeastMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/dr/app/beast/BeastParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package dr.app.beast;

import beagle.BeagleInfo;
import dr.util.Citation;
import dr.util.Pair;
import dr.util.Version;
Expand Down Expand Up @@ -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<String, Set<Pair<String, String>>> categoryMap = new LinkedHashMap<String, Set<Pair<String, String>>>();

Expand All @@ -226,22 +226,22 @@ 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<Pair<String, String>> pairSet = categoryMap.get(category);

for (Pair<String, String>keyPair : 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());
}
}
}

// 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");

}

Expand Down
70 changes: 70 additions & 0 deletions src/dr/util/CitationLogHandler.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
4 changes: 3 additions & 1 deletion src/dr/xml/XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1aaf3d8

Please sign in to comment.