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 
00031 import org.openmobileis.common.util.PropertiesManager;
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       PropertiesManager.getManager().addProperty("org.openmobileis.common.log.file", logFile);
00064       outStream = System.out;
00065 //      System.setOut(outStream);
00066 //      System.setErr(outStream);
00067     } catch (Exception e) {
00068       System.out.println("Error in FileTraceManager Init" +e);
00069     }
00070   }
00071 
00075         public synchronized void trace(int service, int priority, String message)  {
00076                 try     {
00077                         fileStream = new FileOutputStream(logFile, true);
00078                         try     {
00079                                 outStream = new PrintStream(fileStream, true);
00080                                 super.trace(service, priority, message);
00081                         } finally       {
00082                                 outStream = System.out;
00083                                 fileStream.flush();
00084                                 fileStream.close();
00085                         }
00086                 } catch (Throwable ex)  {
00087                         ex.printStackTrace();
00088                 }
00089 
00090         }
00091   
00092   public File getLogFile()      {
00093     return  new File(logFile);
00094   }
00095 
00096 }

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