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