1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package stats;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.io.PrintWriter;
26cdf0e10cSrcweir import java.io.StringWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /**
29cdf0e10cSrcweir  * Write all logs into a java.io.PrintWriter, i.e. a StringBuffer.
30cdf0e10cSrcweir  * Log is gathered there.
31cdf0e10cSrcweir  */
32cdf0e10cSrcweir public class InternalLogWriter extends PrintWriter
33cdf0e10cSrcweir                                             implements share.LogWriter {
34cdf0e10cSrcweir     /** log active **/
35cdf0e10cSrcweir     boolean active;
36cdf0e10cSrcweir     /** write all output to a StringBuffer **/
37cdf0e10cSrcweir     static StringWriter writer = new StringWriter();
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     /**
40cdf0e10cSrcweir      * c'*tor
41cdf0e10cSrcweir      */
InternalLogWriter()42cdf0e10cSrcweir     public InternalLogWriter() {
43cdf0e10cSrcweir          super(new PrintWriter(writer));
44cdf0e10cSrcweir          active = true;
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /**
48cdf0e10cSrcweir      * Initialization.
49cdf0e10cSrcweir      * @param entry The description entry.
50cdf0e10cSrcweir      * @param active Logging is active.
51cdf0e10cSrcweir      * @return True, if initialize worked.
52cdf0e10cSrcweir      */
initialize(share.DescEntry entry, boolean active)53cdf0e10cSrcweir     public boolean initialize(share.DescEntry entry, boolean active) {
54cdf0e10cSrcweir         this.active = active;
55cdf0e10cSrcweir         return true;
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     /**
59cdf0e10cSrcweir      * Method to print a line that is added to the StringBuffer.
60cdf0e10cSrcweir      * @param msg The message that is printed.
61cdf0e10cSrcweir      */
println(String msg)62cdf0e10cSrcweir     public void println(String msg) {
63cdf0e10cSrcweir         if (active)
64cdf0e10cSrcweir             super.println(msg);
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     /**
68cdf0e10cSrcweir      * Method to print to the StringBuffer.
69cdf0e10cSrcweir      * @param msg The message that is printed.
70cdf0e10cSrcweir      */
print(String msg)71cdf0e10cSrcweir     public void print(String msg) {
72cdf0e10cSrcweir         if (active)
73cdf0e10cSrcweir             super.print(msg);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     /**
78cdf0e10cSrcweir      * Is used to sum up the information.
79cdf0e10cSrcweir      * The summary is also added to the StringBuffer.
80cdf0e10cSrcweir      * @param entry The description entry.
81cdf0e10cSrcweir      * @return True, if a summary could be created.
82cdf0e10cSrcweir      */
summary(share.DescEntry entry)83cdf0e10cSrcweir     public boolean summary(share.DescEntry entry) {
84cdf0e10cSrcweir //        linePrefix = "";
85cdf0e10cSrcweir         String header = "***** State for "+entry.longName+" ******";
86cdf0e10cSrcweir         println(header);
87cdf0e10cSrcweir         if (entry.hasErrorMsg) {
88cdf0e10cSrcweir             println(entry.ErrorMsg);
89cdf0e10cSrcweir             println("Whole "+entry.EntryType+": "+entry.State);
90cdf0e10cSrcweir         } else {
91cdf0e10cSrcweir             println("Whole "+entry.EntryType+": "+entry.State);
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir         for (int i=0;i<header.length();i++) {
94cdf0e10cSrcweir             print("*");
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir         println("");
97cdf0e10cSrcweir         return true;
98cdf0e10cSrcweir     }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     /**
101cdf0e10cSrcweir      * Return all the written stuff.
102cdf0e10cSrcweir      * @return All that was written to the StringBuffer with the
103cdf0e10cSrcweir      * 'println()', 'print()' and 'summarize()' methods.
104cdf0e10cSrcweir      * The StringBuffer is emptied afterwards.
105cdf0e10cSrcweir      **/
getLog()106cdf0e10cSrcweir     public String getLog() {
107cdf0e10cSrcweir         String message = writer.getBuffer().toString();
108cdf0e10cSrcweir         writer = new StringWriter();
109cdf0e10cSrcweir         return message;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
getWatcher()112cdf0e10cSrcweir     public Object getWatcher() {
113cdf0e10cSrcweir         return null;
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
setWatcher(Object watcher)116cdf0e10cSrcweir     public void setWatcher(Object watcher) {
117cdf0e10cSrcweir     }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121