]> source.dussan.org Git - aspectj.git/commitdiff
Bug 150487 "Tracing and Logging Framework" (support debug, info, warn, error and...
authormwebster <mwebster>
Wed, 9 Aug 2006 15:53:53 +0000 (15:53 +0000)
committermwebster <mwebster>
Wed, 9 Aug 2006 15:53:53 +0000 (15:53 +0000)
weaver/src/org/aspectj/weaver/tools/AbstractTrace.java
weaver/src/org/aspectj/weaver/tools/CommonsTrace.java
weaver/src/org/aspectj/weaver/tools/DefaultTrace.java
weaver/src/org/aspectj/weaver/tools/Trace.java
weaver/testsrc/org/aspectj/weaver/AbstractTraceTest.java [new file with mode: 0644]
weaver/testsrc/org/aspectj/weaver/CommonsTraceTest.java
weaver/testsrc/org/aspectj/weaver/DefaultTraceTest.java
weaver5/java5-src/org/aspectj/weaver/tools/Jdk14Trace.java
weaver5/java5-testsrc/org/aspectj/weaver/Jdk14TraceTest.java

index 4b421f2aeb18fccaabcdeb63af75366022c2d4d0..0ca7425033c9008d3dbe86e474e1f67c0aea22c2 100644 (file)
@@ -32,44 +32,12 @@ public abstract class AbstractTrace implements Trace {
 
        public abstract void exit(String methodName, Throwable th);
 
-       public void error(String message) {
-               // TODO Auto-generated method stub
-
-       }
-
-       public void error(String message, Throwable th) {
-               // TODO Auto-generated method stub
-
-       }
-
        public void event(String methodName) {
-               // TODO Auto-generated method stub
-
+               throw new UnsupportedOperationException("event");
        }
 
        public void event(String methodName, Object thiz, Object[] args) {
-               // TODO Auto-generated method stub
-
-       }
-
-       public void exit(String methodName) {
-               // TODO Auto-generated method stub
-
-       }
-
-       public void info(String message) {
-               // TODO Auto-generated method stub
-
-       }
-
-       public void warn(String message) {
-               // TODO Auto-generated method stub
-
-       }
-
-       public void warn(String message, Throwable th) {
-               // TODO Auto-generated method stub
-
+               throw new UnsupportedOperationException("event");
        }
        
        /*
@@ -90,7 +58,22 @@ public abstract class AbstractTrace implements Trace {
        public void exit (String methodName, boolean b) {
                exit(methodName,new Boolean(b));
        }
+
+       public void warn(String message) {
+               warn(message,null);
+       }
+
+       public void error(String message) {
+               error(message,null);
+       }
+
+       public void fatal (String message) {
+               fatal(message,null);
+       }
        
+       /*
+        * Formatting
+        */
        protected String formatMessage(String kind, String className, String methodName, Object thiz, Object[] args) {
                StringBuffer message = new StringBuffer();
                Date now = new Date();
@@ -104,6 +87,17 @@ public abstract class AbstractTrace implements Trace {
                return message.toString();
        }
        
+       protected String formatMessage(String kind, String text, Throwable th) {
+               StringBuffer message = new StringBuffer();
+               Date now = new Date();
+               message.append(formatDate(now)).append(" ");
+               message.append(Thread.currentThread().getName()).append(" ");
+               message.append(kind).append(" ");
+               message.append(text);
+               if (th != null) message.append(" ").append(formatObj(th));
+               return message.toString();
+       }
+       
        private static String formatDate (Date date) {
                if (timeFormat == null) {
                        timeFormat = new SimpleDateFormat("HH:mm:ss.SSS");
@@ -166,4 +160,4 @@ public abstract class AbstractTrace implements Trace {
                
                return sb.toString();
        }
-}
+}
\ No newline at end of file
index a5fb09751784f1f033ef1fac006452fdc8c12c1d..75afbaf2d26c2a80edfe63d4b7e222267d70eaab 100644 (file)
@@ -61,4 +61,34 @@ public class CommonsTrace extends AbstractTrace {
        public void setTraceEnabled (boolean b) {
        }
 
+       public void debug (String message) {
+               if (log.isDebugEnabled()) {
+                       log.debug(message);
+               }
+       }
+
+       public void info(String message) {
+               if (log.isInfoEnabled()) {
+                       log.info(message);
+               }
+       }
+
+       public void warn (String message, Throwable th) {
+               if (log.isWarnEnabled()) {
+                       log.warn(message,th);
+               }
+       }
+
+       public void error (String message, Throwable th) {
+               if (log.isErrorEnabled()) {
+                       log.error(message,th);
+               }
+       }
+
+       public void fatal (String message, Throwable th) {
+               if (log.isFatalEnabled()) {
+                       log.fatal(message,th);
+               }
+       }
+
 }
index 3e0bb5609b920755a8abd80b0d7648becadb0417..b50c081ec197fefe690a66dee4c65e6cc253caba 100644 (file)
@@ -55,6 +55,40 @@ public class DefaultTrace extends AbstractTrace {
                        println(formatMessage("<",tracedClass.getName(),methodName,th, null));
                }
        }
+       public void debug (String message) {
+               if (traceEnabled) {
+                       println(formatMessage("?",message,null));
+               }
+       }
+
+       public void info(String message) {
+               if (traceEnabled) {
+                       println(formatMessage("I",message,null));
+               }
+       }
+
+       public void warn (String message, Throwable th) {
+               if (traceEnabled) {
+                       println(formatMessage("W",message,th));
+                       if (th != null) th.printStackTrace();
+               }
+       }
+
+
+       public void error (String message, Throwable th) {
+               if (traceEnabled) {
+                       println(formatMessage("E",message,th));
+                       if (th != null) th.printStackTrace();
+               }
+               
+       }
+
+       public void fatal (String message, Throwable th) {
+               if (traceEnabled) {
+                       println(formatMessage("X",message,th));
+                       if (th != null) th.printStackTrace();
+               }
+       }
 
        /**
         * Template method that allows choice of destination for output
index 9c8fdb6ab2d88f7918c7f8a60690329ffa996b1e..ace5f49716feb57e726cafc45820ffe0de8aeb11 100644 (file)
@@ -26,6 +26,8 @@ public interface Trace {
 
        public void event (String methodName, Object thiz, Object[] args);
        
+       public void debug (String message);
+       
        public void info (String message);
 
        public void warn (String message);
@@ -35,6 +37,11 @@ public interface Trace {
        public void error (String message);
 
        public void error (String message, Throwable th);
+
+       public void fatal (String message);
+
+       public void fatal (String message, Throwable th);
+       
        
        /*
         * Convenience methods
@@ -48,4 +55,4 @@ public interface Trace {
        public boolean isTraceEnabled ();
 
        public void setTraceEnabled (boolean b);
-}
+}
\ No newline at end of file
diff --git a/weaver/testsrc/org/aspectj/weaver/AbstractTraceTest.java b/weaver/testsrc/org/aspectj/weaver/AbstractTraceTest.java
new file mode 100644 (file)
index 0000000..abdff5f
--- /dev/null
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     Matthew Webster - initial implementation
+ *******************************************************************************/
+package org.aspectj.weaver;
+
+import junit.framework.TestCase;
+
+import org.aspectj.weaver.tools.AbstractTrace;
+import org.aspectj.weaver.tools.DefaultTrace;
+
+public class AbstractTraceTest extends TestCase {
+
+       protected AbstractTrace trace;
+
+       public void testIsTraceEnabled() {
+               DefaultTrace trace = new DefaultTrace(getClass());
+               assertFalse(trace.isTraceEnabled());
+       }
+
+       public void testEnterWithThisAndArgs() {
+               trace.enter("testEnterWithThisAndArgs",this,new Object[] { "arg1", "arg2" });
+       }
+
+       public void testEnterWithThis() {
+               trace.enter("testEnterWithThis",this);
+       }
+
+       public void testEnter() {
+               trace.enter("testEnter");
+       }
+
+       public void testExitWithReturn() {
+               trace.exit("testExitWithReturn","ret");
+       }
+
+       public void testExitWithThrowable() {
+               trace.exit("testExitWithThrowable",new RuntimeException());
+       }
+
+       public void testExit() {
+               trace.exit("testExit");
+       }
+
+       public void testDebug() {
+               trace.debug("debug");
+       }
+
+       public void testInfo() {
+               trace.info("information");
+       }
+
+       public void testWarn() {
+               trace.warn("warning");
+       }
+
+       public void testWarnWithException() {
+               trace.warn("warning",new RuntimeException("warning"));
+       }
+
+       public void testError() {
+               trace.error("error");
+       }
+
+       public void testErrorWithException() {
+               trace.error("error",new RuntimeException("error"));
+       }
+
+       public void testFatal() {
+               trace.fatal("fatal");
+       }
+
+       public void testFatalWithException() {
+               trace.fatal("fatal",new RuntimeException("fatal"));
+       }
+
+}
index ccc074be1bf415d16bca2f125b8c8a105f9a57fd..1ff47c45a366daf1704e949ee858d0a7de39e40b 100644 (file)
@@ -12,51 +12,18 @@ package org.aspectj.weaver;
 
 import org.aspectj.weaver.tools.CommonsTrace;
 
-import junit.framework.TestCase;
-
-public class CommonsTraceTest extends TestCase {
-
-       private CommonsTrace trace; 
+public class CommonsTraceTest extends AbstractTraceTest {
        
        protected void setUp() throws Exception {
                super.setUp();
                trace = new CommonsTrace(getClass());
                trace.setTraceEnabled(true);
        }
-
+       
        public void testCommonsTrace() {
                CommonsTrace trace = new CommonsTrace(getClass());
        }
 
-       public void testEnterWithThisAndArgs() {
-               trace.enter("testEnterWithThisAndArgs",this,new Object[] { "arg1", "arg2" });
-       }
-
-       public void testEnterWithThis() {
-               trace.enter("testEnterWithThis",this);
-       }
-
-       public void testEnter() {
-               trace.enter("testEnter");
-       }
-
-       public void testExitWithReturn() {
-               trace.exit("testExitWithReturn","ret");
-       }
-
-       public void testExitWithThrowable() {
-               trace.exit("testExitWithThrowable",new RuntimeException());
-       }
-
-       public void testExit() {
-               trace.exit("testExit");
-       }
-
-       public void testIsTraceEnabled() {
-               CommonsTrace trace = new CommonsTrace(getClass());
-               assertFalse(trace.isTraceEnabled());
-       }
-
        public void testSetTraceEnabled() {
                CommonsTrace trace = new CommonsTrace(getClass());
                trace.setTraceEnabled(true);
index 784c209928d594972d18c7fdb4354c2eba3f7f91..b582d9bc4ba24b1358fca428cc403eda85fbe777 100644 (file)
  *******************************************************************************/
 package org.aspectj.weaver;
 
-import junit.framework.TestCase;
-
 import org.aspectj.weaver.tools.DefaultTrace;
 
-public class DefaultTraceTest extends TestCase {
+public class DefaultTraceTest extends AbstractTraceTest {
 
-       private DefaultTrace trace; 
-       
        protected void setUp() throws Exception {
                super.setUp();
                trace = new DefaultTrace(getClass());
@@ -28,39 +24,9 @@ public class DefaultTraceTest extends TestCase {
                DefaultTrace trace = new DefaultTrace(getClass());
        }
 
-       public void testEnterWithThisAndArgs() {
-               trace.enter("testEnterWithThisAndArgs",this,new Object[] { "arg1", "arg2" });
-       }
-
-       public void testEnterWithThis() {
-               trace.enter("testEnterWithThis",this);
-       }
-
-       public void testEnter() {
-               trace.enter("testEnter");
-       }
-
-       public void testExitWithReturn() {
-               trace.exit("testExitWithReturn","ret");
-       }
-
-       public void testExitWithThrowable() {
-               trace.exit("testExitWithThrowable",new RuntimeException());
-       }
-
-       public void testExit() {
-               trace.exit("testExit");
-       }
-
-       public void testIsTraceEnabled() {
-               DefaultTrace trace = new DefaultTrace(getClass());
-               assertFalse(trace.isTraceEnabled());
-       }
-
        public void testSetTraceEnabled() {
                DefaultTrace trace = new DefaultTrace(getClass());
                trace.setTraceEnabled(true);
                assertTrue(trace.isTraceEnabled());
        }
-
 }
index 7847e612765cc568fe9d488f6a49c5146f35cfc8..ffb49939e1fbbc0acdb85e9335f1fa9f069927f3 100644 (file)
@@ -77,5 +77,35 @@ public class Jdk14Trace extends AbstractTrace {
                        logger.setLevel(Level.INFO);
                }
        }
+
+       public void debug (String message) {
+               if (logger.isLoggable(Level.FINE)) {
+                       logger.fine(message);
+               }
+       }
+
+       public void info(String message) {
+               if (logger.isLoggable(Level.INFO)) {
+                       logger.info(message);
+               }
+       }
+
+       public void warn (String message, Throwable th) {
+               if (logger.isLoggable(Level.WARNING)) {
+                       logger.log(Level.WARNING,message,th);
+               }
+       }
+
+       public void error (String message, Throwable th) {
+               if (logger.isLoggable(Level.SEVERE)) {
+                       logger.log(Level.SEVERE,message,th);
+               }
+       }
+
+       public void fatal (String message, Throwable th) {
+               if (logger.isLoggable(Level.SEVERE)) {
+                       logger.log(Level.SEVERE,message,th);
+               }
+       }
        
 }
index 283aadb55e994d2f87fbcff65eae0f69d4a9a424..5accd227b5f3fd00c5779082b056273a4bd114c0 100644 (file)
@@ -13,12 +13,8 @@ package org.aspectj.weaver;
 import org.aspectj.weaver.tools.DefaultTrace;
 import org.aspectj.weaver.tools.Jdk14Trace;
 
-import junit.framework.TestCase;
+public class Jdk14TraceTest extends AbstractTraceTest {
 
-public class Jdk14TraceTest extends TestCase {
-
-       private Jdk14Trace trace; 
-       
        protected void setUp() throws Exception {
                super.setUp();
                trace = new Jdk14Trace(getClass());
@@ -29,35 +25,6 @@ public class Jdk14TraceTest extends TestCase {
                Jdk14Trace trace = new Jdk14Trace(getClass());
        }
 
-       public void testEnterWithThisAndArgs() {
-               trace.enter("testEnterWithThisAndArgs",this,new Object[] { "arg1", "arg2" });
-       }
-
-       public void testEnterWithThis() {
-               trace.enter("testEnterWithThis",this);
-       }
-
-       public void testEnter() {
-               trace.enter("testEnter");
-       }
-
-       public void testExitWithReturn() {
-               trace.exit("testExitWithReturn","ret");
-       }
-
-       public void testExitWithThrowable() {
-               trace.exit("testExitWithThrowable",new RuntimeException());
-       }
-
-       public void testExit() {
-               trace.exit("testExit");
-       }
-
-       public void testIsTraceEnabled() {
-               DefaultTrace trace = new DefaultTrace(getClass());
-               assertFalse(trace.isTraceEnabled());
-       }
-
        public void testSetTraceEnabled() {
                DefaultTrace trace = new DefaultTrace(getClass());
                trace.setTraceEnabled(true);