RTDebug.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. unit RTDebug;
  2. interface
  3. Uses Windows, Messages, SysUtils, Classes, MGRegistry;
  4. Const
  5. MG_RTD_AddReference =WM_USER+12123;
  6. MG_RTD_DelReference =MG_RTD_AddReference+1;
  7. MG_RTD_GetListHandle =MG_RTD_AddReference+2;
  8. REG_KEY ='\Software\MaxM_BeppeG\RTDebug\';
  9. REG_LOGFILE ='Log File';
  10. REG_LOGONFILE ='Log File Enabled';
  11. type
  12. TRTDebugParameters =record
  13. processID,
  14. threadID :DWord;
  15. Level :Byte;
  16. theString :ShortString;
  17. StrColor :DWord;
  18. end;
  19. var
  20. LogFileName :String ='';
  21. LogOnFile :Boolean =False;
  22. function RTAssert(Level :Byte; Condition :Boolean; TrueStr, FalseStr :ShortString;
  23. StrColor :DWord=0) :Boolean; overload;
  24. function RTAssert(TrueStr :ShortString; StrColor :DWord=0) :Boolean; overload;
  25. function RTAssert(Condition :Boolean; TrueStr, FalseStr :ShortString; StrColor :DWord=0) :Boolean; overload;
  26. function RTAssert(Condition :Boolean; TrueStr :ShortString; StrColor :DWord=0) :Boolean; overload;
  27. function RTFileAssert(Filename :ShortString; Condition :Boolean; TrueStr, FalseStr :ShortString) :Boolean;
  28. function RTFileEmpty(Filename :ShortString) :Boolean;
  29. function GetLogFileName :String;
  30. implementation
  31. procedure AddLineToList(Level :Byte; theString :ShortString; StrColor :DWord);
  32. Var
  33. pCopyData :TCopyDataStruct;
  34. WinHandle :HWnd;
  35. begin
  36. WinHandle :=FindWindow('TRTDebugMainWin', Nil);
  37. if IsWindow(WinHandle) then
  38. begin
  39. pCopyData.cbData :=SizeOf(TRTDebugParameters);
  40. GetMem(pCopyData.lpData, SizeOf(TRTDebugParameters));
  41. TRTDebugParameters(pCopyData.lpData^).processID :=GetCurrentProcessID;
  42. TRTDebugParameters(pCopyData.lpData^).ThreadID :=GetCurrentThreadID;
  43. TRTDebugParameters(pCopyData.lpData^).Level :=Level;
  44. TRTDebugParameters(pCopyData.lpData^).theString :=theString;
  45. TRTDebugParameters(pCopyData.lpData^).StrColor :=StrColor;
  46. SendMessage(WinHandle, WM_COPYDATA, 0, Integer(@pCopyData));
  47. FreeMem(pCopyData.lpData);
  48. end;
  49. end;
  50. function RTAssert(Level :Byte; Condition :Boolean; TrueStr, FalseStr :ShortString;
  51. StrColor :DWord) :Boolean;
  52. begin
  53. Result :=Condition;
  54. if Result then AddLineToList(Level, TrueStr, StrColor)
  55. else AddLineToList(Level, FalseStr, StrColor);
  56. if (LogOnFile) and (LogFilename <> '')
  57. then RTFileAssert(LogFilename, Condition, TrueStr, FalseStr);
  58. end;
  59. function RTAssert(TrueStr :ShortString; StrColor :DWord=0) :Boolean;
  60. begin
  61. Result :=RTAssert(0, true, TrueStr, '', StrColor);
  62. end;
  63. function RTAssert(Condition :Boolean; TrueStr, FalseStr :ShortString; StrColor :DWord=0) :Boolean;
  64. begin
  65. Result :=RTAssert(0, Condition, TrueStr, FalseStr, StrColor);
  66. end;
  67. function RTAssert(Condition :Boolean; TrueStr :ShortString; StrColor :DWord=0) :Boolean;
  68. begin
  69. if Condition
  70. then Result :=RTAssert(0, true, TrueStr, '', StrColor)
  71. else Result :=False;
  72. end;
  73. function RTFileAssert(Filename :ShortString; Condition :Boolean; TrueStr, FalseStr :ShortString) :Boolean;
  74. Var
  75. ToWrite :PChar;
  76. theFile :TFileStream;
  77. begin
  78. if FileExists(FileName) then theFile :=TFileStream.Create(FileName, fmOpenWrite)
  79. else theFile :=TFileStream.Create(FileName, fmCreate);
  80. try
  81. Result :=False;
  82. theFile.Seek(0, soFromEnd);
  83. if Condition
  84. then ToWrite :=PChar(IntToHex(GetCurrentProcessID,8)+' '+
  85. IntToHex(GetCurrentThreadID,8)+' '+
  86. TrueStr+#13#10)
  87. else ToWrite :=PChar(IntToHex(GetCurrentProcessID,8)+' '+
  88. IntToHex(GetCurrentThreadID,8)+' '+
  89. FalseStr+#13#10);
  90. theFile.Write(ToWrite^, Length(ToWrite));
  91. Result :=True;
  92. finally
  93. theFile.Free;
  94. end;
  95. end;
  96. function RTFileEmpty(Filename :ShortString) :Boolean;
  97. Var
  98. theFile :TFileStream;
  99. begin
  100. theFile :=TFileStream.Create(FileName, fmCreate);
  101. try
  102. Result :=False;
  103. theFile.Size :=0;
  104. Result :=True;
  105. finally
  106. theFile.Free;
  107. end;
  108. end;
  109. function GetLogFileName :String;
  110. Var
  111. xReg :TMGRegistry;
  112. begin
  113. xReg :=TMGRegistry.Create;
  114. if xReg.OpenKeyReadOnly(REG_KEY)
  115. then begin
  116. Result :=xReg.ReadString('', true, REG_LOGFILE);
  117. LogOnFile :=xReg.ReadBool(False, REG_LOGONFILE);
  118. end
  119. else begin
  120. Result :='';
  121. LogOnFile :=False;
  122. end;
  123. xReg.Free;
  124. end;
  125. initialization
  126. LogFileName :=GetLogFileName;
  127. end.