1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package stats; 29 30 import java.io.FileWriter; 31 import java.io.IOException; 32 import share.LogWriter; 33 34 import java.io.PrintWriter; 35 import java.text.DecimalFormat; 36 import java.util.Calendar; 37 import java.util.GregorianCalendar; 38 import java.util.HashMap; 39 import java.util.Iterator; 40 41 public class FileLogWriter extends PrintWriter implements LogWriter { 42 43 44 HashMap mFileWriters = null; 45 boolean logging = false; 46 share.DescEntry entry = null; 47 share.Watcher ow = null; 48 49 public FileLogWriter() { 50 super(System.out); 51 Calendar cal = new GregorianCalendar(); 52 DecimalFormat dfmt = new DecimalFormat("00"); 53 super.println("LOG> Log started " + 54 dfmt.format(cal.get(Calendar.DAY_OF_MONTH)) + "." + 55 dfmt.format(cal.get(Calendar.MONTH)) + "." + 56 dfmt.format(cal.get(Calendar.YEAR)) + " - " + 57 dfmt.format(cal.get(Calendar.HOUR_OF_DAY)) + ":" + 58 dfmt.format(cal.get(Calendar.MINUTE)) + ":" + 59 dfmt.format(cal.get(Calendar.SECOND))); 60 super.flush(); 61 } 62 63 public boolean initialize(share.DescEntry entry, boolean logging) { 64 this.logging = logging; 65 this.entry = entry; 66 return true; 67 } 68 69 70 public void addFileLog(String filePath){ 71 try{ 72 if(mFileWriters == null) 73 mFileWriters = new HashMap(); 74 mFileWriters.put(filePath, new FileWriter(filePath)); 75 }catch(IOException e ){ 76 e.printStackTrace(this); 77 } 78 } 79 80 81 public void removeFileLog(String filePath){ 82 if(filePath != null) 83 mFileWriters.remove(filePath); 84 } 85 86 87 public void println(String msg) { 88 89 this.ow = (share.Watcher) entry.UserDefinedParams.get("Watcher"); 90 91 if (ow != null) { 92 ow.ping(); 93 } 94 if (logging) { 95 96 // logoutput to console 97 super.println("LOG> "+msg); 98 super.flush(); 99 100 //logoutput to file 101 if(mFileWriters != null && mFileWriters.size() > 0){ 102 try{ 103 FileWriter fw = null; 104 Iterator iter = mFileWriters.values().iterator(); 105 while(iter.hasNext()){ 106 fw = (FileWriter) iter.next(); 107 fw.write("LOG> " + msg + "\n"); 108 fw.flush(); 109 } 110 }catch(IOException e ){ 111 e.printStackTrace(this); 112 } 113 } 114 } 115 } 116 117 public boolean summary(share.DescEntry entry) { 118 String header = "***** State for "+entry.longName+" ******"; 119 System.out.println(header); 120 if (entry.hasErrorMsg) { 121 System.out.println(entry.ErrorMsg); 122 System.out.println("Whole "+entry.EntryType+": "+entry.State); 123 } else { 124 System.out.println("Whole "+entry.EntryType+": "+entry.State); 125 } 126 for (int i=0;i<header.length();i++) { 127 System.out.print("*"); 128 } 129 System.out.println(""); 130 return true; 131 } 132 133 public Object getWatcher() { 134 return this.ow; 135 } 136 137 public void setWatcher(Object watcher) { 138 entry.UserDefinedParams.put("Watcher", (share.Watcher) watcher); 139 } 140 141 } 142