FileLogTracer.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  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 
00030 package org.openmobileis.common.util.log;
00031 
00032 import java.util.Properties;
00033 import java.io.*;
00034 
00042 public class FileLogTracer extends DefaultLogTracer {
00043 
00044   protected FileOutputStream fileStream;
00045   protected String logFile;
00046 
00047   public FileLogTracer() {
00048   }
00049   
00055   public void setLogTracerProperties(Properties props)  {
00056     try {
00057       logFile = props.getProperty("org.openmobileis.common.log.file");
00058       if (logFile == null)  {
00059         System.out.println("org.openmobileis.common.log.file property is not defined");
00060         System.out.println("LOG to console");
00061         outStream = System.out;
00062         return;
00063       }
00064       File logfile = new File(logFile);
00065       if (logfile.exists()) {
00066         String archivedir = props.getProperty("org.openmobileis.common.log.archivefile");
00067         if (archivedir != null) {
00068           int index = logFile.lastIndexOf(File.separator);
00069           String fileName = logFile;
00070           if (index !=-1) {
00071             fileName = logFile.substring(index+1);
00072           }
00073           java.text.SimpleDateFormat formatter
00074             = new java.text.SimpleDateFormat ("dd-MM-yy#hh-mm-ss", java.util.Locale.FRANCE);
00075           String dateString = formatter.format(new java.util.Date());
00076           String logArchiveFile = archivedir+dateString+"-"+fileName;
00077           FileInputStream input  = new FileInputStream(logFile);
00078           FileOutputStream output = new FileOutputStream(logArchiveFile);
00079           try {
00080             byte[] buf = new byte[4096];
00081             int len = 0;
00082             while ( ( len = input.read( buf ) ) != -1 )
00083               output.write( buf, 0, len );
00084             output.flush();
00085           } finally {
00086             input.close();
00087             output.close();
00088           }
00089         }
00090       }
00091       fileStream = new FileOutputStream(logFile);
00092       outStream = new PrintStream(fileStream, true);
00093       System.setOut(outStream);
00094       System.setErr(outStream);
00095     } catch (Exception e) {
00096       System.out.println("Error in FileTraceManager Init" +e);
00097     }
00098   }
00099 
00100 }

Generated on Mon Dec 4 11:03:26 2006 for OpenMobileIS by  doxygen 1.5.1-p1