labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r1042578 - in /labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging: LogThreadLocal.java Tracing.aj
Date Mon, 06 Dec 2010 11:06:12 GMT
Author: simoneg
Date: Mon Dec  6 11:06:11 2010
New Revision: 1042578

URL: http://svn.apache.org/viewvc?rev=1042578&view=rev
Log:
Rewritten as before/after instead of around to avoid AspectJ bug

Modified:
    labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/LogThreadLocal.java
    labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj

Modified: labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/LogThreadLocal.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/LogThreadLocal.java?rev=1042578&r1=1042577&r2=1042578&view=diff
==============================================================================
--- labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/LogThreadLocal.java
(original)
+++ labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/LogThreadLocal.java
Mon Dec  6 11:06:11 2010
@@ -1,5 +1,7 @@
 package org.apache.magma.logging;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Stack;
 
 public class LogThreadLocal {
@@ -13,6 +15,8 @@ public class LogThreadLocal {
 	private long timeIn = 0;
 	private long timeOut = 0;
 	
+	private ArrayList<Long> times = new ArrayList<Long>();
+	
 	public LogThreadLocal(ThreadLocal<LogThreadLocal> mytl) {
 		this.mytl = mytl;
 	}
@@ -82,4 +86,21 @@ public class LogThreadLocal {
 		return timeOut;
 	}
 	
+	private void checkTimeArray() {
+		this.times.ensureCapacity((int)this.counter + 1);
+		while (this.times.size() <= this.counter) {
+			this.times.add(0l);
+		}		
+	}
+	
+	public void pushTime(long t) {
+		checkTimeArray();
+		this.times.set((int)this.counter, t);
+	}
+	
+	public long popTime() {
+		checkTimeArray();
+		return this.times.get((int)this.counter);
+	}
+	
 };
\ No newline at end of file

Modified: 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=1042578&r1=1042577&r2=1042578&view=diff
==============================================================================
--- labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
(original)
+++ labs/magma/trunk/foundation-logging/src/main/java/org/apache/magma/logging/Tracing.aj
Mon Dec  6 11:06:11 2010
@@ -37,11 +37,123 @@ public abstract aspect Tracing {
 	 * Must be a call, eventually with other conditions
 	 */
 	public abstract pointcut to();
+
+	protected void logIn(final JoinPoint tjp, final String enter, final String more) {
+		final LogThreadLocal ltl = tc.get();
+		if (ltl.isInside()) {
+			return;
+		}
+		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);
+			ltl.pushTime(ltl.getTimeOut());
+		} finally {
+			ltl.exiting();
+		}
+	}
+	
+	protected void logOut(final JoinPoint tjp, Object ret, String exit, String compress, final
String more) {
+		final LogThreadLocal ltl = tc.get();
+		if (ltl.isInside()) {
+			return;
+		}
+		ltl.entering();
+		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();
+			double et = ltl.getTimeOut() - ltl.popTime();
+			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 used {} on {} total nanos", ltl.getTimeIn(), ltl.getTimeOut());
+			}
+		} finally {
+			ltl.decrement();
+			ltl.exiting();			
+		}
+	}
 	
-	private interface Closure {
-		Object doProceed();
+	protected void logException(final JoinPoint tjp, Throwable t) {
+		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();			
+		}		
+	}
+
+	before(): (execution(* *.*(..)) || adviceexecution()) && where() && if(tracingActive)
{
+		logIn(thisJoinPoint, "Entering", "");
+	}
+	after() returning(Object ret): (execution(* *.*(..)) || adviceexecution()) && where()
&& if(tracingActive) {
+		logOut(thisJoinPoint, ret, "Exited", "Executed", "");
+	}
+	after() throwing(Throwable t): (execution(* *.*(..)) || adviceexecution()) && where()
&& if(tracingActive) {
+		logException(thisJoinPoint, t);
+	}
+
+	before(): (call(* *.*(..)) || call(*.new(..))) && where() && to() &&
if(tracingActive) {
+		logIn(thisJoinPoint, "Calling", "");
+	}
+	after() returning(Object ret): (call(* *.*(..)) || call(*.new(..))) && where() &&
to() && if(tracingActive) {
+		logOut(thisJoinPoint, ret, "Called", "Called", "");
+	}
+	after() throwing(Throwable t): (call(* *.*(..)) || call(*.new(..))) && where() &&
to() && if(tracingActive) {
+		logException(thisJoinPoint, t);
 	}
 	
+	/*
 	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()) {
@@ -98,7 +210,7 @@ public abstract aspect Tracing {
 	}
 	
 	after(JoinPoint tjp) throwing (Throwable t) :
-		execution(* Tracing.log(..)) && args(tjp,..)
+		execution(* Tracing.log*(..)) && args(tjp,..)
 	{
 		final LogThreadLocal ltl = tc.get();
 		if (ltl.isInside()) return;
@@ -122,7 +234,7 @@ public abstract aspect Tracing {
 		}
 	}
 	
-	Object around(): (execution(* *.*(..)) || adviceexecution()) && where() &&
if(tracingActive) {
+	Object around(): (execution(* *.*(..))) && where() && if(tracingActive)
{
 		Closure clos = new Closure() {
 			public Object doProceed() {
 				return proceed();
@@ -139,6 +251,8 @@ public abstract aspect Tracing {
 		};
 		return log(thisJoinPoint, clos, "Calling", "Called", "Called", "");
 	}
+	
+	*/
 		
 }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message