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 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.version1;
  11. import java.io.PrintStream;
  12. /**
  13. *
  14. * This class provides some basic functionality for printing trace messages
  15. * into a stream.
  16. *
  17. */
  18. public class Trace {
  19. /**
  20. * There are 3 trace levels (values of TRACELEVEL):
  21. * 0 - No messages are printed
  22. * 1 - Trace messages are printed, but there is no indentation
  23. * according to the call stack
  24. * 2 - Trace messages are printed, and they are indented
  25. * according to the call stack
  26. */
  27. public static int TRACELEVEL = 0;
  28. protected static PrintStream stream = null;
  29. protected static int callDepth = 0;
  30. /**
  31. * Initialization.
  32. */
  33. public static void initStream(PrintStream s) {
  34. stream = s;
  35. }
  36. /**
  37. * Prints an "entering" message. It is intended to be called in the
  38. * beginning of the blocks to be traced.
  39. */
  40. public static void traceEntry(String str) {
  41. if (TRACELEVEL == 0) return;
  42. if (TRACELEVEL == 2) callDepth++;
  43. printEntering(str);
  44. }
  45. /**
  46. * Prints an "exiting" message. It is intended to be called in the
  47. * end of the blocks to be traced.
  48. */
  49. public static void traceExit(String str) {
  50. if (TRACELEVEL == 0) return;
  51. printExiting(str);
  52. if (TRACELEVEL == 2) callDepth--;
  53. }
  54. private static void printEntering(String str) {
  55. printIndent();
  56. stream.println("--> " + str);
  57. }
  58. private static void printExiting(String str) {
  59. printIndent();
  60. stream.println("<-- " + str);
  61. }
  62. private static void printIndent() {
  63. for (int i = 0; i < callDepth; i++)
  64. stream.print(" ");
  65. }
  66. }