diff options
Diffstat (limited to 'docs/sandbox/common/tracing')
-rw-r--r-- | docs/sandbox/common/tracing/Logging.java | 28 | ||||
-rw-r--r-- | docs/sandbox/common/tracing/TraceJoinPoints.java | 132 | ||||
-rw-r--r-- | docs/sandbox/common/tracing/TraceJoinPointsBase.java | 53 | ||||
-rw-r--r-- | docs/sandbox/common/tracing/TraceMyJoinPoints.java | 17 |
4 files changed, 230 insertions, 0 deletions
diff --git a/docs/sandbox/common/tracing/Logging.java b/docs/sandbox/common/tracing/Logging.java new file mode 100644 index 000000000..349abb235 --- /dev/null +++ b/docs/sandbox/common/tracing/Logging.java @@ -0,0 +1,28 @@ + +package tracing; + +import org.aspectj.lang.Signature; + +/** + * @author Wes Isberg + */ +aspect A { + // START-SAMPLE tracing-simpleTiming Record time to execute public methods + /** record time to execute my public methods */ + Object around() : execution(public * com.company..*.* (..)) { + long start = System.currentTimeMillis(); + try { + return proceed(); + } finally { + long end = System.currentTimeMillis(); + recordTime(start, end, + thisJoinPointStaticPart.getSignature()); + } + } + // implement recordTime... + // END-SAMPLE tracing-simpleTiming + + void recordTime(long start, long end, Signature sig) { + // to implement... + } +} diff --git a/docs/sandbox/common/tracing/TraceJoinPoints.java b/docs/sandbox/common/tracing/TraceJoinPoints.java new file mode 100644 index 000000000..fd42a0fde --- /dev/null +++ b/docs/sandbox/common/tracing/TraceJoinPoints.java @@ -0,0 +1,132 @@ + +// START-SAMPLE tracing-traceJoinPoints Trace join points executed to log +/* TraceJoinPoints.java */ + +package tracing; + +import org.aspectj.lang.*; +import org.aspectj.lang.reflect.*; +import java.io.*; + +/** + * Print join points being executed in context to a log.xml file. + * To use this, define the abstract pointcuts in a subaspect. + * @author Jim Hugunin, Wes Isberg + */ +public abstract aspect TraceJoinPoints + extends TraceJoinPointsBase { + + // abstract protected pointcut entry(); + + PrintStream out; + int logs = 0; + int depth = 0; + boolean terminal = false; + + /** + * Emit a message in the log, e.g., + * <pre>TraceJoinPoints tjp = TraceJoinPoints.aspectOf(); + * if (null != tjp) tjp.message("Hello, World!");</pre> + */ + public void message(String s) { + out.println("<message>" + prepareMessage(s) + "</message>"); + } + + protected void startLog() { + makeLogStream(); + } + + protected void completeLog() { + closeLogStream(); + } + + protected void logEnter(JoinPoint.StaticPart jp) { + if (terminal) out.println(">"); + indent(depth); + out.print("<" + jp.getKind()); + writeSig(jp); + writePos(jp); + + depth += 1; + terminal = true; + } + + protected void logExit(JoinPoint.StaticPart jp) { + depth -= 1; + if (terminal) { + getOut().println("/>"); + } else { + indent(depth); + getOut().println("</" + jp.getKind() + ">"); + } + terminal = false; + } + + protected PrintStream getOut() { + if (null == out) { + String m = "not in the control flow of entry()"; + throw new IllegalStateException(m); + } + return out; + } + + protected void makeLogStream() { + try { + String name = "log" + logs++ + ".xml"; + out = new PrintStream(new FileOutputStream(name)); + } catch (IOException ioe) { + out = System.err; + } + } + + protected void closeLogStream() { + PrintStream out = this.out; + if (null != out) { + out.close(); + // this.out = null; + } + } + + /** @return input String formatted for XML */ + protected String prepareMessage(String s) { // XXX unimplemented + return s; + } + + void message(String sink, String s) { + if (null == sink) { + message(s); + } else { + getOut().println("<message sink=" + quoteXml(sink) + + " >" + prepareMessage(s) + "</message>"); + } + } + + void writeSig(JoinPoint.StaticPart jp) { + PrintStream out = getOut(); + out.print(" sig="); + out.print(quoteXml(jp.getSignature().toShortString())); + } + + void writePos(JoinPoint.StaticPart jp) { + SourceLocation loc = jp.getSourceLocation(); + if (loc == null) return; + PrintStream out = getOut(); + + out.print(" pos="); + out.print(quoteXml(loc.getFileName() + + ":" + loc.getLine() + + ":" + loc.getColumn())); + } + + protected String quoteXml(String s) { // XXX weak + return "\"" + s.replace('<', '_').replace('>', '_') + "\""; + } + + protected void indent(int i) { + PrintStream out = getOut(); + while (i-- > 0) out.print(" "); + } +} +// END-SAMPLE tracing-traceJoinPoints + +
\ No newline at end of file diff --git a/docs/sandbox/common/tracing/TraceJoinPointsBase.java b/docs/sandbox/common/tracing/TraceJoinPointsBase.java new file mode 100644 index 000000000..d06423001 --- /dev/null +++ b/docs/sandbox/common/tracing/TraceJoinPointsBase.java @@ -0,0 +1,53 @@ + +// START-SAMPLE tracing-traceJoinPoints Trace join points executed +/* TraceJoinPointsBase.java */ + +package tracing; + +import org.aspectj.lang.JoinPoint; + +/** + * Trace join points being executed in context. + * To use this, define the abstract members in a subaspect. + * <b>Warning</b>: this does not trace join points that do not + * support after advice. + * @author Jim Hugunin, Wes Isberg + */ +abstract aspect TraceJoinPointsBase { + // this line is for AspectJ 1.1 + // for Aspectj 1.0, use "TraceJoinPointsBase dominates * {" + declare precedence : TraceJoinPointsBase, *; + + abstract protected pointcut entry(); + + protected pointcut exit(): call(* java..*.*(..)); + + final pointcut start(): entry() && !cflowbelow(entry()); + + final pointcut trace(): cflow(entry()) + && !cflowbelow(exit()) && !within(TraceJoinPointsBase+); + + private pointcut supportsAfterAdvice() : !handler(*) + && !preinitialization(new(..)); + + before(): start() { startLog(); } + + before(): trace() && supportsAfterAdvice(){ + logEnter(thisJoinPointStaticPart); + } + + after(): trace() && supportsAfterAdvice() { + logExit(thisJoinPointStaticPart); + } + + after(): start() { completeLog(); } + + abstract protected void logEnter(JoinPoint.StaticPart jp); + abstract protected void logExit(JoinPoint.StaticPart jp); + abstract protected void startLog(); + abstract protected void completeLog(); +} + +// END-SAMPLE tracing-traceJoinPoints + +
\ No newline at end of file diff --git a/docs/sandbox/common/tracing/TraceMyJoinPoints.java b/docs/sandbox/common/tracing/TraceMyJoinPoints.java new file mode 100644 index 000000000..a5aa686d6 --- /dev/null +++ b/docs/sandbox/common/tracing/TraceMyJoinPoints.java @@ -0,0 +1,17 @@ + + +// START-SAMPLE tracing-traceJoinPoints Trace to log join points executed by main method +/* TraceMyJoinPoints.java */ + +package tracing; + +import com.company.app.Main; + +/** + * Trace all join points in company application. + * @author Jim Hugunin, Wes Isberg + */ +aspect TraceMyJoinPoints extends TraceJoinPoints { + protected pointcut entry() : execution(void Main.runMain(String[])); +} +// END-SAMPLE tracing-traceJoinPoints |