package org.jboss.logging.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
public class CategoryWriter
extends PrintWriter {
private Category category;
private Priority priority;
private boolean inWrite;
private boolean issuedWarning;
public CategoryWriter( final Category category ) {
this( category, Priority.INFO );
}
public CategoryWriter( final Category category,
final Priority priority ) {
super( new InternalCategoryWriter( category, priority ), true );
}
static class InternalCategoryWriter extends Writer {
private Category category;
private Priority priority;
private boolean closed;
public InternalCategoryWriter( final Category category, final Priority priority ) {
lock = category;
this.category = category;
this.priority = priority;
}
public void write( char[] cbuf, int off, int len )
throws IOException {
if ( closed ) {
throw new IOException( "Called write on closed Writer" );
}
while ( len > 0 && ( cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r' ) ) {
len--;
}
if ( len > 0 ) {
category.log( priority, String.copyValueOf( cbuf, off, len ) );
}
}
public void flush()
throws IOException {
if ( closed ) {
throw new IOException( "Called flush on closed Writer" );
}
}
public void close() {
closed = true;
}
}
}