home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmos2002.zip / SRC / TRACE.MOD < prev    next >
Text File  |  1996-10-16  |  8KB  |  256 lines

  1. IMPLEMENTATION MODULE Trace;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*             Trace routines for Modula-2              *)
  6.         (*                 program development.                 *)
  7.         (*                                                      *)
  8.         (*  Programmer:         P. Moylan                       *)
  9.         (*  Last edited:        16 October 1996                 *)
  10.         (*  Status:             Starting new approach           *)
  11.         (*                                                      *)
  12.         (********************************************************)
  13.  
  14. FROM DumpFile IMPORT
  15.     (* proc *)  DumpString, DumpCard, DumpEOL;
  16.  
  17. (*
  18. FROM Mouse IMPORT
  19.     (* proc *)  MouseAvailable;
  20. *)
  21.  
  22. FROM Timer IMPORT
  23.     (* proc *)  Sleep;
  24.  
  25. FROM Windows IMPORT
  26.     (* type *)  Window, RowRange, ColumnRange,
  27.     (* proc *)  OpenSimpleWindow, CloseWindow, WriteString, WriteLn,
  28.                 ReadChar, EraseLine, CursorUp, PressAnyKey;
  29.  
  30. FROM NumericIO IMPORT
  31.     (* proc *)  WriteCard;
  32.  
  33. (*
  34. FROM UserInterface IMPORT
  35.     (* type *)  UIWindow, Capability, CapabilitySet,
  36.     (* proc *)  AllowMouseControl;
  37. *)
  38.  
  39. FROM TaskControl IMPORT
  40.     (* proc *)  CurrentTaskID;
  41.  
  42. (************************************************************************)
  43.  
  44. TYPE
  45.     ThreadID = [0..100];
  46.  
  47. VAR ThreadInfo: ARRAY ThreadID OF
  48.                     RECORD
  49.                         TraceWindowOpen, TraceEnabled: BOOLEAN;
  50.                         nesting: CARDINAL;  PauseLength: CARDINAL;
  51.                         TraceWindow: Window;
  52.                     END (*RECORD*);
  53.  
  54. (************************************************************************)
  55.  
  56. PROCEDURE CurrentID(): ThreadID;
  57.  
  58.     (* Returns an identifier for the current thread.  If we're tracing  *)
  59.     (* too many threads, or if for any other reason the ID would go     *)
  60.     (* outside the legal range, a result of 0 is returned.              *)
  61.  
  62.     VAR result: CARDINAL;
  63.  
  64.     BEGIN
  65.         result := CurrentTaskID();
  66.         IF result > MAX(ThreadID) THEN result := 0 END (*IF*);
  67.         RETURN result;
  68.     END CurrentID;
  69.  
  70. (************************************************************************)
  71.  
  72. PROCEDURE Pause;
  73.  
  74.     (* Types a "Press any key to continue" message.     *)
  75.     (* A temporary trace window is opened if necessary. *)
  76.  
  77.     BEGIN
  78.         WITH ThreadInfo[CurrentID()] DO
  79.             IF NOT TraceWindowOpen THEN
  80.                 OpenSimpleWindow (TraceWindow, 22, 24, 0, 28);
  81.                 (*
  82.                 IF MouseAvailable() THEN
  83.                     UIW := AllowMouseControl (TraceWindow, "Pause message",
  84.                                             CapabilitySet {wshow, wmove});
  85.                 END (*IF*);
  86.                 *)
  87.             END (*IF*);
  88.             WriteLn (TraceWindow);
  89.             PressAnyKey (TraceWindow);
  90.             IF TraceWindowOpen THEN    (* i.e. not just the temporary window *)
  91.                 EraseLine (TraceWindow, 0);  CursorUp(TraceWindow);
  92.             ELSE
  93.                 CloseWindow (TraceWindow);
  94.             END (*IF*);
  95.         END (*WITH*);
  96.     END Pause;
  97.  
  98. (************************************************************************)
  99.  
  100. PROCEDURE NYI (name: ARRAY OF CHAR);
  101.  
  102.     (* Types a "not yet implemented" message.  A trace window is opened *)
  103.     (* if necessary.                                                    *)
  104.  
  105.     (*VAR UIW: UIWindow;*)
  106.  
  107.     BEGIN
  108.         WITH ThreadInfo[CurrentID()] DO
  109.             IF NOT TraceWindowOpen THEN
  110.                 OpenSimpleWindow (TraceWindow, 21, 24, 1, 45);
  111.                 TraceWindowOpen := TRUE;
  112.                 (*
  113.                 IF MouseAvailable() THEN
  114.                     UIW := AllowMouseControl (TraceWindow, "Procedure trace",
  115.                                     CapabilitySet {wshow, wmove, whide});
  116.                 END (*IF*);
  117.                 *)
  118.             END (*IF*);
  119.             WriteLn (TraceWindow);
  120.             WriteString (TraceWindow, name);
  121.             WriteString (TraceWindow, " is not yet implemented.");
  122.         END (*WITH*);
  123.     END NYI;
  124.  
  125. (************************************************************************)
  126.  
  127. PROCEDURE DeliberatePause;
  128.  
  129.     (* Inserts a delay in execution, for situations where the trace     *)
  130.     (* messages would otherwise flash by on the screen too quickly      *)
  131.     (* to read.                                                         *)
  132.  
  133.     BEGIN
  134.         Sleep (10*ThreadInfo[CurrentID()].PauseLength);
  135.     END DeliberatePause;
  136.  
  137. (************************************************************************)
  138.  
  139. PROCEDURE InTrace (name: ARRAY OF CHAR);
  140.  
  141.     (* To be called when entering a procedure.  *)
  142.  
  143.     VAR j: CARDINAL;
  144.  
  145.     BEGIN
  146.         WITH ThreadInfo[CurrentID()] DO
  147.             IF TraceEnabled THEN
  148.                 WriteLn (TraceWindow);
  149.                 FOR j := 1 TO nesting DO
  150.                     WriteString (TraceWindow, "   ");
  151.                 END (*FOR*);
  152.                 WriteString (TraceWindow, "Entering ");
  153.                 WriteString (TraceWindow, name);
  154.                 DeliberatePause;
  155.             END (*IF*);
  156.             INC (nesting);
  157.         END (*WITH*);
  158.     END InTrace;
  159.  
  160. (************************************************************************)
  161.  
  162. PROCEDURE OutTrace (name: ARRAY OF CHAR);
  163.  
  164.     (* To be called when leaving a procedure.   *)
  165.  
  166.     VAR j: CARDINAL;
  167.  
  168.     BEGIN
  169.         WITH ThreadInfo[CurrentID()] DO
  170.             DEC (nesting);
  171.             IF TraceEnabled THEN
  172.                 WriteLn (TraceWindow);
  173.                 FOR j := 1 TO nesting DO
  174.                     WriteString (TraceWindow, "   ");
  175.                 END (*FOR*);
  176.                 WriteString (TraceWindow, "Leaving ");
  177.                 WriteString (TraceWindow, name);
  178.                 DeliberatePause;
  179.             END (*IF*);
  180.         END (*WITH*);
  181.     END OutTrace;
  182.  
  183. (************************************************************************)
  184.  
  185. PROCEDURE TraceOn (firstrow, lastrow: RowRange;
  186.                         firstcol, lastcol: ColumnRange;
  187.                         SlowDownFactor: CARDINAL);
  188.  
  189.     (* Turns tracing on.                *)
  190.  
  191.     (*VAR UIW: UIWindow;*)
  192.  
  193.     BEGIN
  194.         DumpString ("TraceOn called by task ");  DumpCard (CurrentID());  DumpEOL;
  195.         WITH ThreadInfo[CurrentID()] DO
  196.             IF TraceWindowOpen THEN
  197.  
  198.                 (* Close any previous trace window. *)
  199.  
  200.                 CloseWindow (TraceWindow);
  201.  
  202.             END (*IF*);
  203.             PauseLength := SlowDownFactor;
  204.             OpenSimpleWindow (TraceWindow, firstrow, lastrow, firstcol, lastcol);
  205.             WriteString (TraceWindow, "Tracing task ");
  206.             WriteCard (TraceWindow, CurrentID());  WriteLn (TraceWindow);
  207.             (*
  208.             IF MouseAvailable() THEN
  209.                 UIW := AllowMouseControl (TraceWindow, "Procedure trace",
  210.                                     CapabilitySet {wshow, wmove, whide});
  211.             END (*IF*);
  212.             *)
  213.             TraceWindowOpen := TRUE;  TraceEnabled := TRUE;
  214.         END (*WITH*);
  215.     END TraceOn;
  216.  
  217. (************************************************************************)
  218.  
  219. PROCEDURE TraceOff;
  220.  
  221.     (* Turns tracing off.               *)
  222.  
  223.     BEGIN
  224.         WITH ThreadInfo[CurrentID()] DO
  225.             IF TraceEnabled THEN
  226.                 CloseWindow (TraceWindow);  TraceWindowOpen := FALSE;
  227.             END (*IF*);
  228.             TraceEnabled := FALSE;
  229.         END (*WITH*);
  230.     END TraceOff;
  231.  
  232. (************************************************************************)
  233.  
  234. PROCEDURE TraceStatus (): BOOLEAN;
  235.  
  236.     (* Says whether tracing is currently on.            *)
  237.  
  238.     BEGIN
  239.         RETURN ThreadInfo[CurrentID()].TraceEnabled;
  240.     END TraceStatus;
  241.  
  242. (************************************************************************)
  243. (*                          INITIALISATION                              *)
  244. (************************************************************************)
  245.  
  246. VAR j: ThreadID;
  247.  
  248. BEGIN
  249.     FOR j := 0 TO MAX(ThreadID) DO
  250.         WITH ThreadInfo[j] DO
  251.             TraceEnabled := FALSE;  nesting := 0;  TraceWindowOpen := FALSE;
  252.         END (*WITH*);
  253.     END (*FOR*);
  254. END Trace.
  255.  
  256.