dbugsrv.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. {
  2. Make sure to set your project's options with, CompilerOptions --> Target "-o" -->Filename Value="fpcdebugserver",
  3. i.e. the executable name must be the same as the client's const named dbugmsg.DebugServerID.
  4. }
  5. program dbugsrv;
  6. {$MODE OBJFPC}
  7. {$H+}
  8. {$APPTYPE CONSOLE}
  9. uses
  10. classes,SysUtils,simpleipc,dbugmsg,strutils;
  11. Type
  12. { THelperToWrite }
  13. THelperToWrite = class
  14. private
  15. Class var StrLogFilename: string;
  16. Class procedure WriteLnAllParams;
  17. Class procedure InitParamsDependencies;
  18. { methods which override standard Write and WriteLn of the console output }
  19. Class procedure DoWrite(const aBuffer: string);
  20. Class procedure DoWrite(var aBuffer: string; const aMinimumFieldWidthIndent: integer); overload;
  21. Class procedure DoWriteLn(const aBuffer: string);
  22. { methods which write in a log file, too }
  23. Class procedure WriteNowThisLineInLog(aBuffer: string);
  24. Class procedure WriteLnNowThisLineInLog(aBuffer: string);
  25. Class function ReplaceSpecialCharsInLog(const aBuffer: string): string;
  26. public
  27. end;
  28. Var
  29. Srv : TSimpleIPCServer;
  30. Msg : TDebugMessage;
  31. StrBuffer : string = '';
  32. ObjFileStream : TFileStream = Nil;
  33. class procedure THelperToWrite.WriteLnAllParams;
  34. Var
  35. iNumParam: integer;
  36. sBuffer: string;
  37. begin
  38. sBuffer := 'ParamCount='+IntToStr(ParamCount)+LineEnding;
  39. for iNumParam := 0 to ParamCount do
  40. sBuffer := IfThen(iNumParam<>ParamCount, sBuffer+'ParamStr('+IntToStr(iNumParam)+') = "'+ParamStr(iNumParam)+'"'+LineEnding, sBuffer+'ParamStr('+IntToStr(iNumParam)+') = "'+ParamStr(iNumParam)+'"');
  41. THelperToWrite.DoWriteLn(sBuffer);
  42. end;
  43. class procedure THelperToWrite.InitParamsDependencies;
  44. begin
  45. If (ParamCount<>0) then
  46. if ParamStr(1)<>'' then begin {ord. params: 1st is a log filename}
  47. THelperToWrite.StrLogFilename:= ParamStr(1);
  48. ObjFileStream:= TFileStream.Create(THelperToWrite.StrLogFilename, fmCreate or fmOpenWrite or fmShareDenyWrite);
  49. ObjFileStream.Position:= 0;
  50. end;
  51. end;
  52. class procedure THelperToWrite.DoWrite(const aBuffer: string);
  53. begin
  54. Write(aBuffer);
  55. if Assigned(ObjFileStream) then THelperToWrite.WriteNowThisLineInLog(StrBuffer);
  56. end;
  57. class procedure THelperToWrite.DoWrite(var aBuffer: string; const aMinimumFieldWidthIndent: integer);
  58. begin
  59. Write(aBuffer:aMinimumFieldWidthIndent,' ');
  60. if Assigned(ObjFileStream) then THelperToWrite.WriteNowThisLineInLog(StrBuffer);
  61. end;
  62. class procedure THelperToWrite.DoWriteLn(const aBuffer: string);
  63. begin
  64. WriteLn(aBuffer);
  65. if Assigned(ObjFileStream) then THelperToWrite.WriteLnNowThisLineInLog(aBuffer+LineEnding)
  66. end;
  67. class procedure THelperToWrite.WriteNowThisLineInLog(aBuffer: string);
  68. var
  69. sBuffer: string;
  70. begin
  71. sBuffer:= THelperToWrite.ReplaceSpecialCharsInLog(aBuffer);
  72. ObjFileStream.Write(sBuffer[1],length(sBuffer));
  73. end;
  74. class procedure THelperToWrite.WriteLnNowThisLineInLog(aBuffer: string);
  75. var
  76. sBuffer: string;
  77. begin
  78. aBuffer:= ' '{sep. each field of the msg-record}+aBuffer+LineEnding;
  79. sBuffer:= THelperToWrite.ReplaceSpecialCharsInLog(aBuffer);
  80. ObjFileStream.Write(sBuffer[1],length(sBuffer));
  81. end;
  82. class function THelperToWrite.ReplaceSpecialCharsInLog(const aBuffer: string): string;
  83. begin
  84. Result := StringsReplace(aBuffer, [LineEnding+LineEnding], [LineEnding], [rfReplaceAll]);
  85. end;
  86. ResourceString
  87. SWelcomeOnSrv = 'IPC server started. Listening for debug messages:';
  88. begin
  89. Srv:=TSimpleIPCServer.Create(Nil);
  90. Try
  91. Srv.ServerID:=DebugServerID;
  92. Srv.Global:=True;
  93. Srv.Active:=True;
  94. Srv.StartServer;
  95. THelperToWrite.InitParamsDependencies;
  96. THelperToWrite.WriteLnAllParams;
  97. StrBuffer:=SWelcomeOnSrv;
  98. THelperToWrite.DoWriteLn(StrBuffer);
  99. Repeat
  100. If Srv.PeekMessage(1,True) then
  101. begin
  102. Srv.MsgData.Seek(0,soFrombeginning);
  103. ReadDebugMessageFromStream(Srv.MsgData,MSg);
  104. StrBuffer:=FormatDateTime('hh:nn:ss.zzz',Msg.MsgTimeStamp)+': ';
  105. THelperToWrite.DoWrite(StrBuffer);
  106. StrBuffer:=DebugMessageName(MSg.MsgType);
  107. THelperToWrite.DoWrite(StrBuffer,12);
  108. StrBuffer:=Msg.Msg;
  109. THelperToWrite.DoWriteLn(StrBuffer);
  110. end
  111. else
  112. Sleep(10);
  113. Until False;
  114. Finally
  115. if Assigned(ObjFileStream) then
  116. ObjFileStream.Free;
  117. Srv.Free;
  118. end;
  119. end.