eventlog.inc 3.6 KB

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