IdSysLog.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 10357: IdSysLog.pas
  11. {
  12. { Rev 1.2 15.9.2003 12:34:08 TPrami
  13. { - if not AUsePID now Process information will be written also
  14. }
  15. {
  16. { Rev 1.0 2002.11.12 10:54:32 PM czhower
  17. }
  18. unit IdSysLog;
  19. // Copyright the Indy pit crew
  20. // Original Author: Stephane Grobety ([email protected])
  21. // Release history:
  22. //
  23. // 09/19/01; J. Peter Mugaas
  24. // devided SysLogMessage into this unit
  25. // 08/09/01: Dev started
  26. {ToDo: Somehow figure out how to make a bound port and bound IP property
  27. in UDP Client. This will probably require some changes to the Indy core units
  28. though.
  29. }
  30. interface
  31. uses
  32. Classes, IdAssignedNumbers, IdSocketHandle, IdSysLogMessage, IdUDPBase, IdUDPClient;
  33. type
  34. TIdSysLog = class(TIdUDPClient)
  35. protected
  36. function GetBinding: TIdSocketHandle; override;
  37. public
  38. constructor Create(AOwner: TComponent); override;
  39. procedure SendMsg(const AMsg: TIdSysLogMessage; const AAutoTimeStamp: Boolean = True); overload;
  40. procedure SendMsg(const AMsg: String; const AFacility : TidSyslogFacility; const ASeverity: TIdSyslogSeverity); overload;
  41. procedure SendMsg(const AProcess: String; const AText : String; const AFacility : TidSyslogFacility;
  42. const ASeverity: TIdSyslogSeverity; const AUsePID : Boolean = False; const APID : Integer = -1); overload;
  43. published
  44. property Port default IdPORT_syslog;
  45. end;
  46. implementation
  47. uses
  48. IdGlobal, SysUtils, IdStackConsts;
  49. { TIdSysLog }
  50. constructor TIdSysLog.Create(AOwner: TComponent);
  51. begin
  52. inherited Create(AOwner);
  53. Port := IdPORT_syslog;
  54. end;
  55. procedure TIdSysLog.SendMsg(const AMsg: TIdSyslogMessage; const AAutoTimeStamp: Boolean = True);
  56. begin
  57. if AAutoTimeStamp then begin
  58. AMsg.TimeStamp := Now;
  59. end;
  60. Send(AMsg.EncodeMessage);
  61. end;
  62. function TIdSysLog.GetBinding: TIdSocketHandle;
  63. const
  64. FromPort = 514;
  65. begin
  66. Result := inherited GetBinding;
  67. // if Result.Port <> FromPort then
  68. // begin
  69. // {Recommened by RFC 3164 - Use 514 as to connect to the SysLog server}
  70. // Result.Port := FromPort;
  71. // Result.SetSockOpt(Id_SOL_SOCKET, Id_SO_REUSEADDR, PChar(@Id_SO_True), SizeOf(Id_SO_True));
  72. // Result.Bind;
  73. // end;
  74. end;
  75. procedure TIdSysLog.SendMsg(const AMsg: String; const AFacility: TidSyslogFacility;
  76. const ASeverity: TIdSyslogSeverity);
  77. var
  78. LMsg : TIdSyslogMessage;
  79. begin
  80. LMsg := TIdSyslogMessage.Create(nil);
  81. try
  82. LMsg.Msg.Text := AMsg;
  83. LMsg.Facility := AFacility;
  84. LMsg.Severity := ASeverity;
  85. SendMsg(LMsg);
  86. finally
  87. FreeAndNil(LMsg);
  88. end;
  89. end;
  90. procedure TIdSysLog.SendMsg(const AProcess, AText: String;
  91. const AFacility: TidSyslogFacility; const ASeverity: TIdSyslogSeverity;
  92. const AUsePID: Boolean; const APID: Integer);
  93. var
  94. LMsg : TIdSyslogMessage;
  95. begin
  96. LMsg := TIdSyslogMessage.Create(nil);
  97. try
  98. LMsg.Msg.PIDAvailable := AUsePID;
  99. LMsg.Msg.Process := AProcess;
  100. // <TP>
  101. // AUsePID was not honored
  102. LMsg.Msg.PIDAvailable := AUsePID;
  103. if AUsePID then begin
  104. LMsg.Msg.PID := APID;
  105. LMsg.Msg.Content := AText;
  106. end else begin
  107. LMsg.Msg.Content := AText;
  108. end;
  109. // </TP>
  110. LMsg.Facility := AFacility;
  111. LMsg.Severity := ASeverity;
  112. SendMsg(LMsg);
  113. finally
  114. FreeAndNil(LMsg);
  115. end;
  116. end;
  117. end.