home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / unity / d23456 / SYNAPSE.ZIP / source / lib / SLogSend.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-09-09  |  5.0 KB  |  165 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 001.000.000 |
  3. |==============================================================================|
  4. | Content: SysLog client                                                       |
  5. |==============================================================================|
  6. | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
  7. | (the "License"); you may not use this file except in compliance with the     |
  8. | License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
  9. |                                                                              |
  10. | Software distributed under the License is distributed on an "AS IS" basis,   |
  11. | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
  12. | the specific language governing rights and limitations under the License.    |
  13. |==============================================================================|
  14. | The Original Code is Synapse Delphi Library.                                 |
  15. |==============================================================================|
  16. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  17. | Portions created by Lukas Gebauer are Copyright (c)2001.                     |
  18. | All Rights Reserved.                                                         |
  19. |==============================================================================|
  20. | Contributor(s):                                                              |
  21. |==============================================================================|
  22. | History: see HISTORY.HTM from distribution package                           |
  23. |          (Found at URL: http://www.ararat.cz/synapse/)                       |
  24. |==============================================================================}
  25.  
  26. // RFC-3164
  27.  
  28. {$Q-}
  29. {$WEAKPACKAGEUNIT ON}
  30.  
  31. unit SLogSend;
  32.  
  33. interface
  34.  
  35. uses
  36.   SysUtils, Classes,
  37.   blcksock, SynaUtil;
  38.  
  39. const
  40.   cSysLogProtocol = '514';
  41.  
  42.   FCL_Kernel = 0;
  43.   FCL_UserLevel = 1;
  44.   FCL_MailSystem = 2;
  45.   FCL_System = 3;
  46.   FCL_Security = 4;
  47.   FCL_Syslogd = 5;
  48.   FCL_Printer = 6;
  49.   FCL_News = 7;
  50.   FCL_UUCP = 8;
  51.   FCL_Clock = 9;
  52.   FCL_Authorization = 10;
  53.   FCL_FTP = 11;
  54.   FCL_NTP = 12;
  55.   FCL_LogAudit = 13;
  56.   FCL_LogAlert = 14;
  57.   FCL_Time = 15;
  58.   FCL_Local0 = 16;
  59.   FCL_Local1 = 17;
  60.   FCL_Local2 = 18;
  61.   FCL_Local3 = 19;
  62.   FCL_Local4 = 20;
  63.   FCL_Local5 = 21;
  64.   FCL_Local6 = 22;
  65.   FCL_Local7 = 23;
  66.  
  67. type
  68.   TSyslogSeverity = (Emergency, Alert, Critical, Error, Warning, Notice, Info,
  69.     Debug);
  70.  
  71.   TSyslogSend = class(TObject)
  72.   private
  73.     FSyslogHost: string;
  74.     FSyslogPort: string;
  75.     FSock: TUDPBlockSocket;
  76.     FFacility: Byte;
  77.     FSeverity: TSyslogSeverity;
  78.     FTag: string;
  79.     FMessage: string;
  80.   public
  81.     constructor Create;
  82.     destructor Destroy; override;
  83.     function DoIt: Boolean;
  84.   published
  85.     property SyslogHost: string read FSyslogHost Write FSyslogHost;
  86.     property SyslogPort: string read FSyslogPort Write FSyslogPort;
  87.     property Facility: Byte read FFacility Write FFacility;
  88.     property Severity: TSyslogSeverity read FSeverity Write FSeverity;
  89.     property Tag: string read FTag Write FTag;
  90.     property LogMessage: string read FMessage Write FMessage;
  91.   end;
  92.  
  93. function ToSysLog(const SyslogServer: string; Facil: Byte;
  94.   Sever: TSyslogSeverity; const Content: string): Boolean;
  95.  
  96. implementation
  97.  
  98. constructor TSyslogSend.Create;
  99. begin
  100.   inherited Create;
  101.   FSock := TUDPBlockSocket.Create;
  102.   FSock.CreateSocket;
  103.   FSyslogHost := cLocalhost;
  104.   FSyslogPort := cSysLogProtocol;
  105.   FFacility := FCL_Local0;
  106.   FSeverity := Debug;
  107.   FTag := ExtractFileName(ParamStr(0));
  108.   FMessage := '';
  109. end;
  110.  
  111. destructor TSyslogSend.Destroy;
  112. begin
  113.   FSock.Free;
  114.   inherited Destroy;
  115. end;
  116.  
  117. function TSyslogSend.DoIt: Boolean;
  118. var
  119.   Buf: string;
  120.   S: string;
  121.   L: TStringList;
  122. begin
  123.   Result := False;
  124.   Buf := '<' + IntToStr((FFacility * 8) + Ord(FSeverity)) + '>';
  125.   Buf := Buf + CDateTime(now) + ' ';
  126.   L := TStringList.Create;
  127.   try
  128.     FSock.ResolveNameToIP(FSock.Localname, L);
  129.     if L.Count < 1 then
  130.       S := '0.0.0.0'
  131.     else
  132.       S := L[0];
  133.   finally
  134.     L.Free;
  135.   end;
  136.   Buf := Buf + S + ' ';
  137.   Buf := Buf + Tag + ': ' + FMessage;
  138.   if Length(Buf) <= 1024 then
  139.   begin
  140.     FSock.Connect(FSyslogHost, FSyslogPort);
  141.     FSock.SendString(Buf);
  142.     Result := FSock.LastError = 0;
  143.   end;
  144. end;
  145.  
  146. {==============================================================================}
  147.  
  148. function ToSysLog(const SyslogServer: string; Facil: Byte;
  149.   Sever: TSyslogSeverity; const Content: string): Boolean;
  150. begin
  151.   Result := False;
  152.   with TSyslogSend.Create do
  153.     try
  154.       SyslogHost :=SyslogServer;
  155.       Facility := Facil;
  156.       Severity := Sever;
  157.       LogMessage := Content;
  158.       Result := DoIt;
  159.     finally
  160.       Free;
  161.     end;
  162. end;
  163.  
  164. end.
  165.