1 29 30 package com.caucho.log; 31 32 import com.caucho.loader.EnvironmentLocal; 33 import com.caucho.util.Alarm; 34 import com.caucho.util.QDate; 35 import com.caucho.vfs.StderrStream; 36 import com.caucho.vfs.StdoutStream; 37 import com.caucho.vfs.StreamImpl; 38 import com.caucho.vfs.Vfs; 39 import com.caucho.vfs.WriteStream; 40 41 import java.io.IOException ; 42 import java.io.OutputStream ; 43 import java.io.PrintStream ; 44 45 48 public class EnvironmentStream extends StreamImpl { 49 private final static WriteStream _origSystemOut = Vfs.openWrite(System.out); 51 private final static WriteStream _origSystemErr = Vfs.openWrite(System.err); 53 54 private static PrintStream _systemOut; 56 private static PrintStream _systemErr; 58 59 private static EnvironmentStream _stdoutStream; 61 private static EnvironmentStream _stderrStream; 63 64 private EnvironmentLocal<OutputStream > _environmentStream; 66 67 73 public EnvironmentStream(String envVariable, OutputStream defaultStream) 74 { 75 _environmentStream = new EnvironmentLocal<OutputStream >(envVariable); 76 _environmentStream.setGlobal(defaultStream); 77 } 78 79 84 public EnvironmentStream(OutputStream defaultStream) 85 { 86 _environmentStream = new EnvironmentLocal<OutputStream >(); 87 _environmentStream.setGlobal(defaultStream); 88 } 89 90 93 public String getVariable() 94 { 95 return _environmentStream.getVariable(); 96 } 97 98 101 public OutputStream getGlobalStream() 102 { 103 return (OutputStream ) _environmentStream.getGlobal(); 104 } 105 106 109 public Object setGlobalStream(OutputStream defaultStream) 110 { 111 return _environmentStream.setGlobal(defaultStream); 112 } 113 114 117 public OutputStream getStream() 118 { 119 return (OutputStream ) _environmentStream.get(); 120 } 121 122 125 public Object setStream(OutputStream os) 126 { 127 return _environmentStream.set(os); 128 } 129 130 133 public boolean canWrite() 134 { 135 OutputStream stream = getStream(); 136 137 return stream != null; 138 } 139 140 143 public void write(byte []buf, int offset, int length, boolean isEnd) 144 throws IOException 145 { 146 OutputStream stream = getStream(); 147 148 if (stream == null) 149 return; 150 151 synchronized (stream) { 152 stream.write(buf, offset, length); 153 if (isEnd) 154 stream.flush(); 155 } 156 } 157 158 161 public void flush() 162 throws IOException 163 { 164 OutputStream stream = getStream(); 165 166 if (stream == null) 167 return; 168 169 synchronized (stream) { 170 stream.flush(); 171 } 172 } 173 174 177 public void close() 178 throws IOException 179 { 180 OutputStream stream = getStream(); 181 182 if (stream == null) 183 return; 184 185 synchronized (stream) { 186 stream.flush(); 187 } 188 } 189 190 193 public static void setStdout(OutputStream os) 194 { 195 if (_stdoutStream == null) { 196 OutputStream systemOut = System.out; 197 198 _stdoutStream = new EnvironmentStream("caucho.stdout.stream", 199 systemOut); 200 WriteStream out = new WriteStream(_stdoutStream); 201 out.setDisableClose(true); 202 _systemOut = new PrintStream (out, true); 203 System.setOut(_systemOut); 204 205 if (os == systemOut) 206 return; 207 } 208 209 if (os == _systemErr || os == _systemOut) 210 return; 211 212 if (os instanceof WriteStream) { 213 WriteStream out = (WriteStream) os; 214 215 if (out.getSource() == StdoutStream.create() || 216 out.getSource() == StderrStream.create()) 217 return; 218 } 219 220 _stdoutStream.setStream(os); 221 } 222 223 226 public static EnvironmentStream getStdout() 227 { 228 return _stdoutStream; 229 } 230 231 234 public static WriteStream getOriginalSystemOut() 235 { 236 return _origSystemOut; 237 } 238 239 242 public static void setStderr(OutputStream os) 243 { 244 if (_stderrStream == null) { 245 OutputStream systemErr = System.err; 246 247 _stderrStream = new EnvironmentStream("caucho.stderr.stream", 248 systemErr); 249 WriteStream err = new WriteStream(_stderrStream); 250 err.setDisableClose(true); 251 _systemErr = new PrintStream (err, true); 252 System.setErr(_systemErr); 253 254 if (os == systemErr) 255 return; 256 } 257 258 if (os == _systemErr || os == _systemOut) 259 return; 260 261 if (os instanceof WriteStream) { 262 WriteStream out = (WriteStream) os; 263 264 if (out.getSource() == StdoutStream.create() || 265 out.getSource() == StderrStream.create()) 266 return; 267 } 268 269 _stderrStream.setStream(os); 270 } 271 272 275 public static EnvironmentStream getStderr() 276 { 277 return _stderrStream; 278 } 279 280 283 public static WriteStream getOriginalSystemErr() 284 { 285 return _origSystemErr; 286 } 287 288 292 public static void logStderr(String msg, Throwable e) 293 { 294 try { 295 long now = Alarm.getCurrentTime(); 296 297 msg = QDate.formatLocal(now, "[%Y-%m-%d %H:%M:%S] ") + msg; 298 299 _origSystemErr.println(msg); 300 301 e.printStackTrace(_origSystemErr.getPrintWriter()); 302 303 _origSystemErr.flush(); 304 } catch (Throwable e1) { 305 } 306 } 307 308 312 public static void logStderr(String msg) 313 { 314 try { 315 long now = Alarm.getCurrentTime(); 316 317 msg = QDate.formatLocal(now, "[%Y-%m-%d %H:%M:%S] ") + msg; 318 319 _origSystemErr.println(msg); 320 } catch (Throwable e1) { 321 } 322 } 323 324 static { 325 _origSystemOut.setFlushOnNewline(true); 326 _origSystemErr.setFlushOnNewline(true); 327 } 328 } 329 | Popular Tags |