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 share.LogWriter;
26 
27 import java.io.PrintWriter;
28 import java.text.DecimalFormat;
29 import java.util.Calendar;
30 import java.util.GregorianCalendar;
31 
32 public class SimpleLogWriter extends PrintWriter implements LogWriter {
33 
34     boolean m_bLogging = false;
35     share.DescEntry entry = null;
36     share.Watcher ow = null;
37 
SimpleLogWriter()38     public SimpleLogWriter() {
39         super(System.out);
40         Calendar cal = new GregorianCalendar();
41         DecimalFormat dfmt = new DecimalFormat("00");
42         super.println("LOG> Log started " +
43             dfmt.format(cal.get(Calendar.DAY_OF_MONTH)) + "." +
44             dfmt.format(cal.get(Calendar.MONTH)) + "." +
45             dfmt.format(cal.get(Calendar.YEAR)) + " - " +
46             dfmt.format(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
47             dfmt.format(cal.get(Calendar.MINUTE)) + ":" +
48             dfmt.format(cal.get(Calendar.SECOND)));
49         super.flush();
50     }
51 
initialize(share.DescEntry _entry, boolean _bLogging)52     public boolean initialize(share.DescEntry _entry, boolean _bLogging) {
53         m_bLogging = _bLogging;
54         entry = _entry;
55 
56         return true;
57     }
58 
println(String msg)59     public void println(String msg) {
60         if ((ow == null) && (entry != null))
61         {
62             this.ow = (share.Watcher) entry.UserDefinedParams.get("Watcher");
63             if (this.ow != null)
64             {
65                 this.ow.ping();
66             }
67         }
68         else
69         {
70             if (ow != null)
71             {
72                 this.ow.ping();
73             }
74             else
75             {
76                 // special case: ow == null && entry == null
77                 System.out.println(msg);
78             }
79         }
80 
81         if (m_bLogging) {
82             super.println("LOG> " + msg);
83             super.flush();
84         }
85     // else
86     // {
87     //     super.println(" ++ " + msg);
88     //     super.flush();
89     // }
90     }
91 
summary(share.DescEntry entry)92     public boolean summary(share.DescEntry entry) {
93         return true;
94     }
95 
getWatcher()96     public Object getWatcher() {
97         return this.ow;
98     }
99 
setWatcher(Object watcher)100     public void setWatcher(Object watcher)
101     {
102         if (watcher != null)
103         {
104             entry.UserDefinedParams.put("Watcher", (share.Watcher) watcher);
105         }
106     }
107 }
108