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

FileOpenCloseLogTracer.java

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

Generated on Mon Jul 10 10:29:29 2006 for OpenMobileIS by  doxygen 1.4.4