00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00094
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 }