1 17 18 package org.apache.tomcat.util.log; 19 20 import java.io.IOException ; 21 import java.io.PrintStream ; 22 import java.util.EmptyStackException ; 23 import java.util.Stack ; 24 25 35 public class SystemLogHandler extends PrintStream { 36 37 38 40 41 44 public SystemLogHandler(PrintStream wrapped) { 45 super(wrapped); 46 out = wrapped; 47 } 48 49 50 52 53 56 protected PrintStream out = null; 57 58 59 62 protected static ThreadLocal logs = new ThreadLocal (); 63 64 65 68 protected static Stack reuse = new Stack (); 69 70 71 73 74 77 public static void startCapture() { 78 CaptureLog log = null; 79 if (!reuse.isEmpty()) { 80 try { 81 log = (CaptureLog)reuse.pop(); 82 } catch (EmptyStackException e) { 83 log = new CaptureLog(); 84 } 85 } else { 86 log = new CaptureLog(); 87 } 88 Stack stack = (Stack )logs.get(); 89 if (stack == null) { 90 stack = new Stack (); 91 logs.set(stack); 92 } 93 stack.push(log); 94 } 95 96 97 100 public static String stopCapture() { 101 Stack stack = (Stack )logs.get(); 102 if (stack == null || stack.isEmpty()) { 103 return null; 104 } 105 CaptureLog log = (CaptureLog)stack.pop(); 106 if (log == null) { 107 return null; 108 } 109 String capture = log.getCapture(); 110 log.reset(); 111 reuse.push(log); 112 return capture; 113 } 114 115 116 118 119 122 protected PrintStream findStream() { 123 Stack stack = (Stack )logs.get(); 124 if (stack != null && !stack.isEmpty()) { 125 CaptureLog log = (CaptureLog)stack.peek(); 126 if (log != null) { 127 PrintStream ps = log.getStream(); 128 if (ps != null) { 129 return ps; 130 } 131 } 132 } 133 return out; 134 } 135 136 137 139 140 public void flush() { 141 findStream().flush(); 142 } 143 144 public void close() { 145 findStream().close(); 146 } 147 148 public boolean checkError() { 149 return findStream().checkError(); 150 } 151 152 protected void setError() { 153 } 155 156 public void write(int b) { 157 findStream().write(b); 158 } 159 160 public void write(byte[] b) 161 throws IOException { 162 findStream().write(b); 163 } 164 165 public void write(byte[] buf, int off, int len) { 166 findStream().write(buf, off, len); 167 } 168 169 public void print(boolean b) { 170 findStream().print(b); 171 } 172 173 public void print(char c) { 174 findStream().print(c); 175 } 176 177 public void print(int i) { 178 findStream().print(i); 179 } 180 181 public void print(long l) { 182 findStream().print(l); 183 } 184 185 public void print(float f) { 186 findStream().print(f); 187 } 188 189 public void print(double d) { 190 findStream().print(d); 191 } 192 193 public void print(char[] s) { 194 findStream().print(s); 195 } 196 197 public void print(String s) { 198 findStream().print(s); 199 } 200 201 public void print(Object obj) { 202 findStream().print(obj); 203 } 204 205 public void println() { 206 findStream().println(); 207 } 208 209 public void println(boolean x) { 210 findStream().println(x); 211 } 212 213 public void println(char x) { 214 findStream().println(x); 215 } 216 217 public void println(int x) { 218 findStream().println(x); 219 } 220 221 public void println(long x) { 222 findStream().println(x); 223 } 224 225 public void println(float x) { 226 findStream().println(x); 227 } 228 229 public void println(double x) { 230 findStream().println(x); 231 } 232 233 public void println(char[] x) { 234 findStream().println(x); 235 } 236 237 public void println(String x) { 238 findStream().println(x); 239 } 240 241 public void println(Object x) { 242 findStream().println(x); 243 } 244 245 } 246 | Popular Tags |