Index: lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java =================================================================== --- lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java (revision 1206426) +++ lucene/src/java/org/apache/lucene/index/IndexWriterConfig.java (working copy) @@ -695,6 +695,10 @@ * to this. */ public IndexWriterConfig setInfoStream(InfoStream infoStream) { + if (infoStream == null) { + throw new IllegalArgumentException("Cannot set InfoStream implementation to null. "+ + "To disable logging use InfoStream.NO_OUTPUT"); + } this.infoStream = infoStream; return this; } @@ -703,8 +707,7 @@ * Convenience method that uses {@link PrintStreamInfoStream} */ public IndexWriterConfig setInfoStream(PrintStream printStream) { - this.infoStream = printStream == null ? null : new PrintStreamInfoStream(printStream); - return this; + return setInfoStream(printStream == null ? InfoStream.NO_OUTPUT : new PrintStreamInfoStream(printStream)); } @Override @@ -725,7 +728,7 @@ sb.append("maxBufferedDocs=").append(maxBufferedDocs).append("\n"); sb.append("mergedSegmentWarmer=").append(mergedSegmentWarmer).append("\n"); sb.append("codec=").append(codec).append("\n"); - sb.append("infoStream=").append(infoStream == null ? "null" : infoStream.getClass().getName()).append("\n"); + sb.append("infoStream=").append(infoStream.getClass().getName()).append("\n"); sb.append("mergePolicy=").append(mergePolicy).append("\n"); sb.append("indexerThreadPool=").append(indexerThreadPool).append("\n"); sb.append("readerPooling=").append(readerPooling).append("\n"); Index: lucene/src/java/org/apache/lucene/util/InfoStream.java =================================================================== --- lucene/src/java/org/apache/lucene/util/InfoStream.java (revision 1206426) +++ lucene/src/java/org/apache/lucene/util/InfoStream.java (working copy) @@ -26,22 +26,43 @@ private static final AtomicInteger MESSAGE_ID = new AtomicInteger(); protected final int messageID = MESSAGE_ID.getAndIncrement(); + /** Instance of InfoStream that does no logging at all. */ + public static final InfoStream NO_OUTPUT = new NoOutput(); + private static final class NoOutput extends InfoStream { + @Override + public void message(String component, String message) {} + + @Override + public boolean isEnabled(String component) { return false; } + + @Override + public void close() {} + } + /** prints a message */ public abstract void message(String component, String message); - private static InfoStream defaultInfoStream; + /** returns true if messages are enabled and should be posted to {@link #message}. */ + public abstract boolean isEnabled(String component); - /** The default infoStream (possibly null) used - * by a newly instantiated classes. + private static InfoStream defaultInfoStream = NO_OUTPUT; + + /** The default {@code InfoStream} used by a newly instantiated classes. * @see #setDefault */ - public static InfoStream getDefault() { + public static synchronized InfoStream getDefault() { return defaultInfoStream; } - /** Sets the default infoStream (possibly null) used - * by a newly instantiated classes. - * @see #setDefault */ - public static void setDefault(InfoStream infoStream) { + /** Sets the default {@code InfoStream} used + * by a newly instantiated classes. It cannot be {@code null}, + * to disable logging use {@link #NO_OUTPUT}. + * @see #getDefault */ + public static synchronized void setDefault(InfoStream infoStream) { + if (infoStream == null) { + throw new IllegalArgumentException("Cannot set InfoStream default implementation to null. "+ + "To disable logging use InfoStream.NO_OUTPUT"); + } defaultInfoStream = infoStream; } + } Index: lucene/src/java/org/apache/lucene/util/PrintStreamInfoStream.java =================================================================== --- lucene/src/java/org/apache/lucene/util/PrintStreamInfoStream.java (revision 1206426) +++ lucene/src/java/org/apache/lucene/util/PrintStreamInfoStream.java (working copy) @@ -37,6 +37,11 @@ } @Override + public boolean isEnabled(String component) { + return true; + } + + @Override public void close() throws IOException { if (!isSystemStream()) { stream.close(); Index: lucene/src/test-framework/java/org/apache/lucene/util/FailOnNonBulkMergesInfoStream.java =================================================================== --- lucene/src/test-framework/java/org/apache/lucene/util/FailOnNonBulkMergesInfoStream.java (revision 1206426) +++ lucene/src/test-framework/java/org/apache/lucene/util/FailOnNonBulkMergesInfoStream.java (working copy) @@ -26,6 +26,11 @@ @Override public void close() throws IOException { } + + @Override + public boolean isEnabled(String component) { + return true; + } @Override public void message(String component, String message) { Index: lucene/src/test-framework/java/org/apache/lucene/util/NullInfoStream.java =================================================================== --- lucene/src/test-framework/java/org/apache/lucene/util/NullInfoStream.java (revision 1206426) +++ lucene/src/test-framework/java/org/apache/lucene/util/NullInfoStream.java (working copy) @@ -20,7 +20,7 @@ import java.io.IOException; /** - * Prints nothing. Just to make sure tests pass w/ and without infostream + * Prints nothing. Just to make sure tests pass w/ and without enabled InfoStream * without actually making noise. * @lucene.experimental */ @@ -32,6 +32,11 @@ } @Override + public boolean isEnabled(String component) { + return true; // to actually enable logging, we just ignore on message() + } + + @Override public void close() throws IOException { } } Index: lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java (revision 1206426) +++ lucene/src/test/org/apache/lucene/index/TestIndexWriterExceptions.java (working copy) @@ -24,6 +24,7 @@ import java.util.Iterator; import java.util.List; import java.util.Random; +import java.util.concurrent.atomic.AtomicBoolean; import org.apache.lucene.analysis.*; import org.apache.lucene.document.Document; @@ -960,19 +961,23 @@ // LUCENE-1429 public void testOutOfMemoryErrorCausesCloseToFail() throws Exception { - final List thrown = new ArrayList(); + final AtomicBoolean thrown = new AtomicBoolean(false); final Directory dir = newDirectory(); final IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setInfoStream(new InfoStream() { @Override public void message(String component, final String message) { - if (message.startsWith("now flush at close") && 0 == thrown.size()) { - thrown.add(null); + if (message.startsWith("now flush at close") && thrown.compareAndSet(false, true)) { throw new OutOfMemoryError("fake OOME at " + message); } } @Override + public boolean isEnabled(String component) { + return true; + } + + @Override public void close() throws IOException {} }));