Win32.WinTrace.Mod 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. MODULE WinTrace;
  2. IMPORT Kernel32, Modules,Trace,Commands;
  3. CONST
  4. none = 0; console = 1; file = 2;
  5. VAR
  6. hin-, hout-: Kernel32.HANDLE;
  7. mode: LONGINT; (* none, console or file *)
  8. (* Sender to be used with Stream.Writer *)
  9. PROCEDURE Send* (CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: LONGINT);
  10. VAR b: Kernel32.BOOL;
  11. BEGIN
  12. IF mode # none THEN
  13. b := Kernel32.WriteFile (hout, buf[ofs], len, len, NIL);
  14. Kernel32.FlushFileBuffers(hout);
  15. END;
  16. END Send;
  17. (* Receiver to be used with Stream.Reader *)
  18. PROCEDURE Receive* (VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len, res: LONGINT);
  19. VAR b: Kernel32.BOOL;
  20. BEGIN
  21. b := Kernel32.ReadFile (hin, buf[ofs], min, len, NIL);
  22. END Receive;
  23. PROCEDURE Init;
  24. BEGIN
  25. mode := none;
  26. END Init;
  27. PROCEDURE Close*;
  28. VAR res: LONGINT;
  29. BEGIN
  30. IF mode = console THEN
  31. Kernel32.CloseHandle(hout);
  32. res := Kernel32.FreeConsole ();
  33. ELSIF mode = file THEN
  34. Kernel32.CloseHandle(hout);
  35. END;
  36. hout := Kernel32.InvalidHandleValue;
  37. mode := none;
  38. END Close;
  39. PROCEDURE OpenConsole*;
  40. VAR res: LONGINT;
  41. BEGIN
  42. IF mode = console THEN RETURN
  43. ELSIF mode = file THEN Close
  44. END;
  45. IF Kernel32.AttachConsole(-1) = Kernel32.False THEN
  46. res := Kernel32.AllocConsole ();
  47. END;
  48. hin := Kernel32.GetStdHandle (Kernel32.STDInput);
  49. ASSERT ((hin) # (Kernel32.InvalidHandleValue));
  50. hout := Kernel32.GetStdHandle (Kernel32.STDOutput);
  51. ASSERT ((hout) # (Kernel32.InvalidHandleValue));
  52. Trace.Char := Char;
  53. mode := console;
  54. END OpenConsole;
  55. PROCEDURE OpenFile*(context: Commands.Context);
  56. VAR filename: ARRAY 256 OF CHAR;
  57. BEGIN
  58. Close;
  59. IF ~context.arg.GetString(filename) THEN filename := "WinTrace.Text" END;
  60. hout := Kernel32.CreateFile(filename, {Kernel32.GenericWrite}, {Kernel32.FileShareRead}, NIL, Kernel32.CreateAlways, {Kernel32.FileAttributeNormal}, Kernel32.NULL);
  61. ASSERT ((hout) # (Kernel32.InvalidHandleValue));
  62. Trace.Char := Char;
  63. mode := file;
  64. END OpenFile;
  65. PROCEDURE Terminate;
  66. BEGIN
  67. Close;
  68. END Terminate;
  69. PROCEDURE Char(c: CHAR);
  70. VAR len: LONGINT; b: Kernel32.BOOL;
  71. BEGIN
  72. len := 1;
  73. b := Kernel32.WriteFile(hout,c,len,len,NIL);
  74. END Char;
  75. BEGIN
  76. Init;
  77. Modules.InstallTermHandler (Terminate);
  78. END WinTrace.
  79. WinTrace.OpenFile ~
  80. WinTrace.OpenFile myTrace.Text ~
  81. WinTrace.OpenConsole
  82. WinTrace.Close