Update of /cvsroot/freenet/freenet/src/freenet/support
In directory sc8-pr-cvs1:/tmp/cvs-serv25229/src/freenet/support

Modified Files:
        FileLoggerHook.java 
Log Message:
don't show RT histogram if not using CPAlgoRT. Fix caption on successful inserts page. 
Implement log rotation with optional compression using external native gzip. Logging. 
New diags insertRoutingSuccessRatio, insertNonRoutingSuccessRatio, 
insertFailRoutingNonRoutingRatio. Improved toString() on some objects. Ignore 
QueryRejected.hopsToLive - use hopsToLive/2. Give StoreData a much longer timeout when 
we go to AWSD from an insert.


Index: FileLoggerHook.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/support/FileLoggerHook.java,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- FileLoggerHook.java 20 Aug 2003 00:26:17 -0000      1.27
+++ FileLoggerHook.java 2 Sep 2003 17:05:22 -0000       1.28
@@ -4,6 +4,8 @@
 import java.util.StringTokenizer;
 import java.util.Vector;
 import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Calendar;
 import java.util.LinkedList;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
@@ -26,6 +28,8 @@
     
     private volatile boolean closed = false;
     
+    protected static int INTERVAL = GregorianCalendar.MINUTE;
+    
     private static String uname;
     static {
         try {
@@ -56,6 +60,14 @@
     
     protected OutputStream uout;
     
+    /* Base filename for rotating logs */
+    protected String baseFilename = null;
+    
+    /* Whether to redirect stdout */
+    protected boolean redirectStdOut = false;
+    /* Whether to redirect stderr */
+    protected boolean redirectStdErr = false;
+    
     /**
      * Something wierd happens when the disk gets full, also we don't want to block
      * So run the actual write on another thread
@@ -65,6 +77,7 @@
     
     protected static int MAX_LIST_SIZE=100000;
     protected static long MAX_LIST_BYTES=10*(1<<20);
+    protected static boolean useNativeGzip = false;
     
     // FIXME: should reimplement LinkedList with minimal locking
     
@@ -72,10 +85,42 @@
        MAX_LIST_SIZE = len;
     }
     
+    public static void setUseNativeGzip(boolean x) {
+       useNativeGzip = x;
+    }
+    
     public static void setMaxListBytes(long len) {
        MAX_LIST_BYTES = len;
     }
     
+    public static void setInterval(String intervalName) {
+       if(intervalName.equalsIgnoreCase("MINUTE"))
+           INTERVAL = Calendar.MINUTE;
+       else if(intervalName.equalsIgnoreCase("HOUR"))
+           INTERVAL = Calendar.HOUR;
+       else if(intervalName.equalsIgnoreCase("DAY"))
+           INTERVAL = Calendar.DAY_OF_MONTH;
+       else if(intervalName.equalsIgnoreCase("WEEK"))
+           INTERVAL = Calendar.WEEK_OF_YEAR;
+       else if(intervalName.equalsIgnoreCase("MONTH"))
+           INTERVAL = Calendar.MONTH;
+       else if(intervalName.equalsIgnoreCase("YEAR"))
+           INTERVAL = Calendar.YEAR;
+       else throw new IllegalArgumentException("invalid interval "+intervalName);
+    }
+    
+    protected String getHourLogName(Calendar c) {
+       return baseFilename + "-" + c.get(c.YEAR) + "-" + pad2(c.get(c.MONTH)) +
+           "-" + pad2(c.get(c.DAY_OF_MONTH)) + "-" + pad2(c.get(c.HOUR_OF_DAY)) +
+           (INTERVAL == c.MINUTE ? ("-" + pad2(c.get(c.MINUTE))) : "");
+    }
+    
+    protected String pad2(int x) {
+       String s = Integer.toString(x);
+       if(s.length() == 1) s = "0" + s;
+       return s;
+    }
+    
     class WriterThread extends Thread {
        WriterThread() {
            super("Log File Writer Thread");
@@ -83,8 +128,76 @@
        
        public void run() {
            Object o = null;
+           long thisTime = System.currentTimeMillis();
+           long prevTime;
+           long nextHour = -1;
+           GregorianCalendar gc = null;
+           String filename = null;
+           if(baseFilename != null) {
+               gc = new GregorianCalendar();
+               filename = getHourLogName(gc);
+               uout = openNewLogFile(filename);
+               switch(INTERVAL) {
+               case Calendar.YEAR:
+                   gc.set(Calendar.MONTH, 0);
+               case Calendar.MONTH:
+                   gc.set(Calendar.DAY_OF_MONTH, 0);
+               case Calendar.WEEK_OF_YEAR:
+                   if(INTERVAL == Calendar.WEEK_OF_YEAR)
+                       gc.set(Calendar.DAY_OF_WEEK, 0);
+               case Calendar.DAY_OF_MONTH:
+                   gc.set(Calendar.HOUR, 0);
+               case Calendar.HOUR:
+                   gc.set(Calendar.MINUTE, 0);
+               case Calendar.MINUTE:
+                   gc.set(Calendar.SECOND, 0);
+                   gc.set(Calendar.MILLISECOND, 0);
+               }
+               gc.add(INTERVAL, 1);
+               nextHour = gc.getTimeInMillis();
+               System.err.println("Start time: "+gc);
+           }
+           if(redirectStdOut)
+               System.setOut(lout);
+           if(redirectStdErr)
+               System.setErr(lout);
            while(true) {
                try {
+                   prevTime = thisTime;
+                   thisTime = System.currentTimeMillis();
+                   if(baseFilename != null) {
+                       if(thisTime > nextHour) {
+                           // Switch logs
+                           try {
+                               uout.flush();
+                           } catch (IOException e) {
+                               System.err.println("Flushing on change caught "+e);
+                           }
+                           String oldFilename = filename;
+                           filename = getHourLogName(gc);
+                           OutputStream os = openNewLogFile(filename);
+                           OutputStream oldUout = uout;
+                           uout = os;
+                           if(redirectStdOut)
+                               System.setOut(lout);
+                           if(redirectStdErr)
+                               System.setErr(lout);
+                           try {
+                               oldUout.close();
+                           } catch (IOException e) {
+                               System.err.println("Closing on change caught "+e);
+                           }
+                           System.err.println("Almost rotated");
+                           if(useNativeGzip) {
+                               CompressorThread ct = new 
CompressorThread(oldFilename);
+                               ct.start();
+                               // Don't care about result
+                           } // FIXME: implement a portable default compressor
+                           gc.add(INTERVAL, 1);
+                           nextHour = gc.getTimeInMillis();
+                           System.err.println("Rotated");
+                       }
+                   }
                    if(uout != null) {
                        myWrite(uout, null);
                    } else lout.flush();
@@ -94,6 +207,7 @@
                            try {
                                list.wait(500);
                            } catch (InterruptedException e) {};
+                           if(list.size() == 0) continue;
                        }
                        o = list.removeFirst();
                        listBytes -= (((byte[])o).length+16);
@@ -137,6 +251,76 @@
            }
        }
        
+       protected OutputStream openNewLogFile(String filename) {
+           while(true) {
+               long sleepTime = 1000;
+               try {
+                   System.err.println("Creating new logfile "+filename);
+                   return new FileOutputStream(filename,true);
+               } catch (IOException e) {
+                   System.err.println("Could not create FOS "+filename+": "+e);
+                   System.err.println("Sleeping "+sleepTime/1000+" seconds");
+                   try {
+                       Thread.sleep(sleepTime);
+                   } catch (InterruptedException ex) {};
+                   sleepTime += sleepTime;
+               }
+           }
+       }
+    }
+    
+    protected int runningCompressors = 0;
+    protected Object runningCompressorsSync = new Object();
+    
+    class CompressorThread extends Thread {
+       protected String filename;
+       
+       public CompressorThread(String fn) {
+           super("Logfile compressor thread");
+           this.filename = fn;
+       }
+       
+       public void run() {
+           synchronized(runningCompressorsSync) {
+               runningCompressors++;
+               if(runningCompressors > 1) {
+                   System.err.println("Too many compressors running!");
+                   return;
+               }
+           }
+           try {
+               System.err.println("Starting gzip "+filename);
+               Process r = Runtime.getRuntime().exec(new String[] {
+                   "gzip", filename });
+               System.err.println("Started gzip "+filename);
+               InputStream is = r.getInputStream();
+               InputStream es = r.getErrorStream();
+               while(true) {
+                   byte[] buf = new byte[1024];
+                   try {
+                       is.read(buf, 0, buf.length);
+                   } catch (IOException e) {};
+                   try {
+                       es.read(buf, 0, buf.length);
+                   } catch (IOException e) {};
+                   try {
+                       r.exitValue();
+                       break;
+                   } catch (IllegalThreadStateException e) {}
+               }
+               System.err.println("Finished gzip "+filename);
+               is.close();
+               es.close();
+           } catch (IOException e) {
+               System.err.println("Cannot compress old logfile "+
+                                  filename);
+               e.printStackTrace(System.err);
+           } finally {
+               synchronized(runningCompressorsSync) {
+                   runningCompressors--;
+               }
+           }
+       }
     }
     
     /**
@@ -152,17 +336,21 @@
      * @exception IOException if the file couldn't be opened for append.
      */
     public FileLoggerHook(String filename, String fmt, 
-                          String dfmt, int threshold, boolean assumeWorking)
-       throws IOException {
-        this(new FileOutputStream(filename, true), fmt, dfmt, threshold);
-       
+                         String dfmt, int threshold, boolean assumeWorking)
+       throws IOException {
+       this(false, filename, fmt, dfmt, threshold, assumeWorking);
+    }
+    
+    private void checkStdStreams() {
         // Redirect System.err and System.out to the Logger Printstream
         // if they don't exist (like when running under javaw)
-       if(!assumeWorking) {
-           System.out.print(" \b");
-           if(System.out.checkError()) System.setOut(lout);
-           System.err.print(" \b"); 
-           if(System.err.checkError()) System.setErr(lout);
+       System.out.print(" \b");
+       if(System.out.checkError()) {
+           redirectStdOut = true;
+       }
+       System.err.print(" \b"); 
+       if(System.err.checkError()) {
+           redirectStdErr = true;
        }
     }
     
@@ -182,7 +370,36 @@
      */
     public FileLoggerHook(PrintStream stream, String fmt, String dfmt,
                           int threshold) {
-        lout = stream;
+       this(fmt, dfmt, threshold);
+       lout = stream;
+       WriterThread wt = new WriterThread();
+       //wt.setDaemon(true);
+       CloserThread ct = new CloserThread();
+       Runtime.getRuntime().addShutdownHook(ct);
+       wt.start();
+    }
+    
+    public FileLoggerHook(boolean rotate, String baseFilename, String fmt, 
+                         String dfmt, int threshold, boolean assumeWorking) 
+       throws IOException {
+       this(fmt, dfmt, threshold);
+       if(!assumeWorking)
+           checkStdStreams();
+       if(rotate) {
+           lout = null;
+           this.baseFilename = baseFilename;
+       } else {
+           lout = new PrintStream(new FileOutputStream(baseFilename));
+       }
+       WriterThread wt = new WriterThread();
+       //wt.setDaemon(true);
+       CloserThread ct = new CloserThread();
+       Runtime.getRuntime().addShutdownHook(ct);
+       wt.start();
+    }
+    
+    private FileLoggerHook(String fmt, String dfmt,
+                          int threshold) {
         this.threshold = threshold;
         if (dfmt != null && !dfmt.equals("")) {
             try {
@@ -231,11 +448,6 @@
 
         this.str = new String[strVec.size()];
        str = (String[])strVec.toArray(str);
-       WriterThread wt = new WriterThread();
-       //wt.setDaemon(true);
-       CloserThread ct = new CloserThread();
-       Runtime.getRuntime().addShutdownHook(ct);
-       wt.start();
     }
     
     public void log(Object o, Class c, String msg, Throwable e, int priority){

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to