Author: alexlehm
Date: 2007-12-06 21:59:03 +0000 (Thu, 06 Dec 2007)
New Revision: 16375

Modified:
   trunk/apps/Freemail/src/freemail/InboundContact.java
   trunk/apps/Freemail/src/freemail/utils/Logger.java
Log:
improved Logger class to use the one from Freenet if available (e.g. running as 
plugin) or log to stdout if not.
This should work as long as we do not use the freenet logger class directly.



Modified: trunk/apps/Freemail/src/freemail/InboundContact.java
===================================================================
--- trunk/apps/Freemail/src/freemail/InboundContact.java        2007-12-06 
21:47:50 UTC (rev 16374)
+++ trunk/apps/Freemail/src/freemail/InboundContact.java        2007-12-06 
21:59:03 UTC (rev 16375)
@@ -95,8 +95,7 @@
                        // a fix for the bug causing this 
(https://bugs.freenetproject.org/view.php?id=1087) was committed on Feb 4 2007,
                        // anybody who has started using Freemail after that 
date is not affected.
                        if(slot.length()!=52) {
-                               Logger.normal(this,"ignoring malformed slot 
"+slot+" (probably due to previous bug)");
-                               Logger.normal(this,"please the fix the entry in 
"+this.ibct_dir);
+                               Logger.normal(this,"Ignoring malformed slot 
"+slot+" (probably due to previous bug). Please the fix the entry in 
"+this.ibct_dir);
                                break;
                        }
                        String key = basekey+slot;

Modified: trunk/apps/Freemail/src/freemail/utils/Logger.java
===================================================================
--- trunk/apps/Freemail/src/freemail/utils/Logger.java  2007-12-06 21:47:50 UTC 
(rev 16374)
+++ trunk/apps/Freemail/src/freemail/utils/Logger.java  2007-12-06 21:59:03 UTC 
(rev 16375)
@@ -3,13 +3,14 @@
  *
  * this is a first attempt at fixing the logging so that not everything
  * is written to stdout. This class attempts to mimic the Logger class
- * from Freenet, later we can probably use the class from Freenet without
- * changing much except the import statement.
+ * from Freenet and calls the Freenet Logger class is available.
  *
  */

 package freemail.utils;

+import java.lang.NoClassDefFoundError;
+
 public class Logger {

        static final private int INTERNAL=1;
@@ -18,10 +19,28 @@
        static final private int NORMAL=8;
        static final private int ERROR=16;

+       static boolean initialized=false;
+       static boolean foundFreenetLogger=false;
+       
        //static final private int loglevel=INTERNAL|DEBUG|MINOR|NORMAL|ERROR; 
// everything
        //static final private int loglevel=DEBUG|NORMAL|ERROR;
        static final private int loglevel=NORMAL|ERROR; // should be ok for 
normal users

+       static private boolean useFreenetLogger()
+       {
+               if(!initialized) {
+                       try {
+                               freenet.support.Logger.shouldLog(0, null);
+                               foundFreenetLogger=true;
+                       }
+                       catch(NoClassDefFoundError ex) {
+                               foundFreenetLogger=false;
+                       }
+                       initialized=true;
+               }
+               return foundFreenetLogger;
+       }
+       
        static private void log(int l, Object o, String s, String level) {
                if((l&loglevel)!=0) {
                        System.out.println(level+"("+o.getClass().getName()+"): 
"+s);
@@ -35,35 +54,67 @@
        }

        static public void minor(Object o, String s) {
-               log(MINOR,o,s,"MINOR");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.minor(o,s);
+               } else {
+                       log(MINOR,o,s,"MINOR");
+               }
        }

        static public void minor(Class c, String s) {
-               log(MINOR,c,s,"MINOR");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.minor(c,s);
+               } else {
+                       log(MINOR,c,s,"MINOR");
+               }
        }

        static public void normal(Object o, String s) {
-               log(NORMAL,o,s,"NORMAL");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.normal(o,s);
+               } else {
+                       log(NORMAL,o,s,"NORMAL");
+               }
        }

        static public void normal(Class c, String s) {
-               log(NORMAL,c,s,"NORMAL");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.normal(c,s);
+               } else {
+                       log(NORMAL,c,s,"NORMAL");
+               }
        }

        static public void error(Object o, String s) {
-               log(ERROR,o,s,"ERROR");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.error(o,s);
+               } else {
+                       log(ERROR,o,s,"ERROR");
+               }
        }

        static public void error(Class c, String s) {
-               log(ERROR,c,s,"ERROR");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.error(c,s);
+               } else {
+                       log(ERROR,c,s,"ERROR");
+               }
        }

        static public void debug(Object o, String s) {
-               log(DEBUG,o,s,"DEBUG");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.debug(o,s);
+               } else {
+                       log(DEBUG,o,s,"DEBUG");
+               }
        }

        static public void debug(Class c, String s) {
-               log(DEBUG,c,s,"DEBUG");
+               if(useFreenetLogger()) {
+                       freenet.support.Logger.debug(c,s);
+               } else {
+                       log(DEBUG,c,s,"DEBUG");
+               }
        }

 }


Reply via email to