1 22 package org.jboss.logging.util; 23 24 import java.io.IOException ; 25 import java.io.PrintStream ; 26 27 import org.apache.log4j.Category; 28 import org.apache.log4j.Priority; 29 30 31 43 public class CategoryStream 44 extends PrintStream 45 { 46 51 public static final boolean TRACE = 52 getBoolean(CategoryStream.class.getName() + ".trace", false); 53 54 55 private static boolean getBoolean(String name, boolean defaultValue) 56 { 57 String value = System.getProperty(name, null); 58 if (value == null) 59 return defaultValue; 60 return new Boolean (value).booleanValue(); 61 } 62 63 private Category category; 64 private Priority priority; 65 private boolean issuedWarning; 66 67 70 public CategoryStream(final Category category) 71 { 72 this(category, Priority.INFO, System.out); 73 } 74 75 79 public CategoryStream(final Category category, 80 final Priority priority, 81 final PrintStream ps) 82 { 83 super(ps); 84 this.category = category; 85 this.priority = priority; 86 } 87 88 public void println(String msg) 89 { 90 if( msg == null ) 91 msg = "null"; 92 byte[] bytes = msg.getBytes(); 93 write(bytes, 0, bytes.length); 94 } 95 96 public void println(Object msg) 97 { 98 if( msg == null ) 99 msg = "null"; 100 byte[] bytes = msg.toString().getBytes(); 101 write(bytes, 0, bytes.length); 102 } 103 104 public void write(byte b) 105 { 106 byte[] bytes = {b}; 107 write(bytes, 0, 1); 108 } 109 110 private ThreadLocal recursiveCheck = new ThreadLocal (); 111 public void write(byte[] b, int off, int len) 112 { 113 Boolean recursed = (Boolean )recursiveCheck.get(); 114 if (recursed != null && recursed.equals(Boolean.TRUE)) 115 { 116 120 if( issuedWarning == false ) 121 { 122 String msg = "ERROR: invalid console appender config detected, console stream is looping"; 123 try 124 { 125 out.write(msg.getBytes()); 126 } 127 catch(IOException ignore) 128 { 129 } 130 issuedWarning = true; 131 } 132 return; 133 } 134 135 while( len > 0 && (b[len-1] == '\n' || b[len-1] == '\r') && len > off ) 137 len --; 138 139 if (len != 0) 143 { 144 String msg = new String (b, off, len); 145 recursiveCheck.set(Boolean.TRUE); 146 if (TRACE) 147 { 148 category.log(priority, msg, new Throwable ()); 149 } 150 else 151 { 152 category.log(priority, msg); 153 } 154 recursiveCheck.set(Boolean.FALSE); 155 } 156 } 157 } 158 | Popular Tags |