eventlog.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 2003 by the Free Pascal development team
  5. Win32 implementation part of event logging facility.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. uses windows,registry;
  13. Function TEventLog.DefaultFileName : String;
  14. begin
  15. Result:=ChangeFileExt(Paramstr(0),'.log');
  16. end;
  17. Resourcestring
  18. SErrNoSysLog = 'Could not open system log (error %d)';
  19. SErrLogFailed = 'Failed to log entry (error %d)';
  20. Procedure TEventLog.ActivateSystemLog;
  21. begin
  22. CheckIdentification;
  23. FLogHandle := Pointer(OpenEventLog(Nil,Pchar(Identification)));
  24. If FLogHandle=Nil then
  25. Raise ELogError.CreateFmt(SErrNoSysLog,[GetLastError]);
  26. end;
  27. Procedure TEventLog.DeActivateSystemLog;
  28. begin
  29. CloseEventLog(Cardinal(FLogHandle));
  30. end;
  31. {
  32. function ReportEvent(hEventLog: THandle; wType, wCategory: Word;
  33. dwEventID: DWORD; lpUserSid: Pointer; wNumStrings: Word;
  34. dwDataSize: DWORD; lpStrings, lpRawData: Pointer): BOOL; stdcall;
  35. }
  36. procedure TEventLog.WriteSystemLog(EventType : TEventType; Msg : String);
  37. Var
  38. P : PChar;
  39. I : Integer;
  40. FCategory : Word;
  41. FEventID : DWord;
  42. FEventType : Word;
  43. begin
  44. FCategory:=MapTypeToCategory(EventType);
  45. FEventID:=MapTypeToEventID(EventType);
  46. FEventType:=MapTypeToEvent(EventType);
  47. P:=PChar(Msg);
  48. If Not ReportEvent(Cardinal(FLogHandle),FEventType,FCategory,FEventID,Nil,1,0,@P,Nil) then
  49. begin
  50. I:=GetLastError;
  51. Raise ELogError.CreateFmt(SErrLogFailed,[I]);
  52. end;
  53. end;
  54. Function TEventLog.RegisterMessageFile(AFileName : String) : Boolean;
  55. Const
  56. SKeyEventLog = '\SYSTEM\CurrentControlSet\Services\EventLog\Application\%s';
  57. SKeyCategoryCount = 'CategoryCount';
  58. SKeyEventMessageFile = 'EventMessageFile';
  59. SKeyCategoryMessageFile = 'CategoryMessageFile';
  60. SKeyTypesSupported = 'TypesSupported';
  61. Var
  62. ELKey : String;
  63. R : TRegistry;
  64. begin
  65. CheckIdentification;
  66. If AFileName='' then
  67. AFileName:=ParamStr(0);
  68. R:=TRegistry.Create;
  69. Try
  70. R.RootKey:=HKEY_LOCAL_MACHINE;
  71. ELKey:=Format(SKeyEventLog,[IDentification]);
  72. Result:=R.OpenKey(ELKey,True);
  73. If Result then
  74. try
  75. R.WriteInteger(SKeyCategoryCount,4);
  76. R.WriteString(SKeyCategoryMessageFile,AFileName);
  77. R.WriteString(SKeyEventMessageFile,AFileName);
  78. R.WriteInteger(SKeyTypesSupported,7);
  79. except
  80. Result:=False;
  81. end
  82. Finally
  83. R.Free;
  84. end;
  85. end;
  86. function TEventLog.MapTypeToCategory(EventType: TEventType): Word;
  87. begin
  88. If (EventType=ETCustom) then
  89. DoGetCustomEventCategory(Result)
  90. else
  91. Result:=Ord(EventType);
  92. If Result=0 then
  93. Result:=1;
  94. end;
  95. function TEventLog.MapTypeToEventID(EventType: TEventType): DWord;
  96. begin
  97. If (EventType=ETCustom) then
  98. DoGetCustomEventID(Result)
  99. else
  100. begin
  101. If (FEventIDOffset=0) then
  102. FEventIDOffset:=1000;
  103. Result:=FEventIDOffset+Ord(EventType);
  104. end;
  105. end;
  106. function TEventLog.MapTypeToEvent(EventType: TEventType): DWord;
  107. Const
  108. EVENTLOG_SUCCESS=0;
  109. WinET : Array[TEventType] of word = (EVENTLOG_SUCCESS,
  110. EVENTLOG_INFORMATION_TYPE,
  111. EVENTLOG_WARNING_TYPE,EVENTLOG_ERROR_TYPE,
  112. EVENTLOG_AUDIT_SUCCESS);
  113. begin
  114. If EventType=etCustom Then
  115. begin
  116. If CustomLogType=0 then
  117. CustomLogType:=EVENTLOG_SUCCESS;
  118. Result:=CustomLogType;
  119. DoGetCustomEvent(Result);
  120. end
  121. else
  122. Result:=WinET[EventType];
  123. end;