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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. */
  10. package tracing.version2;
  11. import java.io.PrintStream;
  12. /**
  13. *
  14. * This class provides support for printing trace messages into a stream.
  15. * Trace messages are printed before and after constructors and methods
  16. * are executed.
  17. * It defines one abstract crosscut for injecting that tracing functionality
  18. * into any application classes.
  19. * To use it, provide a subclass that concretizes the abstract crosscut.
  20. */
  21. abstract aspect Trace {
  22. /*
  23. * Functional part
  24. */
  25. /**
  26. * There are 3 trace levels (values of TRACELEVEL):
  27. * 0 - No messages are printed
  28. * 1 - Trace messages are printed, but there is no indentation
  29. * according to the call stack
  30. * 2 - Trace messages are printed, and they are indented
  31. * according to the call stack
  32. */
  33. public static int TRACELEVEL = 2;
  34. protected static PrintStream stream = System.err;
  35. protected static int callDepth = 0;
  36. /**
  37. * Initialization.
  38. */
  39. public static void initStream(PrintStream s) {
  40. stream = s;
  41. }
  42. protected static void traceEntry(String str) {
  43. if (TRACELEVEL == 0) return;
  44. if (TRACELEVEL == 2) callDepth++;
  45. printEntering(str);
  46. }
  47. protected static void traceExit(String str) {
  48. if (TRACELEVEL == 0) return;
  49. printExiting(str);
  50. if (TRACELEVEL == 2) callDepth--;
  51. }
  52. private static void printEntering(String str) {
  53. printIndent();
  54. stream.println("--> " + str);
  55. }
  56. private static void printExiting(String str) {
  57. printIndent();
  58. stream.println("<-- " + str);
  59. }
  60. private static void printIndent() {
  61. for (int i = 0; i < callDepth; i++)
  62. stream.print(" ");
  63. }
  64. /*
  65. * Crosscut part
  66. */
  67. /**
  68. * Application classes - left unspecified.
  69. * Subclasses should concretize this pointcut with class names.
  70. */
  71. abstract pointcut myClass();
  72. /**
  73. * The constructors in those classes.
  74. */
  75. pointcut myConstructor(): myClass() && execution(new(..));
  76. /**
  77. * The methods of those classes.
  78. */
  79. pointcut myMethod(): myClass() && execution(* *(..));
  80. /**
  81. * Prints trace messages before and after executing constructors.
  82. */
  83. before(): myConstructor() {
  84. traceEntry("" + thisJoinPointStaticPart.getSignature());
  85. }
  86. after(): myConstructor() {
  87. traceExit("" + thisJoinPointStaticPart.getSignature());
  88. }
  89. /**
  90. * Prints trace messages before and after executing methods.
  91. */
  92. before(): myMethod() {
  93. traceEntry("" + thisJoinPointStaticPart.getSignature());
  94. }
  95. after(): myMethod() {
  96. traceExit("" + thisJoinPointStaticPart.getSignature());
  97. }
  98. }