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.8KB

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