1 16 package org.mortbay.log; 17 18 import java.io.PrintStream ; 19 20 import org.apache.commons.logging.Log; 21 import org.mortbay.log.LogFactory; 22 import org.mortbay.util.ByteArrayOutputStream2; 23 24 30 public class LogStream extends PrintStream 31 { 32 public static class STDERR extends LogStream 33 {STDERR() {super("STDERR ",LogFactory.getLog("stderr"));}} 34 35 public static class STDOUT extends LogStream 36 {STDOUT() {super("STDOUT ",LogFactory.getLog("stdout"));}} 37 38 39 final static PrintStream STDERR_STREAM=System.err; 40 final static PrintStream STDOUT_STREAM=System.out; 41 42 43 47 public static void setLogStdErr(boolean log) 48 { 49 if (log) 50 { 51 if (!(System.err instanceof LogStream)) 52 System.setErr(new LogStream.STDERR()); 53 } 54 else 55 System.setErr(STDERR_STREAM); 56 } 57 58 59 public static boolean getLogStdErr() 60 { 61 return System.err instanceof LogStream; 62 } 63 64 65 69 public static void setLogStdOut(boolean log) 70 { 71 if (log) 72 { 73 if (!(System.out instanceof LogStream)) 74 System.setOut(new LogStream.STDOUT()); 75 } 76 else 77 System.setOut(STDOUT_STREAM); 78 } 79 80 81 public static boolean getLogStdOut() 82 { 83 return System.out instanceof LogStream; 84 } 85 86 87 private String tag; 88 private Log log; 89 private ByteArrayOutputStream2 bout; 90 91 93 public void flush() 94 { 95 super.flush(); 96 if (bout.size()>0) 97 { 98 String s=new String (bout.getBuf(),0,bout.size()).trim(); 99 if (s.length()>0 && log!=null) 100 log.info(tag+": "+s); 101 } 102 bout.reset(); 103 } 104 105 109 public LogStream(String tag, Log log) 110 { 111 super(new ByteArrayOutputStream2(128), true); 112 bout=(ByteArrayOutputStream2)this.out; 113 this.tag=tag; 114 this.log=log; 115 } 116 117 public void close() 118 { 119 flush(); 120 super.close(); 121 } 122 public void println() 123 { 124 super.println(); 125 flush(); 126 } 127 public void println(boolean arg0) 128 { 129 super.println(arg0); 130 flush(); 131 } 132 public void println(char arg0) 133 { 134 super.println(arg0); 135 flush(); 136 } 137 public void println(char[] arg0) 138 { 139 super.println(arg0); 140 flush(); 141 } 142 public void println(double arg0) 143 { 144 super.println(arg0); 145 flush(); 146 } 147 public void println(float arg0) 148 { 149 super.println(arg0); 150 flush(); 151 } 152 public void println(int arg0) 153 { 154 super.println(arg0); 155 flush(); 156 } 157 public void println(long arg0) 158 { 159 super.println(arg0); 160 flush(); 161 } 162 public void println(Object arg0) 163 { 164 super.println(arg0); 165 flush(); 166 } 167 public void println(String arg0) 168 { 169 super.println(arg0); 170 flush(); 171 } 172 public void write(byte[] arg0, int arg1, int arg2) 173 { 174 super.write(arg0, arg1, arg2); 175 flush(); 176 } 177 178 } 179 | Popular Tags |