eventlog.inc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2003 by the Free Pascal development team
  4. OS/2 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. {$R-}
  12. const
  13. EventLogAvailable: boolean = false;
  14. No_Handle = cardinal ($FFFFFFFF);
  15. EventLogHandle: cardinal = No_Handle;
  16. sis_MMIOAddr = 0;
  17. sis_MEC_Table = 1;
  18. sis_Sys_Log = 2;
  19. lf_LogEnable = 1; { Logging enabled }
  20. lf_LogAvailable = 2; { Logging available }
  21. ErrLog_Service = 1;
  22. ErrLog_Version = 1;
  23. lf_Bit_ProcName = 1; {used to indicate whether the current error log}
  24. {entry packet contains space in which the error}
  25. {logging facility can place a long process name}
  26. {("on" indicates YES, "off" indicates NO) }
  27. lf_Bit_Origin_256 = 2; {used to indicate whether the current error log }
  28. {entry packet contains an 8 byte originator name}
  29. {or a 256 byte originator name ("on" indicates }
  30. {a 256 byte originator name, "off" indicates an }
  31. {8 byte originator name) }
  32. lf_Bit_DateTime = 4; {used to indicate that the caller has placed time}
  33. {and date values in the Error Log entry packet }
  34. {and does not wish to have those values modified }
  35. {during the logging process ("on" indicates that }
  36. {the error log entry packet already contains time}
  37. {and date values, "off" indicates the packet does}
  38. {not already contain time and date values) }
  39. lf_Bit_Suspend = 8;
  40. lf_Bit_Resume = 16;
  41. lf_Bit_Redirect = 32;
  42. lf_Bit_GetStatus = 64;
  43. lf_Bit_Register = 128;
  44. lf_Bit_Remote_Fail = 256;
  45. MaxDataSize = 3400;
  46. type
  47. Str3 = string [3];
  48. TLogRecord = record
  49. Len: word; { length of this record (including the Len field) }
  50. Rec_ID: word; { record ID }
  51. Status: cardinal; { record status bits (see lf_Bit_* constants) }
  52. Qualifier: array [1..4] of char; { qualifier tag }
  53. Reserved: cardinal;
  54. Time: cardinal; { hours, minutes, seconds, hundreds }
  55. Date: cardinal; { day, month, year (stored as word) }
  56. case byte of
  57. 0: (Data: array [1..MaxDataSize] of char);
  58. 1: (Originator256: array [0..255] of char;
  59. ProcessName_O256: array [1..260] of char;
  60. FormatDLLName_O256_ProcName: array [1..12] of char;
  61. Data_O256_ProcName: array [1..MaxDataSize] of char);
  62. 2: (Originator256b: array [0..255] of char;
  63. FormatDLLName_O256: array [1..12] of char;
  64. Data_O256: array [1..MaxDataSize] of char);
  65. 3: (Originator8: array [0..7] of char;
  66. ProcessName_O8: array [1..260] of char;
  67. FormatDLLName_O8_ProcName: array [1..12] of char;
  68. Data_O8_ProcName: array [1..MaxDataSize] of char);
  69. 4: (Originator8b: array [0..7] of char;
  70. FormatDLLName_O8: array [1..12] of char;
  71. Data_O8: array [1..MaxDataSize] of char);
  72. end;
  73. LogRecord = TLogRecord;
  74. PLogRecord = ^TLogRecord;
  75. TLogEntryRec = record
  76. Version: word; {this version is 1}
  77. Count: word; {number of log records in this buffer}
  78. LogRec: array [0..0] of TLogRecord; {repeated count times}
  79. end;
  80. LogEntryRec = TLogEntryRec;
  81. PLogEntryRec = ^TLogEntryRec;
  82. function DosQueryRASInfo (Index: cardinal; var PBuffer: pointer): longint;
  83. cdecl; external 'DOSCALLS' index 112;
  84. function LogOpen (var Handle: cardinal): longint; cdecl;
  85. external 'DOSCALLS' index 430;
  86. function LogClose (Handle: cardinal): longint; cdecl;
  87. external 'DOSCALLS' index 431;
  88. function LogAddEntries (Handle: cardinal; Service: cardinal;
  89. LogEntries: PLogEntryRec): longint; cdecl; external 'DOSCALLS' index 432;
  90. function LogAddEntries (Handle: cardinal; Service: cardinal;
  91. var LogEntries: TLogEntryRec): longint; cdecl; external 'DOSCALLS' index 432;
  92. function TEventLog.DefaultFileName: string;
  93. begin
  94. Result := GetEnvironmentVariable ('TEMP');
  95. if Result = '' then
  96. begin
  97. Result := GetEnvironmentVariable ('TMP');
  98. if Result = '' then Result := ExpandFileName ('.');
  99. end;
  100. Result := Result + DirectorySeparator +
  101. ChangeFileExt (ExtractFileName (ParamStr (0)), '.log');
  102. end;
  103. Resourcestring
  104. SErrNoSysLog = 'Could not open system log (error %d)';
  105. SErrLogFailed = 'Failed to log entry (error %d)';
  106. procedure TEventLog.ActivateSystemLog;
  107. var
  108. P: PWord;
  109. begin
  110. CheckIdentification;
  111. DosQueryRASInfo (sis_Sys_Log, P);
  112. EventLogAvailable := P^ and (lf_LogAvailable or lf_LogEnable)
  113. = (lf_LogAvailable or lf_LogEnable);
  114. if not (EventLogAvailable) then
  115. ActivateFileLog
  116. else
  117. if EventLogHandle = No_Handle then
  118. LogOpen (EventLogHandle);
  119. end;
  120. procedure TEventLog.DeactivateSystemLog;
  121. begin
  122. if EventLogAvailable then
  123. if EventLogHandle <> No_Handle then
  124. begin
  125. LogClose (EventLogHandle);
  126. EventLogHandle := No_Handle;
  127. end
  128. else
  129. DeactivateFileLog;
  130. end;
  131. procedure TEventLog.WriteSystemLog (EventType: TEventType; Msg: string);
  132. const
  133. WinET: array [TEventType] of Str3 = ('USR', 'INF', 'WRN', 'ERR', 'DBG');
  134. var
  135. P: PLogEntryRec;
  136. S: string;
  137. Cnt, TSize, DSize: cardinal;
  138. W: word;
  139. begin
  140. if not (EventLogAvailable) then
  141. WriteFileLog (EventType, Msg)
  142. else
  143. begin
  144. S := Copy (Identification, 1, 256);
  145. TSize := Length (Msg);
  146. Cnt := Succ (Pred (TSize) div MaxDataSize);
  147. if Cnt > high (word) then
  148. begin
  149. Cnt := high (word);
  150. TSize := Cnt * MaxDataSize;
  151. end;
  152. DSize := TSize + 4 + Cnt * (24 + 256 + 260 + 12);
  153. GetMem (P, DSize);
  154. FillChar (P^, DSize, #0);
  155. with P^ do
  156. begin
  157. Version := ErrLog_Version;
  158. Count := Cnt;
  159. for W := 0 to Pred (Cnt) do
  160. with LogRec [W] do
  161. begin
  162. if (W = Pred (Cnt)) and (TSize mod MaxDataSize <> 0) then
  163. begin
  164. Len := 24 + 256 + 260 + 12 + TSize mod MaxDataSize;
  165. Move (Msg [Succ (W * MaxDataSize)],
  166. Data_O256_ProcName [1], TSize mod MaxDataSize);
  167. end
  168. else
  169. begin
  170. Len := 24 + 256 + 260 + 12 + MaxDataSize;
  171. Move (Msg [Succ (W * MaxDataSize)],
  172. Data_O256_ProcName [1], MaxDataSize);
  173. end;
  174. Rec_ID := $4650; { FP }
  175. Status := lf_Bit_ProcName or lf_Bit_Origin_256;
  176. Move (WinET [EventType] [1], Qualifier,
  177. Length (WinET [EventType]));
  178. Move (S [1], Originator256 [0], Length (S));
  179. end;
  180. end;
  181. LogAddEntries (EventLogHandle, ErrLog_Service, P);
  182. FreeMem (P, DSize);
  183. end;
  184. end;
  185. Function TEventLog.RegisterMessageFile(AFileName : String) : Boolean;
  186. begin
  187. Result:=True;
  188. end;
  189. function TEventLog.MapTypeToCategory(EventType: TEventType): Word;
  190. begin
  191. Result:=0;
  192. If (EventType=ETCustom) then
  193. DoGetCustomEventCategory(Result);
  194. end;
  195. function TEventLog.MapTypeToEventID(EventType: TEventType): DWord;
  196. begin
  197. Result:=0;
  198. If (EventType=ETCustom) then
  199. DoGetCustomEventID(Result);
  200. end;
  201. function TEventLog.MapTypeToEvent(EventType: TEventType): DWord;
  202. begin
  203. If EventType=etCustom Then
  204. begin
  205. Result:=CustomLogType;
  206. DoGetCustomEvent(Result);
  207. end
  208. else
  209. Result := ord (EventType);
  210. end;