| 1 19 package freecs.util.logger; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.nio.channels.WritableByteChannel ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Calendar ; 29 import java.util.zip.GZIPOutputStream ; 30 31 import freecs.Server; 32 33 34 41 public class LogFile implements LogDestination { 42 private static SimpleDateFormat date = new SimpleDateFormat ("yyyyMMdd"); 43 44 final String path; 45 FileOutputStream fos; 46 WritableByteChannel fc; 47 int logFileDay; 48 49 50 LogFile (String path) { 51 this.path = path; 52 } 53 54 public WritableByteChannel getChannel() throws FileNotFoundException , IOException { 55 if (fc == null) { 56 fc = createNewLogFile(); 57 return fc; 58 } 59 LogWriter.instance.cal.setTimeInMillis(System.currentTimeMillis()); 60 if (logFileDay==LogWriter.instance.cal.get(Calendar.DAY_OF_MONTH)) 61 return fc; 62 backupLogFile(); 63 fc = createNewLogFile(); 64 return fc; 65 } 66 67 private WritableByteChannel createNewLogFile () throws FileNotFoundException , IOException { 68 LogWriter.instance.cal.setTimeInMillis(System.currentTimeMillis()); 69 File f = new File (path); 70 if (f.exists()) { 71 LogWriter.instance.cal.setTimeInMillis(f.lastModified()); 72 if (logFileDay!=LogWriter.instance.cal.get(Calendar.DAY_OF_MONTH)) 73 backupLogFile(); 74 f = new File (path); 75 } 76 logFileDay = LogWriter.instance.cal.get(Calendar.DAY_OF_MONTH); 77 fos = new FileOutputStream (f); 78 return fos.getChannel(); 79 } 80 81 private void backupLogFile() throws IOException { 82 LogWriter.instance.cal.setTimeInMillis(System.currentTimeMillis()); 83 String backupPath; 84 if (path.indexOf(".")> -1) 85 backupPath = path.substring(0, path.lastIndexOf(".")) 86 + "_" + date.format(LogWriter.instance.cal.getTime()) 87 + path.substring(path.lastIndexOf(".")); 88 else 89 backupPath = path + "_" + date.format(LogWriter.instance.cal.getTime()); 90 if (fos != null) { 91 fos.flush(); 92 if (fc != null) 93 fc.close(); 94 fos.close(); 95 } 96 File f = new File (path); 97 File backupFile = new File (backupPath); 98 f.renameTo(backupFile); 99 new LogFileShrinker(backupFile).start(); 100 } 101 102 public static void main (String arg[]) { 103 File f = new File (arg[0]); 104 LogFileShrinker lfs = new LogFileShrinker(f); 105 lfs.start(); 106 System.out.println("Start"); 107 try { 108 lfs.join(); 109 } catch (Exception e) { } 110 System.out.println("DONE"); 111 } 112 113 114 117 static class LogFileShrinker extends Thread { 118 final File toZip; 119 120 LogFileShrinker (File f) { 121 toZip = f; 122 } 123 124 public void run() { 125 String originalFile = toZip.getAbsolutePath(); 126 String compressedFile = originalFile + ".gz"; 127 FileInputStream fis = null; 128 GZIPOutputStream gzos = null; 129 FileOutputStream fos = null; 130 try { 131 try { 132 fis = new FileInputStream (toZip); 133 } catch (IOException ioe) { 134 Server.debug ("LogFileShrinker", "Unable to open uncompressed file " + originalFile, ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 135 return; 136 } 137 138 try { 139 File zipped = new File (toZip.getAbsolutePath() + ".gz"); 140 fos = new FileOutputStream (zipped); 141 } catch (IOException ioe) { 142 Server.debug ("LogFileShrinker", "Unable to open zip file-destination " + compressedFile, ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 143 return; 144 } 145 try { 146 gzos = new GZIPOutputStream (fos); 147 int read; 148 while ((read = fis.read()) > -1) 149 gzos.write(read); 150 gzos.close(); 151 gzos=null; 152 fos.close(); 153 fos=null; 154 fis.close(); 155 fis=null; 156 if (!toZip.delete()) 157 Server.log("LogFileShrinker", "Unable to delete original file " + originalFile, Server.MSG_ERROR, Server.LVL_MAJOR); 158 } catch (IOException ioe) { 159 Server.debug ("LogFileShrinker", "Unable to compress file " + originalFile + " to " + compressedFile, ioe, Server.MSG_ERROR, Server.LVL_MAJOR); 160 } 161 } finally { 162 if (fis != null) try { 163 fis.close(); 164 } catch (Exception e) { 165 } 167 if (gzos != null) try { 168 gzos.close(); 169 } catch (Exception e) { } 170 if (fos != null) try { 171 fos.close(); 172 } catch (Exception e) { } 173 } 174 } 175 } 176 } | Popular Tags |