Author: simoneg
Date: Mon Nov 15 20:45:12 2010
New Revision: 1035440
URL: http://svn.apache.org/viewvc?rev=1035440&view=rev
Log:
Renamed to Tracing, added a global guard to disable it from properties
Added:
labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
(with props)
Removed:
labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/CompleteLog.aj
Added:
labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj?rev=1035440&view=auto
==============================================================================
---
labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
(added)
+++
labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
Mon Nov 15 20:45:12 2010
@@ -0,0 +1,144 @@
+package org.apache.magma.logging;
+
+import java.util.Arrays;
+
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.Signature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.magma.settings.Settings;
+
+
+public abstract aspect Tracing {
+
+ private static boolean tracingActive = false;
+
+ static {
+ String tr = Settings.get("magma.tracing");
+ if (tr != null && !tr.equals("false")) {
+ tracingActive = true;
+ }
+ }
+
+ private ThreadLocal<LogThreadLocal> tc = new
ThreadLocal<LogThreadLocal>() {
+ protected LogThreadLocal initialValue() {
+ return new LogThreadLocal(this);
+ }
+ };
+
+ /**
+ * Which code to log.
+ * Must be a within or withincode, eventually with cflow or other
conditions.
+ */
+ public abstract pointcut where();
+
+ /**
+ * Calls to which code to log, usually should exclude stuff already in
"where" to reduce noise.
+ * Must be a call, eventually with other conditions
+ */
+ public abstract pointcut to();
+
+ private interface Closure {
+ Object doProceed();
+ }
+
+ protected Object log(final JoinPoint tjp, Closure clos, final String
enter, String exit, String compress, final String more) {
+ final LogThreadLocal ltl = tc.get();
+ if (ltl.isInside()) {
+ return clos.doProceed();
+ }
+ ltl.entering();
+ try {
+ ltl.sendBuffered();
+ } catch (Exception e) {}
+ ltl.increment();
+ try {
+ final Signature sig = tjp.getSignature();
+ Object th = tjp.getThis();
+ Class c = null;
+ if (th != null) {
+ c = th.getClass();
+ } else {
+ c = tjp.getSignature().getDeclaringType();
+ }
+ final Logger l = LoggerFactory.getLogger(c);
+ final Object[] args = tjp.getArgs();
+ LogClosure logcl = new LogClosure() {
+ @Override
+ public void doLog() {
+ if (args != null && args.length > 0) {
+ l.debug(">{} {} {}({}) [{}]
"+more, new Object[] {ltl.getCounter(), enter, sig.toShortString(), args,
tjp.getSourceLocation()});
+ } else {
+ l.debug(">{} {} {} [{}] "+more,
new Object[] {ltl.getCounter(), enter, sig.toShortString(),
tjp.getSourceLocation()});
+ }
+ }
+ };
+ ltl.setBuffered(logcl);
+ long st = ltl.getTimeOut();
+ ltl.exiting();
+ Object ret = null;
+ ret = clos.doProceed();
+ ltl.entering();
+ double et = ltl.getTimeOut() - st;
+ et = et / 1000000;
+ if (ltl.getBuffered() == null) {
+ l.debug("<{} {} {}({}) took {}ms returned {}",
new Object[] {ltl.getCounter(), exit, sig.toShortString(), args, new
Double(et), ret});
+ } else {
+ ltl.setBuffered(null);
+ l.debug("){} {} {}({}) [{}] took {}ms returned
{} "+more, new Object[] {ltl.getCounter(), compress, sig.toShortString(), args,
tjp.getSourceLocation(), new Double(et), ret});
+ }
+ if (ltl.getCounter() == 1) {
+ l.debug("Logger overhead {}/{}",
ltl.getTimeIn(), ltl.getTimeOut());
+ }
+ return ret;
+ } finally {
+ ltl.decrement();
+ ltl.exiting();
+ }
+ }
+
+ after(JoinPoint tjp) throwing (Throwable t) :
+ execution(* Tracing.log(..)) && args(tjp,..)
+ {
+ final LogThreadLocal ltl = tc.get();
+ if (ltl.isInside()) return;
+ if (ltl.getLastException() == t) return;
+ ltl.entering();
+ try {
+ ltl.setLastException(t);
+ Signature sig = tjp.getSignature();
+ Object th = tjp.getThis();
+ Class c = null;
+ if (th != null) {
+ c = th.getClass();
+ } else {
+ c = tjp.getSignature().getDeclaringType();
+ }
+ Logger l = LoggerFactory.getLogger(c);
+ Object[] args = tjp.getArgs();
+ l.warn("<!" + ltl.getCounter() + " exception " +
sig.toShortString() + "(" + Arrays.toString(args) + ")", t);
+ } finally {
+ ltl.exiting();
+ }
+ }
+
+ Object around(): (execution(* *.*(..)) || adviceexecution()) && where()
&& if(tracingActive) {
+ Closure clos = new Closure() {
+ public Object doProceed() {
+ return proceed();
+ }
+ };
+ return log(thisJoinPoint, clos, "Entering", "Exited",
"Executed", "");
+ }
+
+ Object around(): (call(* *.*(..)) || call(*.new(..))) && where() &&
to() && if(tracingActive) {
+ Closure clos = new Closure() {
+ public Object doProceed() {
+ return proceed();
+ }
+ };
+ return log(thisJoinPoint, clos, "Calling", "Called", "Called",
"");
+ }
+
+}
+
Propchange:
labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]