dbugintf.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. {
  2. This file is part of the Free Component library.
  3. Copyright (c) 2005 by Michael Van Canneyt, member of
  4. the Free Pascal development team
  5. Debugserver client interface, based on SimpleIPC
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$mode objfpc}
  13. {$h+}
  14. unit dbugintf;
  15. interface
  16. Type
  17. TDebugLevel = (dlInformation,dlWarning,dlError);
  18. procedure SendBoolean(const Identifier: string; const Value: Boolean);
  19. procedure SendDateTime(const Identifier: string; const Value: TDateTime);
  20. procedure SendInteger(const Identifier: string; const Value: Integer; HexNotation: Boolean = False);
  21. procedure SendPointer(const Identifier: string; const Value: Pointer);
  22. procedure SendDebugEx(const Msg: string; MType: TDebugLevel);
  23. procedure SendDebug(const Msg: string);
  24. procedure SendMethodEnter(const MethodName: string);
  25. procedure SendMethodExit(const MethodName: string);
  26. procedure SendSeparator;
  27. procedure SendDebugFmt(const Msg: string; const Args: array of const);
  28. procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TDebugLevel);
  29. { low-level routines }
  30. Function StartDebugServer : integer;
  31. Procedure InitDebugClient;
  32. Const
  33. SendError : String = '';
  34. ResourceString
  35. SProcessID = 'Process %s';
  36. SEntering = '> Entering ';
  37. SExiting = '< Exiting ';
  38. SSeparator = '>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<';
  39. implementation
  40. Uses
  41. SysUtils, classes,msgintf, process, simpleipc;
  42. Const
  43. DmtInformation = lctInformation;
  44. DmtWarning = lctWarning;
  45. DmtError = lctError;
  46. ErrorLevel : Array[TDebugLevel] of integer
  47. = (dmtInformation,dmtWarning,dmtError);
  48. IndentChars = 2;
  49. var
  50. DebugClient : TSimpleIPCClient = nil;
  51. MsgBuffer : TMemoryStream = Nil;
  52. ServerID : Integer;
  53. Indent : Integer = 0;
  54. Procedure WriteMessage(Const Msg : TDebugMessage);
  55. begin
  56. MsgBuffer.Seek(0,soFrombeginning);
  57. WriteDebugMessageToStream(MsgBuffer,Msg);
  58. DebugClient.SendMessage(mtUnknown,MsgBuffer);
  59. end;
  60. procedure SendDebugMessage(Var Msg : TDebugMessage);
  61. begin
  62. try
  63. If (DebugClient=Nil) then
  64. InitDebugClient;
  65. if (Indent>0) then
  66. Msg.Msg:=StringOfChar(' ',Indent)+Msg.Msg;
  67. WriteMessage(Msg);
  68. except
  69. On E : Exception do
  70. SendError:=E.Message;
  71. end;
  72. end;
  73. procedure SendBoolean(const Identifier: string; const Value: Boolean);
  74. Const
  75. Booleans : Array[Boolean] of string = ('False','True');
  76. begin
  77. SendDebugFmt('%s = %s',[Identifier,Booleans[value]]);
  78. end;
  79. procedure SendDateTime(const Identifier: string; const Value: TDateTime);
  80. begin
  81. SendDebugFmt('%s = %s',[Identifier,DateTimeToStr(Value)]);
  82. end;
  83. procedure SendInteger(const Identifier: string; const Value: Integer; HexNotation: Boolean = False);
  84. Const
  85. Msgs : Array[Boolean] of string = ('%s = %d','%s = %x');
  86. begin
  87. SendDebugFmt(Msgs[HexNotation],[Identifier,Value]);
  88. end;
  89. procedure SendPointer(const Identifier: string; const Value: Pointer);
  90. begin
  91. SendDebugFmt('%s = %p',[Identifier,Value]);
  92. end;
  93. procedure SendDebugEx(const Msg: string; MType: TDebugLevel);
  94. Var
  95. Mesg : TDebugMessage;
  96. begin
  97. Mesg.MsgTimeStamp:=Now;
  98. Mesg.MsgType:=ErrorLevel[MTYpe];
  99. Mesg.Msg:=Msg;
  100. SendDebugMessage(Mesg);
  101. end;
  102. procedure SendDebug(const Msg: string);
  103. Var
  104. Mesg : TDebugMessage;
  105. begin
  106. Mesg.MsgTimeStamp:=Now;
  107. Mesg.MsgType:=dmtInformation;
  108. Mesg.Msg:=Msg;
  109. SendDebugMessage(Mesg);
  110. end;
  111. procedure SendMethodEnter(const MethodName: string);
  112. begin
  113. SendDebug(SEntering+MethodName);
  114. inc(Indent,IndentChars);
  115. end;
  116. procedure SendMethodExit(const MethodName: string);
  117. begin
  118. Dec(Indent,IndentChars);
  119. If (Indent<0) then
  120. Indent:=0;
  121. SendDebug(SExiting+MethodName);
  122. end;
  123. procedure SendSeparator;
  124. begin
  125. SendDebug(SSeparator);
  126. end;
  127. procedure SendDebugFmt(const Msg: string; const Args: array of const);
  128. Var
  129. Mesg : TDebugMessage;
  130. begin
  131. Mesg.MsgTimeStamp:=Now;
  132. Mesg.MsgType:=dmtInformation;
  133. Mesg.Msg:=Format(Msg,Args);
  134. SendDebugMessage(Mesg);
  135. end;
  136. procedure SendDebugFmtEx(const Msg: string; const Args: array of const; MType: TDebugLevel);
  137. Var
  138. Mesg : TDebugMessage;
  139. begin
  140. Mesg.MsgTimeStamp:=Now;
  141. Mesg.MsgType:=ErrorLevel[mType];
  142. Mesg.Msg:=Format(Msg,Args);
  143. SendDebugMessage(Mesg);
  144. end;
  145. function StartDebugServer : Integer;
  146. begin
  147. With TProcess.Create(Nil) do
  148. Try
  149. CommandLine:='debugserver';
  150. Execute;
  151. Result:=ProcessID;
  152. Finally
  153. Free;
  154. end;
  155. end;
  156. procedure FreeDebugClient;
  157. Var
  158. msg : TDebugMessage;
  159. begin
  160. try
  161. If (DebugClient<>Nil) and
  162. (DebugClient.ServerRunning) then
  163. begin
  164. Msg.MsgType:=lctStop;
  165. Msg.MsgTimeStamp:=Now;
  166. Msg.Msg:=Format(SProcessID,[ApplicationName]);
  167. WriteMessage(Msg);
  168. end;
  169. FreeAndNil(MsgBuffer);
  170. FreeAndNil(DebugClient);
  171. except
  172. end;
  173. end;
  174. Procedure InitDebugClient;
  175. Var
  176. msg : TDebugMessage;
  177. I : Integer;
  178. begin
  179. DebugClient:=TSimpleIPCClient.Create(Nil);
  180. DebugClient.ServerID:=DebugServerID;
  181. If not DebugClient.ServerRunning then
  182. begin
  183. ServerID:=StartDebugServer;
  184. I:=0;
  185. While (I<10) and not DebugClient.ServerRunning do
  186. begin
  187. Inc(I);
  188. Sleep(100);
  189. end;
  190. end;
  191. DebugClient.Connect;
  192. MsgBuffer:=TMemoryStream.Create;
  193. Msg.MsgType:=lctIdentify;
  194. Msg.MsgTimeStamp:=Now;
  195. Msg.Msg:=Format(SProcessID,[ApplicationName]);
  196. WriteMessage(Msg);
  197. end;
  198. Initialization
  199. Finalization
  200. FreeDebugClient;
  201. end.