eventlog.pp 8.6 KB

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