IdSysLog.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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
  46. Classes,
  47. IdAssignedNumbers, IdSocketHandle, IdSysLogMessage, IdUDPBase, IdUDPClient;
  48. type
  49. TIdSysLog = class(TIdUDPClient)
  50. protected
  51. function GetBinding: TIdSocketHandle; override;
  52. public
  53. constructor Create(AOwner: TComponent); override;
  54. procedure SendLogMessage(const AMsg: TIdSysLogMessage;
  55. const AAutoTimeStamp: Boolean = true); overload;
  56. procedure SendLogMessage(const AMsg: String;
  57. const AFacility : TidSyslogFacility;
  58. const ASeverity: TIdSyslogSeverity); overload;
  59. procedure SendLogMessage(const AProcess: String; const AText : String;
  60. const AFacility : TidSyslogFacility;
  61. const ASeverity: TIdSyslogSeverity;
  62. const AUsePID : Boolean = False;
  63. const APID : Integer = -1); overload;
  64. published
  65. property Port default IdPORT_syslog;
  66. end;
  67. implementation
  68. uses
  69. IdGlobal, SysUtils;
  70. { TIdSysLog }
  71. constructor TIdSysLog.Create(AOwner: TComponent);
  72. begin
  73. inherited Create(AOwner);
  74. Port := IdPORT_syslog;
  75. end;
  76. procedure TIdSysLog.SendLogMessage(const AMsg: TIdSyslogMessage; const AAutoTimeStamp: Boolean = true);
  77. begin
  78. if AAutoTimeStamp then begin
  79. AMsg.TimeStamp := Now;
  80. end;
  81. Send(AMsg.EncodeMessage, IndyTextEncoding_8Bit);
  82. end;
  83. function TIdSysLog.GetBinding: TIdSocketHandle;
  84. //const FromPort = 514;
  85. begin
  86. Result := inherited GetBinding;
  87. // if Result.Port <> FromPort then
  88. // begin
  89. // {Recommened by RFC 3164 - Use 514 as to connect to the SysLog server}
  90. // Result.Port := FromPort;
  91. // Result.SetSockOpt(Id_SOL_SOCKET, Id_SO_REUSEADDR, Id_SO_True);
  92. // Result.Bind;
  93. // end;
  94. end;
  95. procedure TIdSysLog.SendLogMessage(const AMsg: String;
  96. const AFacility: TidSyslogFacility;
  97. const ASeverity: TIdSyslogSeverity);
  98. var
  99. LMsg : TIdSyslogMessage;
  100. begin
  101. LMsg := TIdSyslogMessage.Create(nil);
  102. try
  103. LMsg.Msg.Text := AMsg;
  104. LMsg.Facility := AFacility;
  105. LMsg.Severity := ASeverity;
  106. SendLogMessage(LMsg);
  107. finally
  108. LMsg.Free;
  109. end;
  110. end;
  111. procedure TIdSysLog.SendLogMessage(const AProcess, AText: String;
  112. const AFacility: TidSyslogFacility;
  113. const ASeverity: TIdSyslogSeverity;
  114. const AUsePID: Boolean;
  115. const APID: Integer);
  116. var
  117. LMsg : TIdSyslogMessage;
  118. begin
  119. LMsg := TIdSyslogMessage.Create(nil);
  120. try
  121. LMsg.Msg.PIDAvailable := AUsePID;
  122. // <TP>
  123. // AUsePID was not honored
  124. LMsg.Msg.PIDAvailable := AUsePID;
  125. if AUsePID then begin
  126. LMsg.Msg.PID := APID;
  127. end;
  128. LMsg.Msg.Content := AText;
  129. // </TP>
  130. LMsg.Msg.Process := AProcess;
  131. LMsg.Facility := AFacility;
  132. LMsg.Severity := ASeverity;
  133. SendLogMessage(LMsg);
  134. finally
  135. LMsg.Free;
  136. end;
  137. end;
  138. end.