1 22 package org.jboss.logging.util; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.io.Writer ; 27 28 import org.apache.log4j.Category; 29 import org.apache.log4j.Priority; 30 31 45 public class CategoryWriter 46 extends PrintWriter { 47 private Category category; 48 private Priority priority; 49 private boolean inWrite; 50 private boolean issuedWarning; 51 52 57 public CategoryWriter( final Category category ) { 58 this( category, Priority.INFO ); 59 } 60 61 68 public CategoryWriter( final Category category, 69 final Priority priority ) { 70 super( new InternalCategoryWriter( category, priority ), true ); 71 } 72 73 76 static class InternalCategoryWriter extends Writer { 77 private Category category; 78 private Priority priority; 79 private boolean closed; 80 81 public InternalCategoryWriter( final Category category, final Priority priority ) { 82 lock = category; 83 this.category = category; 85 this.priority = priority; 86 } 87 88 public void write( char[] cbuf, int off, int len ) 89 throws IOException { 90 if ( closed ) { 91 throw new IOException ( "Called write on closed Writer" ); 92 } 93 while ( len > 0 && ( cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r' ) ) { 95 len--; 96 } 97 if ( len > 0 ) { 98 category.log( priority, String.copyValueOf( cbuf, off, len ) ); 99 } 100 } 101 102 103 public void flush() 104 throws IOException { 105 if ( closed ) { 106 throw new IOException ( "Called flush on closed Writer" ); 107 } 108 } 109 110 public void close() { 111 closed = true; 112 } 113 } 114 115 } 116 | Popular Tags |