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 697214 2008-09-19 19:55:59Z 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) throws Exception
60 {
61 super.init(rs);
62
63 String level = rs.getString(TEST_LOGGER_LEVEL);
64 if (level != null)
65 {
66 setEnabledLevel(toLevel(level));
67 }
68 }
69
70 public void on()
71 {
72 suppress = false;
73 }
74
75 public void off()
76 {
77 suppress = true;
78 }
79
80 public void startCapture()
81 {
82 capture = true;
83 }
84
85 public void stopCapture()
86 {
87 capture = false;
88 }
89
90 public boolean isLevelEnabled(int level)
91 {
92 return !suppress && super.isLevelEnabled(level);
93 }
94
95
96 protected void write(PrintStream ps, String prefix, String message, Throwable t)
97 {
98 if (capture)
99 {
100 super.write(systemDotIn, prefix, message, t);
101 }
102 else
103 {
104 super.write(ps, prefix, message, t);
105 }
106 }
107
108 /**
109 * Return the captured log messages to date.
110 * @return log messages
111 */
112 public String getLog()
113 {
114 return log.toString();
115 }
116
117 }