eventlog.inc 8.4 KB

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