Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » log » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   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   /**
   26    * This helper class may be used to do sophisticated redirection of 
   27    * System.out and System.err on a per Thread basis.
   28    * 
   29    * A stack is implemented per Thread so that nested startCapture
   30    * and stopCapture can be used.
   31    *
   32    * @author Remy Maucherat
   33    * @author Glenn L. Nielsen
   34    */
   35   public class SystemLogHandler extends PrintStream {
   36   
   37   
   38       // ----------------------------------------------------------- Constructors
   39   
   40   
   41       /**
   42        * Construct the handler to capture the output of the given steam.
   43        */
   44       public SystemLogHandler(PrintStream wrapped) {
   45           super(wrapped);
   46           out = wrapped;
   47       }
   48   
   49   
   50       // ----------------------------------------------------- Instance Variables
   51   
   52   
   53       /**
   54        * Wrapped PrintStream.
   55        */
   56       protected PrintStream out = null;
   57   
   58   
   59       /**
   60        * Thread <-> CaptureLog associations.
   61        */
   62       protected static ThreadLocal logs = new ThreadLocal();
   63   
   64   
   65       /**
   66        * Spare CaptureLog ready for reuse.
   67        */
   68       protected static Stack reuse = new Stack();
   69   
   70   
   71       // --------------------------------------------------------- Public Methods
   72   
   73   
   74       /**
   75        * Start capturing thread's output.
   76        */
   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       /**
   98        * Stop capturing thread's output and return captured data as a String.
   99        */
  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       // ------------------------------------------------------ Protected Methods
  117   
  118   
  119       /**
  120        * Find PrintStream to which the output must be written to.
  121        */
  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       // ---------------------------------------------------- PrintStream Methods
  138   
  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           //findStream().setError();
  154       }
  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   }

Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » log » [javadoc | source]