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 
24 package stats;
25 
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import share.LogWriter;
29 
30 import java.io.PrintWriter;
31 import java.text.DecimalFormat;
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 
37 public class FileLogWriter extends PrintWriter implements LogWriter {
38 
39 
40     HashMap mFileWriters = null;
41     boolean logging = false;
42     share.DescEntry entry = null;
43     share.Watcher ow = null;
44 
FileLogWriter()45     public FileLogWriter() {
46         super(System.out);
47         Calendar cal = new GregorianCalendar();
48         DecimalFormat dfmt = new DecimalFormat("00");
49         super.println("LOG> Log started " +
50                     dfmt.format(cal.get(Calendar.DAY_OF_MONTH)) + "." +
51                     dfmt.format(cal.get(Calendar.MONTH)) + "." +
52                     dfmt.format(cal.get(Calendar.YEAR)) + " - " +
53                     dfmt.format(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
54                     dfmt.format(cal.get(Calendar.MINUTE)) + ":" +
55                     dfmt.format(cal.get(Calendar.SECOND)));
56         super.flush();
57     }
58 
initialize(share.DescEntry entry, boolean logging)59     public boolean initialize(share.DescEntry entry, boolean logging) {
60          this.logging = logging;
61          this.entry = entry;
62          return true;
63     }
64 
65 
addFileLog(String filePath)66     public void addFileLog(String filePath){
67         try{
68             if(mFileWriters == null)
69                 mFileWriters = new HashMap();
70             mFileWriters.put(filePath, new FileWriter(filePath));
71         }catch(IOException e ){
72             e.printStackTrace(this);
73         }
74     }
75 
76 
removeFileLog(String filePath)77     public void removeFileLog(String filePath){
78         if(filePath != null)
79             mFileWriters.remove(filePath);
80     }
81 
82 
println(String msg)83     public void println(String msg) {
84 
85         this.ow = (share.Watcher) entry.UserDefinedParams.get("Watcher");
86 
87         if (ow != null) {
88             ow.ping();
89         }
90         if (logging) {
91 
92             // logoutput to console
93             super.println("LOG> "+msg);
94             super.flush();
95 
96             //logoutput to file
97             if(mFileWriters != null && mFileWriters.size() > 0){
98                 try{
99                     FileWriter fw = null;
100                     Iterator iter = mFileWriters.values().iterator();
101                     while(iter.hasNext()){
102                         fw = (FileWriter) iter.next();
103                         fw.write("LOG> " + msg + "\n");
104                         fw.flush();
105                     }
106                 }catch(IOException e ){
107                     e.printStackTrace(this);
108                 }
109             }
110         }
111     }
112 
summary(share.DescEntry entry)113     public boolean summary(share.DescEntry entry) {
114         String header = "***** State for "+entry.longName+" ******";
115         System.out.println(header);
116         if (entry.hasErrorMsg) {
117             System.out.println(entry.ErrorMsg);
118             System.out.println("Whole "+entry.EntryType+": "+entry.State);
119         } else {
120             System.out.println("Whole "+entry.EntryType+": "+entry.State);
121         }
122         for (int i=0;i<header.length();i++) {
123             System.out.print("*");
124         }
125         System.out.println("");
126         return true;
127     }
128 
getWatcher()129     public Object getWatcher() {
130         return this.ow;
131     }
132 
setWatcher(Object watcher)133     public void setWatcher(Object watcher) {
134         entry.UserDefinedParams.put("Watcher", (share.Watcher) watcher);
135     }
136 
137 }
138