eventlog.pp 8.4 KB

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