View Javadoc

1   package org.wcb.common;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   */
20  import java.io.PrintWriter;
21  import java.io.BufferedWriter;
22  import java.io.FileWriter;
23  import java.io.BufferedReader;
24  import java.io.FileReader;
25  import java.io.IOException;
26  import java.io.FileNotFoundException;
27  import java.util.GregorianCalendar;
28  import java.util.Calendar;
29  
30  /***
31   *This writes a log file named x10 and logs commands 
32   *sent to the x10 interface.  If there are system failures
33   *or other problems they should show up here.
34   *
35   * $Log: EventLogger.java,v $
36   * Revision 1.3  2004/07/22 03:17:55  wbogaardt
37   * removed deprecated methods
38   *
39   * Revision 1.2  2003/10/10 22:50:44  wbogaardt
40   * removed error messages and cleaned up format
41   *
42   *
43   */
44  public class EventLogger {
45      private static BufferedWriter logWriter = null;
46      private static String logFile = null;
47      public static final String DEFAULTFILENAME="x10";
48      public static final String FILEHEADER="# This is the x10 events log file\n"+
49              "#It logs events created by the Autohome system for CM11A or CM17 modules\n";
50      private static String userHome=null;
51      private static String userFilename = new String(DEFAULTFILENAME);
52      private static final String SEP = System.getProperty("file.separator");
53  
54      public EventLogger()
55      {
56          this(DEFAULTFILENAME);
57      }
58  
59      public EventLogger(String fileName)
60      {
61          this(null, fileName);
62      }
63  
64      public EventLogger(String dirName, String fileName)
65      {
66          if (null == dirName) {
67              userHome = System.getProperty("user.home");
68          } 
69          else 
70          {
71          	userHome = dirName;
72          }
73          userFilename = fileName;
74      }
75  
76      public String getFileName()
77      {
78          return userFilename;
79      }
80  
81  
82      public synchronized static void writeLog(String logInfo, String logFilename)
83      {
84          if(logInfo ==null )
85              return;
86  
87          Calendar gc = new GregorianCalendar();
88          String currentFileName;
89          int month = gc.get(Calendar.MONTH)+1;
90          int day = gc.get(Calendar.DATE);
91          StringBuffer sbMonth = new StringBuffer();
92          StringBuffer sbDay = new StringBuffer();
93          if(month < 10) sbMonth.append("0");
94          sbMonth.append(month);
95  
96          if(day < 10) sbDay.append("0");
97          sbDay.append(day);
98  
99          currentFileName	= logFilename + "_" + gc.get(Calendar.YEAR) + "" + sbMonth.toString() + "" + sbDay.toString() + ".log";
100         userFilename = userHome + SEP + currentFileName;
101 
102         // If we don't already have a log file for today's date, we'll create a new one.
103         if ( needNewFile( logWriter, logFile, currentFileName ) )
104         {
105             try
106             {
107                 if(logWriter != null)
108                     logWriter.close();
109                 logWriter = null;
110                 logWriter = new BufferedWriter(new FileWriter(userFilename, true));
111             }
112             catch (IOException	e)
113             {
114                 return;
115             }
116         }
117         logFile = currentFileName;
118         String thisLine = stampLine(logInfo, gc);
119         try
120         {
121             logWriter.write(thisLine, 0, thisLine.length());
122             logWriter.newLine();
123             logWriter.flush();
124         }
125         catch(IOException e)
126         {
127         }
128         return;
129 
130     }
131 
132     private static String stampLine(String lineInfo, Calendar c)
133     {
134         int currHour=c.get(Calendar.HOUR_OF_DAY);
135         int currMin=c.get(Calendar.MINUTE);
136         return "["+currHour+":"+currMin+":"+c.get(Calendar.SECOND)+":"+c.get(Calendar.MILLISECOND)+"] "+lineInfo;
137     }
138 
139     public StringBuffer readLog(String theFile)
140     {
141         StringBuffer line=new StringBuffer("");
142         try {
143             BufferedReader br = new BufferedReader(new FileReader(theFile));
144             try
145             {
146                 while(br.readLine()!=null)
147                     line.append(br.readLine()+"\n");
148             }
149             catch (Exception err)
150             {
151                 return null;
152             }
153         }
154         catch(FileNotFoundException ex)
155         {
156             return null;
157         }
158         return line;	 
159     }
160 
161     private static boolean needNewFile( BufferedWriter file, String fileName, String currentFileName )
162     {
163         if(file == null || fileName == null ||
164                 (currentFileName.startsWith(fileName.substring(0,fileName.indexOf("."))) == false))
165             return true;
166         else
167             return false;
168     }
169 
170     public void makeLogFile(String logInfo)
171     {
172         try
173         {
174             PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(userFilename)));
175             out.println(FILEHEADER);
176             GregorianCalendar currDate = new GregorianCalendar();
177             int currHour=currDate.get(Calendar.HOUR_OF_DAY);
178             int currMin=currDate.get(Calendar.MINUTE);
179             out.println("["+currHour+":"+currMin+"]"+logInfo);
180             out.flush();
181             out=null;
182         }
183         catch (Exception e)
184         {
185             e.printStackTrace();
186         }
187     }
188 }
189 
190 
191 
192 
193 
194