1   package org.apache.velocity.test.misc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.PrintStream;
24  import org.apache.velocity.runtime.RuntimeServices;
25  import org.apache.velocity.runtime.log.SystemLogChute;
26  
27  /**
28   * LogChute implementation that can easily capture output
29   * or suppress it entirely.  By default, both capture and suppress
30   * are on. To have this behave like a normal SystemLogChute,
31   * you must turn it on() and stopCapture().
32   *
33   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
34   * @author Nathan Bubna
35   * @version $Id: TestLogChute.java 749684 2009-03-03 18:38:16Z nbubna $
36   */
37  public class TestLogChute extends SystemLogChute
38  {
39      public static final String TEST_LOGGER_LEVEL = "runtime.log.logsystem.test.level";
40  
41      private ByteArrayOutputStream log;
42      private PrintStream systemDotIn;
43      private boolean suppress = true;
44      private boolean capture = true;
45  
46      public TestLogChute()
47      {
48          log = new ByteArrayOutputStream();
49          systemDotIn = new PrintStream(log, true);
50      }
51  
52      public TestLogChute(boolean suppress, boolean capture)
53      {
54          this();
55          this.suppress = suppress;
56          this.capture = capture;
57      }
58      
59      public void init(RuntimeServices rs)
60          throws Exception
61      {
62          super.init(rs);
63  
64          String level = rs.getString(TEST_LOGGER_LEVEL);
65          if (level != null)
66          {
67              setEnabledLevel(toLevel(level));
68          }
69      }
70  
71      public void on()
72      {
73          suppress = false;
74      }
75  
76      public void off()
77      {
78          suppress = true;
79      }
80  
81      public void startCapture()
82      {
83          capture = true;
84      }
85  
86      public void stopCapture()
87      {
88          capture = false;
89      }
90  
91      public boolean isLevelEnabled(int level)
92      {
93          return (!suppress || capture) && super.isLevelEnabled(level);
94      }
95              
96  
97      protected void write(PrintStream ps, String prefix, String message, Throwable t)
98      {
99          if (capture)
100         {
101             super.write(systemDotIn, prefix, message, t);
102         }
103         else
104         {
105             super.write(ps, prefix, message, t);
106         }
107     }
108 
109     /**
110      * Return the captured log messages to date.
111      * @return log messages
112      */
113     public String getLog()
114     {
115         return log.toString();
116     }
117 
118 }