Skip to content

Commit

Permalink
Commit my version of what I forgot before....
Browse files Browse the repository at this point in the history
  • Loading branch information
manning authored and Stanford NLP committed Feb 28, 2016
1 parent 395e1f1 commit e2e96ed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
61 changes: 20 additions & 41 deletions src/edu/stanford/nlp/util/StringUtils.java
@@ -1,5 +1,4 @@
package edu.stanford.nlp.util; package edu.stanford.nlp.util;
import edu.stanford.nlp.util.logging.Redwood;


import edu.stanford.nlp.io.IOUtils; import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.io.RuntimeIOException; import edu.stanford.nlp.io.RuntimeIOException;
Expand All @@ -8,6 +7,7 @@
import edu.stanford.nlp.ling.HasOffset; import edu.stanford.nlp.ling.HasOffset;
import edu.stanford.nlp.ling.HasWord; import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.math.SloppyMath; import edu.stanford.nlp.math.SloppyMath;
import edu.stanford.nlp.util.logging.Redwood;


import java.io.*; import java.io.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
Expand Down Expand Up @@ -51,7 +51,7 @@
public class StringUtils { public class StringUtils {


/** A logger for this class */ /** 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. * Don't let anyone instantiate this class.
Expand Down Expand Up @@ -1086,7 +1086,7 @@ public static String checkRequiredProperties(Properties props,


/** /**
* Prints to a file. If the file already exists, appends if * Prints to a file. If the file already exists, appends if
* <code>append=true</code>, and overwrites if <code>append=false</code>. * {@code append=true}, and overwrites if {@code append=false}.
*/ */
public static void printToFile(File file, String message, boolean append, public static void printToFile(File file, String message, boolean append,
boolean printLn, String encoding) { boolean printLn, String encoding) {
Expand Down Expand Up @@ -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 * Prints to a file. If the file already exists, appends if
* <code>append=true</code>, and overwrites if <code>append=false</code>. * {@code append=true}, and overwrites if {@code append=false}.
*/ */
public static void printToFileLn(File file, String message, boolean append) { public static void printToFileLn(File file, String message, boolean append) {
PrintWriter pw = null; PrintWriter pw = null;
Expand All @@ -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 * Prints to a file. If the file already exists, appends if
* <code>append=true</code>, and overwrites if <code>append=false</code>. * {@code append=true}, and overwrites if {@code append=false}.
*/ */
public static void printToFile(File file, String message, boolean append) { public static void printToFile(File file, String message, boolean append) {
PrintWriter pw = null; PrintWriter pw = null;
Expand All @@ -1149,13 +1149,9 @@ public static void printToFile(File file, String message, boolean append) {
pw = new PrintWriter(fw); pw = new PrintWriter(fw);
pw.print(message); pw.print(message);
} catch (Exception e) { } catch (Exception e) {
log.info("Exception: in printToFile " + file.getAbsolutePath()); throw new RuntimeIOException("Exception in printToFile " + file.getAbsolutePath(), e);
e.printStackTrace();
} finally { } finally {
if (pw != null) { IOUtils.closeIgnoringExceptions(pw);
pw.flush();
pw.close();
}
} }
} }


Expand All @@ -1170,15 +1166,15 @@ public static void printToFile(File file, String message) {


/** /**
* Prints to a file. If the file already exists, appends if * Prints to a file. If the file already exists, appends if
* <code>append=true</code>, and overwrites if <code>append=false</code> * {@code append=true}, and overwrites if {@code append=false}.
*/ */
public static void printToFile(String filename, String message, boolean append) { public static void printToFile(String filename, String message, boolean append) {
printToFile(new File(filename), message, append); printToFile(new File(filename), message, append);
} }


/** /**
* Prints to a file. If the file already exists, appends if * Prints to a file. If the file already exists, appends if
* <code>append=true</code>, and overwrites if <code>append=false</code> * {@code append=true}, and overwrites if {@code append=false}.
*/ */
public static void printToFileLn(String filename, String message, boolean append) { public static void printToFileLn(String filename, String message, boolean append) {
printToFileLn(new File(filename), message, append); printToFileLn(new File(filename), message, append);
Expand Down Expand Up @@ -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'. <p/> * Strip directory from filename. Like Unix 'basename'. <p/>
* *
* Example: <code>getBaseName("/u/wcmac/foo.txt") ==> "foo.txt"</code> * Example: {@code getBaseName("/u/wcmac/foo.txt") ==> "foo.txt"}
*/ */
public static String getBaseName(String fileName) { public static String getBaseName(String fileName) {
return getBaseName(fileName, ""); return getBaseName(fileName, "");
Expand Down Expand Up @@ -2648,19 +2629,17 @@ public static String expandEnvironmentVariables(String raw) {
* Logs the command line arguments to Redwood on the given channels. * Logs the command line arguments to Redwood on the given channels.
* The logger should be a RedwoodChannels of a single channel: the main class. * 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. * @param args The command-line arguments to log.
*/ */
public static void logInvocationString(Redwood.RedwoodChannels log, String[] args) { public static void logInvocationString(Redwood.RedwoodChannels logger, String[] args) {
if (log.channelNames.length > 0) { StringBuilder sb = new StringBuilder("Invoked on ");
if (log.channelNames[0] instanceof Class) { sb.append(new Date());
log.info(toInvocationString(((Class) log.channelNames[0]).getSimpleName(), args)); sb.append(" with arguments:");
} else { for (String arg : args) {
log.info(toInvocationString(log.channelNames[0].toString(), args)); sb.append(' ').append(arg);

}
} else {
log.info(toInvocationString("CoreNLP", args));
} }
logger.info(sb.toString());
} }

} }
6 changes: 5 additions & 1 deletion src/edu/stanford/nlp/util/logging/Redwood.java
Expand Up @@ -955,6 +955,10 @@ public static class Util {
private Util() {} // static methods private Util() {} // static methods


private static Object[] revConcat(Object[] B, Object... A) { 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]; Object[] C = new Object[A.length+B.length];
System.arraycopy(A, 0, C, 0, A.length); System.arraycopy(A, 0, C, 0, A.length);
System.arraycopy(B, 0, C, A.length, B.length); System.arraycopy(B, 0, C, A.length, B.length);
Expand Down Expand Up @@ -1195,7 +1199,7 @@ public static void printChannels(int width){
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static class RedwoodChannels { public static class RedwoodChannels {
public final Object[] channelNames; private final Object[] channelNames;


public RedwoodChannels(Object... channelNames) { public RedwoodChannels(Object... channelNames) {
this.channelNames = channelNames; this.channelNames = channelNames;
Expand Down

0 comments on commit e2e96ed

Please sign in to comment.