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.

Trace.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. */
  13. package tracing.version3;
  14. import java.io.PrintStream;
  15. /**
  16. *
  17. * This class provides support for printing trace messages into a stream.
  18. * Trace messages are printed before and after constructors and methods
  19. * are executed.
  20. * The messages are appended with the string representation of the objects
  21. * whose constructors and methods are being traced.
  22. * It defines one abstract pointcut for injecting that tracing functionality
  23. * into any application classes.
  24. *
  25. */
  26. abstract aspect Trace {
  27. /*
  28. * Functional part
  29. */
  30. /**
  31. * There are 3 trace levels (values of TRACELEVEL):
  32. * 0 - No messages are printed
  33. * 1 - Trace messages are printed, but there is no indentation
  34. * according to the call stack
  35. * 2 - Trace messages are printed, and they are indented
  36. * according to the call stack
  37. */
  38. public static int TRACELEVEL = 0;
  39. protected static PrintStream stream = null;
  40. protected static int callDepth = 0;
  41. /**
  42. * Initialization.
  43. */
  44. public static void initStream(PrintStream s) {
  45. stream = s;
  46. }
  47. protected static void traceEntry(String str, Object o) {
  48. if (TRACELEVEL == 0) return;
  49. if (TRACELEVEL == 2) callDepth++;
  50. printEntering(str + ": " + o.toString());
  51. }
  52. protected static void traceExit(String str, Object o) {
  53. if (TRACELEVEL == 0) return;
  54. printExiting(str + ": " + o.toString());
  55. if (TRACELEVEL == 2) callDepth--;
  56. }
  57. private static void printEntering(String str) {
  58. printIndent();
  59. stream.println("--> " + str);
  60. }
  61. private static void printExiting(String str) {
  62. printIndent();
  63. stream.println("<-- " + str);
  64. }
  65. private static void printIndent() {
  66. for (int i = 0; i < callDepth; i++)
  67. stream.print(" ");
  68. }
  69. /*
  70. * Crosscut part
  71. */
  72. /**
  73. * Application classes - left unspecified.
  74. */
  75. abstract pointcut myClass(Object obj);
  76. /**
  77. * The constructors in those classes.
  78. */
  79. pointcut myConstructor(Object obj): myClass(obj) && execution(new(..));
  80. /**
  81. * The methods of those classes.
  82. */
  83. // toString is called from within our advice, so we shouldn't
  84. // advise its executions. But if toString is overridden, even
  85. // this might not be enough, so we might want
  86. // && !cflow(execution(String toString()))
  87. pointcut myMethod(Object obj): myClass(obj) &&
  88. execution(* *(..)) && !execution(String toString());
  89. before(Object obj): myConstructor(obj) {
  90. traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
  91. }
  92. after(Object obj): myConstructor(obj) {
  93. traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
  94. }
  95. before(Object obj): myMethod(obj) {
  96. traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
  97. }
  98. after(Object obj): myMethod(obj) {
  99. traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
  100. }
  101. }