eventlog.pp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. Cross-platform 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. {$mode objfpc}
  13. {$h+}
  14. unit eventlog;
  15. interface
  16. uses SysUtils,Classes;
  17. Type
  18. TEventType = (etCustom,etInfo,etWarning,etError,etDebug);
  19. TLogType = (ltSystem,ltFile);
  20. TEventLog = Class(TComponent)
  21. Private
  22. FEventIDOffset : DWord;
  23. FLogHandle : Pointer;
  24. FStream : TFileStream;
  25. FActive: Boolean;
  26. FIdentification: String;
  27. FDefaultEventType: TEventType;
  28. FLogtype: TLogType;
  29. FFileName: String;
  30. FTimeStampFormat: String;
  31. FCustomLogType: Word;
  32. procedure SetActive(const Value: Boolean);
  33. procedure SetIdentification(const Value: String);
  34. procedure SetlogType(const Value: TLogType);
  35. procedure ActivateLog;
  36. procedure DeActivateLog;
  37. procedure ActivateFileLog;
  38. procedure SetFileName(const Value: String);
  39. procedure ActivateSystemLog;
  40. function DefaultFileName: String;
  41. procedure WriteFileLog(EventType : TEventType; Msg: String);
  42. procedure WriteSystemLog(EventType: TEventType; Msg: String);
  43. procedure DeActivateFileLog;
  44. procedure DeActivateSystemLog;
  45. procedure CheckIdentification;
  46. function MapTypeToEvent(EventType: TEventType): DWord;
  47. Protected
  48. Procedure CheckInactive;
  49. Procedure EnsureActive;
  50. Public
  51. Destructor Destroy; override;
  52. Function EventTypeToString(E : TEventType) : String;
  53. Function RegisterMessageFile(AFileName : String) : Boolean; virtual;
  54. Function MapTypeToCategory(EventType : TEventType) : Word;
  55. Function MapTypeToEventID(EventType : TEventType) : DWord;
  56. Procedure Log (EventType : TEventType; Msg : String); {$ifndef fpc }Overload;{$endif}
  57. Procedure Log (EventType : TEventType; Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  58. Procedure Log (Msg : String); {$ifndef fpc }Overload;{$endif}
  59. Procedure Log (Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  60. Procedure Warning (Msg : String); {$ifndef fpc }Overload;{$endif}
  61. Procedure Warning (Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  62. Procedure Error (Msg : String); {$ifndef fpc }Overload;{$endif}
  63. Procedure Error (Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  64. Procedure Debug (Msg : String); {$ifndef fpc }Overload;{$endif}
  65. Procedure Debug (Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  66. Procedure Info (Msg : String); {$ifndef fpc }Overload;{$endif}
  67. Procedure Info (Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  68. Property Identification : String Read FIdentification Write SetIdentification;
  69. Property LogType : TLogType Read Flogtype Write SetlogType;
  70. Property Active : Boolean Read FActive write SetActive;
  71. Property DefaultEventType : TEventType Read FDEfaultEventType Write FDefaultEventType;
  72. Property FileName : String Read FFileName Write SetFileName;
  73. Property TimeStampFormat : String Read FTimeStampFormat Write FTimeStampFormat;
  74. Property CustomLogType : Word Read FCustomLogType Write FCustomLogType;
  75. Property EventIDOffset : DWord Read FEventIDOffset Write FEventIDOffset;
  76. End;
  77. ELogError = Class(Exception);
  78. Resourcestring
  79. SLogInfo = 'Info';
  80. SLogWarning = 'Warning';
  81. SLogError = 'Error';
  82. SLogDebug = 'Debug';
  83. SLogCustom = 'Custom (%d)';
  84. implementation
  85. {$i eventlog.inc}
  86. { TEventLog }
  87. Resourcestring
  88. SErrOperationNotAllowed = 'Operation not allowed when eventlog is active.';
  89. procedure TEventLog.CheckInactive;
  90. begin
  91. If Active then
  92. Raise ELogError.Create(SErrOperationNotAllowed);
  93. end;
  94. procedure TEventLog.Debug(Fmt: String; Args: array of const);
  95. begin
  96. Debug(Format(Fmt,Args));
  97. end;
  98. procedure TEventLog.Debug(Msg: String);
  99. begin
  100. Log(etDebug,Msg);
  101. end;
  102. procedure TEventLog.EnsureActive;
  103. begin
  104. If Not Active then
  105. Active:=True;
  106. end;
  107. procedure TEventLog.Error(Fmt: String; Args: array of const);
  108. begin
  109. Error(Format(Fmt,Args));
  110. end;
  111. procedure TEventLog.Error(Msg: String);
  112. begin
  113. Log(etError,Msg);
  114. end;
  115. procedure TEventLog.Info(Fmt: String; Args: array of const);
  116. begin
  117. Info(Format(Fmt,Args));
  118. end;
  119. procedure TEventLog.Info(Msg: String);
  120. begin
  121. Log(etInfo,Msg);
  122. end;
  123. procedure TEventLog.Log(Msg: String);
  124. begin
  125. Log(DefaultEventType,msg);
  126. end;
  127. procedure TEventLog.Log(EventType: TEventType; Fmt: String;
  128. Args: array of const);
  129. begin
  130. Log(EventType,Format(Fmt,Args));
  131. end;
  132. procedure TEventLog.Log(EventType: TEventType; Msg: String);
  133. begin
  134. EnsureActive;
  135. Case FlogType of
  136. ltFile : WriteFileLog(EventType,Msg);
  137. ltSystem : WriteSystemLog(EventType,Msg);
  138. end;
  139. end;
  140. procedure TEventLog.WriteFileLog(EventType : TEventType; Msg : String);
  141. Var
  142. S,TS,T : String;
  143. begin
  144. If FTimeStampFormat='' then
  145. FTimeStampFormat:='yyyy-mm-dd hh:nn:ss.zzz';
  146. TS:=FormatDateTime(FTimeStampFormat,Now);
  147. T:=EventTypeToString(EventType);
  148. S:=Format('%s [%s %s] %s%s',[Identification,TS,T,Msg,LineEnding]);
  149. FStream.Write(S[1],Length(S));
  150. end;
  151. procedure TEventLog.Log(Fmt: String; Args: array of const);
  152. begin
  153. Log(Format(Fmt,Args));
  154. end;
  155. procedure TEventLog.SetActive(const Value: Boolean);
  156. begin
  157. If Value<>FActive then
  158. begin
  159. If Value then
  160. ActivateLog
  161. else
  162. DeActivateLog;
  163. FActive:=Value;
  164. end;
  165. end;
  166. Procedure TEventLog.ActivateLog;
  167. begin
  168. Case FLogType of
  169. ltFile : ActivateFileLog;
  170. ltSystem : ActivateSystemLog;
  171. end;
  172. end;
  173. Procedure TEventLog.DeActivateLog;
  174. begin
  175. Case FLogType of
  176. ltFile : DeActivateFileLog;
  177. ltSystem : DeActivateSystemLog;
  178. end;
  179. end;
  180. Procedure TEventLog.ActivateFileLog;
  181. begin
  182. If (FFileName='') then
  183. FFileName:=DefaultFileName;
  184. // This will raise an exception if the file cannot be opened for writing !
  185. FStream:=TFileStream.Create(FFileName,fmCreate or fmShareDenyWrite);
  186. end;
  187. Procedure TEventLog.DeActivateFileLog;
  188. begin
  189. FStream.Free;
  190. FStream:=Nil;
  191. end;
  192. procedure TEventLog.SetIdentification(const Value: String);
  193. begin
  194. FIdentification := Value;
  195. end;
  196. procedure TEventLog.SetlogType(const Value: TLogType);
  197. begin
  198. CheckInactive;
  199. Flogtype := Value;
  200. end;
  201. procedure TEventLog.Warning(Fmt: String; Args: array of const);
  202. begin
  203. Warning(Format(Fmt,Args));
  204. end;
  205. procedure TEventLog.Warning(Msg: String);
  206. begin
  207. Log(etWarning,Msg);
  208. end;
  209. procedure TEventLog.SetFileName(const Value: String);
  210. begin
  211. CheckInactive;
  212. FFileName := Value;
  213. end;
  214. Procedure TEventLog.CheckIdentification;
  215. begin
  216. If (Identification='') then
  217. Identification:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
  218. end;
  219. Function TEventLog.EventTypeToString(E : TEventType) : String;
  220. begin
  221. Case E of
  222. etInfo : Result:=SLogInfo;
  223. etWarning : Result:=SLogWarning;
  224. etError : Result:=SLogError;
  225. etDebug : Result:=SLogDebug;
  226. etCustom : Result:=Format(SLogCustom,[CustomLogType]);
  227. end;
  228. end;
  229. destructor TEventLog.Destroy;
  230. begin
  231. Active:=False;
  232. inherited;
  233. end;
  234. end.
  235. {
  236. $Log$
  237. Revision 1.1 2003-02-19 20:25:16 michael
  238. + Added event log
  239. }