synadbg.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.001.002 |
  3. |==============================================================================|
  4. | Content: Socket debug tools |
  5. |==============================================================================|
  6. | Copyright (c)2008-2011, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2008-2011. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract(Socket debug tools)
  45. Routines for help with debugging of events on the Sockets.
  46. }
  47. {$IFDEF UNICODE}
  48. {$WARN IMPLICIT_STRING_CAST OFF}
  49. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  50. {$ENDIF}
  51. unit synadbg;
  52. interface
  53. uses
  54. blcksock, synsock, synautil, classes, sysutils, synafpc;
  55. type
  56. TSynaDebug = class(TObject)
  57. class procedure HookStatus(Sender: TObject; Reason: THookSocketReason; const Value: string);
  58. class procedure HookMonitor(Sender: TObject; Writing: Boolean; const Buffer: TMemory; Len: Integer);
  59. end;
  60. procedure AppendToLog(const value: Ansistring);
  61. var
  62. LogFile: string;
  63. implementation
  64. procedure AppendToLog(const value: Ansistring);
  65. var
  66. st: TFileStream;
  67. s: string;
  68. h, m, ss, ms: word;
  69. dt: Tdatetime;
  70. begin
  71. if fileexists(LogFile) then
  72. st := TFileStream.Create(LogFile, fmOpenReadWrite or fmShareDenyWrite)
  73. else
  74. st := TFileStream.Create(LogFile, fmCreate or fmShareDenyWrite);
  75. try
  76. st.Position := st.Size;
  77. dt := now;
  78. decodetime(dt, h, m, ss, ms);
  79. s := formatdatetime('yyyymmdd-hhnnss', dt) + format('.%.3d', [ms]) + ' ' + value;
  80. WriteStrToStream(st, s);
  81. finally
  82. st.free;
  83. end;
  84. end;
  85. class procedure TSynaDebug.HookStatus(Sender: TObject; Reason: THookSocketReason; const Value: string);
  86. var
  87. s: string;
  88. begin
  89. case Reason of
  90. HR_ResolvingBegin:
  91. s := 'HR_ResolvingBegin';
  92. HR_ResolvingEnd:
  93. s := 'HR_ResolvingEnd';
  94. HR_SocketCreate:
  95. s := 'HR_SocketCreate';
  96. HR_SocketClose:
  97. s := 'HR_SocketClose';
  98. HR_Bind:
  99. s := 'HR_Bind';
  100. HR_Connect:
  101. s := 'HR_Connect';
  102. HR_CanRead:
  103. s := 'HR_CanRead';
  104. HR_CanWrite:
  105. s := 'HR_CanWrite';
  106. HR_Listen:
  107. s := 'HR_Listen';
  108. HR_Accept:
  109. s := 'HR_Accept';
  110. HR_ReadCount:
  111. s := 'HR_ReadCount';
  112. HR_WriteCount:
  113. s := 'HR_WriteCount';
  114. HR_Wait:
  115. s := 'HR_Wait';
  116. HR_Error:
  117. s := 'HR_Error';
  118. else
  119. s := '-unknown-';
  120. end;
  121. s := inttohex(PtrInt(Sender), 8) + s + ': ' + value + CRLF;
  122. AppendToLog(s);
  123. end;
  124. class procedure TSynaDebug.HookMonitor(Sender: TObject; Writing: Boolean; const Buffer: TMemory; Len: Integer);
  125. var
  126. s, d: Ansistring;
  127. begin
  128. setlength(s, len);
  129. move(Buffer^, pointer(s)^, len);
  130. if writing then
  131. d := '-> '
  132. else
  133. d := '<- ';
  134. s :=inttohex(PtrInt(Sender), 8) + d + s + CRLF;
  135. AppendToLog(s);
  136. end;
  137. initialization
  138. begin
  139. Logfile := changefileext(paramstr(0), '.slog');
  140. end;
  141. end.