From e2e96edbdf94b083e590a8facdb9633fea04f40d Mon Sep 17 00:00:00 2001 From: Christopher Manning Date: Sat, 27 Feb 2016 21:30:07 -0800 Subject: [PATCH] Commit my version of what I forgot before.... --- src/edu/stanford/nlp/util/StringUtils.java | 61 ++++++------------- .../stanford/nlp/util/logging/Redwood.java | 6 +- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/src/edu/stanford/nlp/util/StringUtils.java b/src/edu/stanford/nlp/util/StringUtils.java index a5e40e88d3..45c60645f4 100644 --- a/src/edu/stanford/nlp/util/StringUtils.java +++ b/src/edu/stanford/nlp/util/StringUtils.java @@ -1,5 +1,4 @@ -package edu.stanford.nlp.util; -import edu.stanford.nlp.util.logging.Redwood; +package edu.stanford.nlp.util; import edu.stanford.nlp.io.IOUtils; import edu.stanford.nlp.io.RuntimeIOException; @@ -8,6 +7,7 @@ import edu.stanford.nlp.ling.HasOffset; import edu.stanford.nlp.ling.HasWord; import edu.stanford.nlp.math.SloppyMath; +import edu.stanford.nlp.util.logging.Redwood; import java.io.*; import java.lang.reflect.Field; @@ -51,7 +51,7 @@ public class StringUtils { /** A logger for this class */ - private static Redwood.RedwoodChannels log = Redwood.channels(StringUtils.class); + private static final Redwood.RedwoodChannels log = Redwood.channels(StringUtils.class); /** * Don't let anyone instantiate this class. @@ -1086,7 +1086,7 @@ public static String checkRequiredProperties(Properties props, /** * Prints to a file. If the file already exists, appends if - * append=true, and overwrites if append=false. + * {@code append=true}, and overwrites if {@code append=false}. */ public static void printToFile(File file, String message, boolean append, boolean printLn, String encoding) { @@ -1119,7 +1119,7 @@ public static void printToFile(File file, String message, boolean append, /** * Prints to a file. If the file already exists, appends if - * append=true, and overwrites if append=false. + * {@code append=true}, and overwrites if {@code append=false}. */ public static void printToFileLn(File file, String message, boolean append) { PrintWriter pw = null; @@ -1140,7 +1140,7 @@ public static void printToFileLn(File file, String message, boolean append) { /** * Prints to a file. If the file already exists, appends if - * append=true, and overwrites if append=false. + * {@code append=true}, and overwrites if {@code append=false}. */ public static void printToFile(File file, String message, boolean append) { PrintWriter pw = null; @@ -1149,13 +1149,9 @@ public static void printToFile(File file, String message, boolean append) { pw = new PrintWriter(fw); pw.print(message); } catch (Exception e) { - log.info("Exception: in printToFile " + file.getAbsolutePath()); - e.printStackTrace(); + throw new RuntimeIOException("Exception in printToFile " + file.getAbsolutePath(), e); } finally { - if (pw != null) { - pw.flush(); - pw.close(); - } + IOUtils.closeIgnoringExceptions(pw); } } @@ -1170,7 +1166,7 @@ public static void printToFile(File file, String message) { /** * Prints to a file. If the file already exists, appends if - * append=true, and overwrites if append=false + * {@code append=true}, and overwrites if {@code append=false}. */ public static void printToFile(String filename, String message, boolean append) { printToFile(new File(filename), message, append); @@ -1178,7 +1174,7 @@ public static void printToFile(String filename, String message, boolean append) /** * Prints to a file. If the file already exists, appends if - * append=true, and overwrites if append=false + * {@code append=true}, and overwrites if {@code append=false}. */ public static void printToFileLn(String filename, String message, boolean append) { printToFileLn(new File(filename), message, append); @@ -1891,25 +1887,10 @@ public static String chomp(Object o) { } - public static void printErrInvocationString(String cls, String[] args) { - log.info(toInvocationString(cls, args)); - } - - - public static String toInvocationString(String cls, String[] args) { - StringBuilder sb = new StringBuilder(); - sb.append(cls).append(" invoked on ").append(new Date()); - sb.append(" with arguments:\n "); - for (String arg : args) { - sb.append(' ').append(arg); - } - return sb.toString(); - } - /** * Strip directory from filename. Like Unix 'basename'.

* - * Example: getBaseName("/u/wcmac/foo.txt") ==> "foo.txt" + * Example: {@code getBaseName("/u/wcmac/foo.txt") ==> "foo.txt"} */ public static String getBaseName(String fileName) { return getBaseName(fileName, ""); @@ -2648,19 +2629,17 @@ public static String expandEnvironmentVariables(String raw) { * Logs the command line arguments to Redwood on the given channels. * The logger should be a RedwoodChannels of a single channel: the main class. * - * @param log The redwood logger to log to. + * @param logger The redwood logger to log to. * @param args The command-line arguments to log. */ - public static void logInvocationString(Redwood.RedwoodChannels log, String[] args) { - if (log.channelNames.length > 0) { - if (log.channelNames[0] instanceof Class) { - log.info(toInvocationString(((Class) log.channelNames[0]).getSimpleName(), args)); - } else { - log.info(toInvocationString(log.channelNames[0].toString(), args)); - - } - } else { - log.info(toInvocationString("CoreNLP", args)); + public static void logInvocationString(Redwood.RedwoodChannels logger, String[] args) { + StringBuilder sb = new StringBuilder("Invoked on "); + sb.append(new Date()); + sb.append(" with arguments:"); + for (String arg : args) { + sb.append(' ').append(arg); } + logger.info(sb.toString()); } + } diff --git a/src/edu/stanford/nlp/util/logging/Redwood.java b/src/edu/stanford/nlp/util/logging/Redwood.java index 69a5148417..0464f69415 100644 --- a/src/edu/stanford/nlp/util/logging/Redwood.java +++ b/src/edu/stanford/nlp/util/logging/Redwood.java @@ -955,6 +955,10 @@ public static class Util { private Util() {} // static methods private static Object[] revConcat(Object[] B, Object... A) { + // A is empty whenever do info level logging; B is only empty for blank logging line + if (A.length == 0) { + return B; + } Object[] C = new Object[A.length+B.length]; System.arraycopy(A, 0, C, 0, A.length); System.arraycopy(B, 0, C, A.length, B.length); @@ -1195,7 +1199,7 @@ public static void printChannels(int width){ */ @SuppressWarnings("unused") public static class RedwoodChannels { - public final Object[] channelNames; + private final Object[] channelNames; public RedwoodChannels(Object... channelNames) { this.channelNames = channelNames;