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