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 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
00092
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 }