You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TraceJoinPoints.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // START-SAMPLE tracing-traceJoinPoints Trace join points executed to log
  2. /* TraceJoinPoints.java */
  3. package tracing;
  4. import org.aspectj.lang.*;
  5. import org.aspectj.lang.reflect.*;
  6. import java.io.*;
  7. /**
  8. * Print join points being executed in context to a log.xml file.
  9. * To use this, define the abstract pointcuts in a subaspect.
  10. * @author Jim Hugunin, Wes Isberg
  11. */
  12. public abstract aspect TraceJoinPoints
  13. extends TraceJoinPointsBase {
  14. // abstract protected pointcut entry();
  15. PrintStream out;
  16. int logs = 0;
  17. int depth = 0;
  18. boolean terminal = false;
  19. /**
  20. * Emit a message in the log, e.g.,
  21. * <pre>TraceJoinPoints tjp = TraceJoinPoints.aspectOf();
  22. * if (null != tjp) tjp.message("Hello, World!");</pre>
  23. */
  24. public void message(String s) {
  25. out.println("<message>" + prepareMessage(s) + "</message>");
  26. }
  27. protected void startLog() {
  28. makeLogStream();
  29. }
  30. protected void completeLog() {
  31. closeLogStream();
  32. }
  33. protected void logEnter(JoinPoint.StaticPart jp) {
  34. if (terminal) out.println(">");
  35. indent(depth);
  36. out.print("<" + jp.getKind());
  37. writeSig(jp);
  38. writePos(jp);
  39. depth += 1;
  40. terminal = true;
  41. }
  42. protected void logExit(JoinPoint.StaticPart jp) {
  43. depth -= 1;
  44. if (terminal) {
  45. getOut().println("/>");
  46. } else {
  47. indent(depth);
  48. getOut().println("</" + jp.getKind() + ">");
  49. }
  50. terminal = false;
  51. }
  52. protected PrintStream getOut() {
  53. if (null == out) {
  54. String m = "not in the control flow of entry()";
  55. throw new IllegalStateException(m);
  56. }
  57. return out;
  58. }
  59. protected void makeLogStream() {
  60. try {
  61. String name = "log" + logs++ + ".xml";
  62. out = new PrintStream(new FileOutputStream(name));
  63. } catch (IOException ioe) {
  64. out = System.err;
  65. }
  66. }
  67. protected void closeLogStream() {
  68. PrintStream out = this.out;
  69. if (null != out) {
  70. out.close();
  71. // this.out = null;
  72. }
  73. }
  74. /** @return input String formatted for XML */
  75. protected String prepareMessage(String s) { // XXX unimplemented
  76. return s;
  77. }
  78. void message(String sink, String s) {
  79. if (null == sink) {
  80. message(s);
  81. } else {
  82. getOut().println("<message sink=" + quoteXml(sink)
  83. + " >" + prepareMessage(s) + "</message>");
  84. }
  85. }
  86. void writeSig(JoinPoint.StaticPart jp) {
  87. PrintStream out = getOut();
  88. out.print(" sig=");
  89. out.print(quoteXml(jp.getSignature().toShortString()));
  90. }
  91. void writePos(JoinPoint.StaticPart jp) {
  92. SourceLocation loc = jp.getSourceLocation();
  93. if (loc == null) return;
  94. PrintStream out = getOut();
  95. out.print(" pos=");
  96. out.print(quoteXml(loc.getFileName() +
  97. ":" + loc.getLine() +
  98. ":" + loc.getColumn()));
  99. }
  100. protected String quoteXml(String s) { // XXX weak
  101. return "\"" + s.replace('<', '_').replace('>', '_') + "\"";
  102. }
  103. protected void indent(int i) {
  104. PrintStream out = getOut();
  105. while (i-- > 0) out.print(" ");
  106. }
  107. }
  108. // END-SAMPLE tracing-traceJoinPoints