From acf2739fa68567d65cd776e583aab6d60c2e6991 Mon Sep 17 00:00:00 2001 From: mwebster Date: Mon, 14 Aug 2006 15:55:52 +0000 Subject: [PATCH] Bug 150487 "Tracing and Logging Framework" (WeavingAdaptorMessageHandler delegates and traces) --- .../loadtime/ClassLoaderWeavingAdaptor.java | 22 +-- .../systemtest/tracing/TracingTests.java | 4 +- .../aspectj/systemtest/tracing/tracing.xml | 37 +++-- tests/tracing/ant.xml | 13 ++ tests/tracing/aop-tracemessages.xml | 13 ++ .../aspectj/weaver/tools/WeavingAdaptor.java | 134 ++++++++++++++---- 6 files changed, 173 insertions(+), 50 deletions(-) create mode 100644 tests/tracing/aop-tracemessages.xml diff --git a/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java b/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java index 161c8f62f..d43034c90 100644 --- a/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java +++ b/loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java @@ -124,8 +124,8 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { }; List definitions = parseDefinitions(classLoader); - if (!enabled) { - if (trace.isTraceEnabled()) trace.exit("initialize",enabled); + if (!isEnabled()) { + if (trace.isTraceEnabled()) trace.exit("initialize",false); return; } @@ -147,7 +147,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { // register the definitions registerDefinitions(weaver, classLoader, definitions); - if (enabled) { + if (isEnabled()) { //bcelWorld.setResolutionLoader(loader.getParent());//(ClassLoader)null);// @@ -160,7 +160,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { } initialized = true; - if (trace.isTraceEnabled()) trace.exit("initialize",enabled); + if (trace.isTraceEnabled()) trace.exit("initialize",isEnabled()); } /** @@ -198,11 +198,11 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { } } if (definitions.isEmpty()) { - enabled = false;// will allow very fast skip in shouldWeave() + disable();// will allow very fast skip in shouldWeave() info("no configuration found. Disabling weaver for class loader " + getClassLoaderName(loader)); } } catch (Exception e) { - enabled = false;// will allow very fast skip in shouldWeave() + disable();// will allow very fast skip in shouldWeave() warn("parse definitions failed",e); } return definitions; @@ -217,7 +217,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { registerIncludeExclude(weaver, loader, definitions); registerDump(weaver, loader, definitions); } catch (Exception e) { - enabled = false;// will allow very fast skip in shouldWeave() + disable();// will allow very fast skip in shouldWeave() warn("register definition failed",(e instanceof AbortException)?null:e); } } @@ -341,7 +341,7 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { kind.signal(infos,null,null); } - protected String getContextId () { + public String getContextId () { return weavingContext.getId(); } @@ -413,11 +413,11 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { /* We didn't register any aspects so disable the adaptor */ if (namespace == null) { - enabled = false; + disable(); info("no aspects registered. Disabling weaver for class loader " + getClassLoaderName(loader)); } - if (trace.isTraceEnabled()) trace.exit("registerAspects",enabled); + if (trace.isTraceEnabled()) trace.exit("registerAspects",isEnabled()); } /** @@ -697,4 +697,4 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor { if (trace.isTraceEnabled()) trace.exit("defineClass",clazz); } -} +} \ No newline at end of file diff --git a/tests/src/org/aspectj/systemtest/tracing/TracingTests.java b/tests/src/org/aspectj/systemtest/tracing/TracingTests.java index e782e0887..1eb7924bf 100644 --- a/tests/src/org/aspectj/systemtest/tracing/TracingTests.java +++ b/tests/src/org/aspectj/systemtest/tracing/TracingTests.java @@ -34,8 +34,8 @@ public class TracingTests extends org.aspectj.testing.XMLBasedAjcTestCase { runTest("Default tracing"); } - public void testDefaultTracingMessageHandler () { - runTest("Default TraceMessageHandler"); + public void testTraceMessages () { + runTest("Trace messages"); } } diff --git a/tests/src/org/aspectj/systemtest/tracing/tracing.xml b/tests/src/org/aspectj/systemtest/tracing/tracing.xml index b0dca8d8c..a9f64bd69 100644 --- a/tests/src/org/aspectj/systemtest/tracing/tracing.xml +++ b/tests/src/org/aspectj/systemtest/tracing/tracing.xml @@ -7,35 +7,54 @@ - + + + + + - + + + + + - + - + + + + + + + - - + + - + - diff --git a/tests/tracing/ant.xml b/tests/tracing/ant.xml index f7852949e..b0b7a9e17 100644 --- a/tests/tracing/ant.xml +++ b/tests/tracing/ant.xml @@ -24,4 +24,17 @@ + + + + + + + + + + + + diff --git a/tests/tracing/aop-tracemessages.xml b/tests/tracing/aop-tracemessages.xml new file mode 100644 index 000000000..4f5e36a5a --- /dev/null +++ b/tests/tracing/aop-tracemessages.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java b/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java index dc7cb73fa..3d80b9901 100644 --- a/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java +++ b/weaver/src/org/aspectj/weaver/tools/WeavingAdaptor.java @@ -31,11 +31,13 @@ import java.util.StringTokenizer; import org.aspectj.bridge.AbortException; import org.aspectj.bridge.IMessage; +import org.aspectj.bridge.IMessageContext; import org.aspectj.bridge.IMessageHandler; import org.aspectj.bridge.Message; import org.aspectj.bridge.MessageUtil; import org.aspectj.bridge.MessageWriter; import org.aspectj.bridge.Version; +import org.aspectj.bridge.WeaveMessage; import org.aspectj.bridge.IMessage.Kind; import org.aspectj.org.objectweb.asm.ClassReader; import org.aspectj.util.FileUtil; @@ -63,15 +65,16 @@ import org.aspectj.weaver.bcel.Utility; * weaving messages to be written to the console. * */ -public class WeavingAdaptor { +public class WeavingAdaptor implements IMessageContext { /** * System property used to turn on verbose weaving messages */ public static final String WEAVING_ADAPTOR_VERBOSE = "aj.weaving.verbose"; public static final String SHOW_WEAVE_INFO_PROPERTY = "org.aspectj.weaver.showWeaveInfo"; + public static final String TRACE_MESSAGES_PROPERTY = "org.aspectj.tracing.messages"; - protected boolean enabled = true; + private boolean enabled = true; protected boolean verbose = getVerbose(); protected BcelWorld bcelWorld; protected BcelWeaver weaver; @@ -179,12 +182,21 @@ public class WeavingAdaptor { } protected void setMessageHandler (IMessageHandler mh) { - if (messageHolder != null) { - messageHolder.flushMessages(); - messageHolder = null; + if (mh instanceof ISupportsMessageContext) { + ISupportsMessageContext smc = (ISupportsMessageContext)mh; + smc.setMessageContext(this); } - messageHandler = mh; - bcelWorld.setMessageHandler(mh); + if (mh != messageHolder) messageHolder.setDelegate(mh); + messageHolder.flushMessages(); + } + + protected void disable () { + enabled = false; + messageHolder.flushMessages(); + } + + protected boolean isEnabled () { + return enabled; } /** @@ -396,7 +408,7 @@ public class WeavingAdaptor { return MessageUtil.error(messageHandler,message); } - protected String getContextId () { + public String getContextId () { return "WeavingAdaptor"; } @@ -436,14 +448,97 @@ public class WeavingAdaptor { * Processes messages arising from weaver operations. * Tell weaver to abort on any message more severe than warning. */ - protected class WeavingAdaptorMessageHandler extends MessageWriter { + protected class WeavingAdaptorMessageHandler implements IMessageHandler { - private Set ignoring = new HashSet(); - private IMessage.Kind failKind; + private IMessageHandler delegate; private boolean accumulating = true; private List messages = new ArrayList(); + protected boolean traceMessages = Boolean.getBoolean(TRACE_MESSAGES_PROPERTY); + public WeavingAdaptorMessageHandler (PrintWriter writer) { + + this.delegate = new WeavingAdaptorMessageWriter(writer); + } + + public boolean handleMessage(IMessage message) throws AbortException { + if (traceMessages) traceMessage(message); + if (accumulating) { + boolean result = addMessage(message); + if (0 <= message.getKind().compareTo(IMessage.ERROR)) { + throw new AbortException(message); + } + return result; + } + else return delegate.handleMessage(message); + } + + private void traceMessage (IMessage message) { + if (message instanceof WeaveMessage) { + trace.debug(render(message)); + } + else if (message.isDebug()) { + trace.debug(render(message)); + } + else if (message.isInfo()) { + trace.info(render(message)); + } + else if (message.isWarning()) { + trace.warn(render(message),message.getThrown()); + } + else if (message.isError()) { + trace.error(render(message),message.getThrown()); + } + else if (message.isFailed()) { + trace.fatal(render(message),message.getThrown()); + } + else if (message.isAbort()) { + trace.fatal(render(message),message.getThrown()); + } + else { + trace.error(render(message),message.getThrown()); + } + } + + protected String render(IMessage message) { + return "[" + getContextId() + "] " + message.toString(); + } + + public boolean isIgnoring (Kind kind) { + return delegate.isIgnoring(kind); + } + + public void dontIgnore (IMessage.Kind kind) { + if (null != kind) { + delegate.dontIgnore(kind); + } + } + + private boolean addMessage (IMessage message) { + messages.add(message); + return true; + } + + public void flushMessages () { + for (Iterator iter = messages.iterator(); iter.hasNext();) { + IMessage message = (IMessage)iter.next(); + delegate.handleMessage(message); + } + accumulating = false; + messages.clear(); + } + + public void setDelegate (IMessageHandler messageHandler) { + delegate = messageHandler; + } + } + + protected class WeavingAdaptorMessageWriter extends MessageWriter { + + private Set ignoring = new HashSet(); + private IMessage.Kind failKind; + + public WeavingAdaptorMessageWriter (PrintWriter writer) { super(writer,true); ignore(IMessage.WEAVEINFO); @@ -452,7 +547,6 @@ public class WeavingAdaptor { } public boolean handleMessage(IMessage message) throws AbortException { - addMessage(message); boolean result = super.handleMessage(message); if (0 <= message.getKind().compareTo(failKind)) { throw new AbortException(message); @@ -479,24 +573,8 @@ public class WeavingAdaptor { public void dontIgnore (IMessage.Kind kind) { if (null != kind) { ignoring.remove(kind); - if (kind.equals(IMessage.INFO)) accumulating = false; - } - } - - private void addMessage (IMessage message) { - if (accumulating && isIgnoring(message.getKind())) { - messages.add(message); } } - - public void flushMessages () { - for (Iterator iter = messages.iterator(); iter.hasNext();) { - IMessage message = (IMessage)iter.next(); - super.handleMessage(message); - } - accumulating = false; - messages.clear(); - } protected String render(IMessage message) { return "[" + getContextId() + "] " + super.render(message); -- 2.39.5