IdSysLog.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.5 2004.02.03 5:44:26 PM czhower
  18. Name changes
  19. Rev 1.4 1/21/2004 4:03:54 PM JPMugaas
  20. InitComponent
  21. Rev 1.3 15.9.2003 12:36:44 TPrami
  22. - Process information will be written in SendLogMessages if AUsePID
  23. is false
  24. Rev 1.2 15.9.2003 12:22:10 TPrami
  25. - AUsePID was not honored is SendLogMessage
  26. Rev 1.1 15.9.2003 11:05:26 TPrami
  27. - SendLogMessage was not sending Pid and name correctly. Now
  28. AText goes to the Content, not to the Text property
  29. Rev 1.0 11/13/2002 08:02:06 AM JPMugaas
  30. }
  31. unit IdSysLog;
  32. {
  33. Copyright the Indy pit crew
  34. Original Author: Stephane Grobety ([email protected])
  35. Release history:
  36. 09/19/01; J. Peter Mugaas
  37. divided SysLogMessage into this unit
  38. 08/09/01: Dev started
  39. }
  40. { ToDo: Somehow figure out how to make a bound port and bound IP property
  41. in UDP Client. This will probably require some changes to the Indy core
  42. units though. }
  43. interface
  44. {$i IdCompilerDefines.inc}
  45. uses IdAssignedNumbers, IdSocketHandle, IdSysLogMessage, IdUDPBase, IdUDPClient;
  46. type
  47. TIdSysLog = class(TIdUDPClient)
  48. protected
  49. function GetBinding: TIdSocketHandle; override;
  50. procedure InitComponent; override;
  51. public
  52. procedure SendLogMessage(const AMsg: TIdSysLogMessage;
  53. const AAutoTimeStamp: Boolean = true); overload;
  54. procedure SendLogMessage(const AMsg: String;
  55. const AFacility : TidSyslogFacility;
  56. const ASeverity: TIdSyslogSeverity); overload;
  57. procedure SendLogMessage(const AProcess: String; const AText : String;
  58. const AFacility : TidSyslogFacility;
  59. const ASeverity: TIdSyslogSeverity;
  60. const AUsePID : Boolean = False;
  61. const APID : Integer = -1); overload;
  62. published
  63. property Port default IdPORT_syslog;
  64. end;
  65. implementation
  66. uses
  67. IdGlobal, SysUtils;
  68. { TIdSysLog }
  69. procedure TIdSysLog.InitComponent;
  70. begin
  71. inherited InitComponent;
  72. Port := IdPORT_syslog;
  73. end;
  74. procedure TIdSysLog.SendLogMessage(const AMsg: TIdSyslogMessage; const AAutoTimeStamp: Boolean = true);
  75. var
  76. LEncoding: IIdTextEncoding;
  77. begin
  78. if AAutoTimeStamp then begin
  79. AMsg.TimeStamp := Now;
  80. end;
  81. LEncoding := IndyTextEncoding_8Bit;
  82. Send(AMsg.EncodeMessage, LEncoding{$IFDEF STRING_IS_ANSI}, LEncoding{$ENDIF});
  83. end;
  84. function TIdSysLog.GetBinding: TIdSocketHandle;
  85. //const FromPort = 514;
  86. begin
  87. Result := inherited GetBinding;
  88. // if Result.Port <> FromPort then
  89. // begin
  90. // {Recommened by RFC 3164 - Use 514 as to connect to the SysLog server}
  91. // Result.Port := FromPort;
  92. // Result.SetSockOpt(Id_SOL_SOCKET, Id_SO_REUSEADDR, Id_SO_True);
  93. // Result.Bind;
  94. // end;
  95. end;
  96. procedure TIdSysLog.SendLogMessage(const AMsg: String;
  97. const AFacility: TidSyslogFacility;
  98. const ASeverity: TIdSyslogSeverity);
  99. var
  100. LMsg : TIdSyslogMessage;
  101. begin
  102. LMsg := TIdSyslogMessage.Create(nil);
  103. try
  104. LMsg.Msg.Text := AMsg;
  105. LMsg.Facility := AFacility;
  106. LMsg.Severity := ASeverity;
  107. SendLogMessage(LMsg);
  108. finally
  109. FreeAndNil(LMsg);
  110. end;
  111. end;
  112. procedure TIdSysLog.SendLogMessage(const AProcess, AText: String;
  113. const AFacility: TidSyslogFacility;
  114. const ASeverity: TIdSyslogSeverity;
  115. const AUsePID: Boolean;
  116. const APID: Integer);
  117. var
  118. LMsg : TIdSyslogMessage;
  119. begin
  120. LMsg := TIdSyslogMessage.Create(nil);
  121. try
  122. LMsg.Msg.PIDAvailable := AUsePID;
  123. // <TP>
  124. // AUsePID was not honored
  125. LMsg.Msg.PIDAvailable := AUsePID;
  126. if AUsePID then begin
  127. LMsg.Msg.PID := APID;
  128. end;
  129. LMsg.Msg.Content := AText;
  130. // </TP>
  131. LMsg.Msg.Process := AProcess;
  132. LMsg.Facility := AFacility;
  133. LMsg.Severity := ASeverity;
  134. SendLogMessage(LMsg);
  135. finally
  136. FreeAndNil(LMsg);
  137. end;
  138. end;
  139. end.