Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FileOpenCloseLogTracer.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.common.util.log;
00029 
00030 import java.util.Properties;
00031 import java.io.*;
00032 
00041 public class FileOpenCloseLogTracer extends DefaultLogTracer {
00042 
00043   protected FileOutputStream fileStream;
00044   protected String logFile;
00045 
00046   public FileOpenCloseLogTracer() {
00047   }
00054   public void setLogTracerProperties(Properties props)  {
00055     try {
00056       logFile = props.getProperty("org.openmobileis.common.log.file");
00057       if (logFile == null)  {
00058         System.out.println("LOGFILE property is not defined");
00059         System.out.println("LOG to console");
00060         outStream = System.out;
00061         return;
00062       }
00063       File logfile = new File(logFile);
00064       if (logfile.exists()) {
00065         String archivedir = props.getProperty("org.openmobileis.common.log.archivefile");
00066         if (archivedir == null) {
00067           System.out.println("LOGDIRARCHIVE property is not defined. Old log file is not archived");
00068         } else {
00069           int index = logFile.lastIndexOf(File.separator);
00070           String fileName = logFile;
00071           if (index !=-1) {
00072             fileName = logFile.substring(index+1);
00073           }
00074           java.text.SimpleDateFormat formatter
00075             = new java.text.SimpleDateFormat ("dd-MM-yy#hh-mm-ss", java.util.Locale.FRANCE);
00076           String dateString = formatter.format(new java.util.Date());
00077           String logArchiveFile = archivedir+dateString+"-"+fileName;
00078           FileInputStream input  = new FileInputStream(logFile);
00079           FileOutputStream output = new FileOutputStream(logArchiveFile);
00080           try {
00081             byte[] buf = new byte[4096];
00082             int len = 0;
00083             while ( ( len = input.read( buf ) ) != -1 )
00084               output.write( buf, 0, len );
00085             output.flush();
00086           } finally {
00087             input.close();
00088             output.close();
00089           }
00090         }
00091       }
00092       outStream = System.out;
00093 //      System.setOut(outStream);
00094 //      System.setErr(outStream);
00095     } catch (Exception e) {
00096       System.out.println("Error in FileTraceManager Init" +e);
00097     }
00098   }
00099 
00103         public synchronized void trace(int service, int priority, String message)  {
00104                 try     {
00105                         fileStream = new FileOutputStream(logFile, true);
00106                         try     {
00107                                 outStream = new PrintStream(fileStream, true);
00108                                 super.trace(service, priority, message);
00109                         } finally       {
00110                                 outStream = System.out;
00111                                 fileStream.flush();
00112                                 fileStream.close();
00113                         }
00114                 } catch (Throwable ex)  {
00115                         ex.printStackTrace();
00116                 }
00117 
00118         }
00119   
00120   public File getLogFile()      {
00121     return  new File(logFile);
00122   }
00123 
00124 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4