eventlog.pp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. TLogType = (ltSystem,ltFile);
  19. TLogCodeEvent = Procedure (Sender : TObject; Var Code : DWord) of Object;
  20. TLogCategoryEvent = Procedure (Sender : TObject; Var Code : Word) of Object;
  21. TEventLog = Class(TComponent)
  22. Private
  23. fAppendContent : Boolean;
  24. FEventIDOffset : DWord;
  25. FLogHandle : Pointer;
  26. FStream : TFileStream;
  27. FActive: Boolean;
  28. FRaiseExceptionOnError: 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; const Msg: String);
  48. procedure WriteSystemLog(EventType: TEventType; const 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. Function UnRegisterMessageFile : Boolean; virtual;
  66. Procedure Log (EventType : TEventType; const Msg : String); {$ifndef fpc }Overload;{$endif}
  67. Procedure Log (EventType : TEventType; const Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  68. Procedure Log (const Msg : String); {$ifndef fpc }Overload;{$endif}
  69. Procedure Log (const Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  70. Procedure Warning (const Msg : String); {$ifndef fpc }Overload;{$endif}
  71. Procedure Warning (const Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  72. Procedure Error (const Msg : String); {$ifndef fpc }Overload;{$endif}
  73. Procedure Error (const Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  74. Procedure Debug (const Msg : String); {$ifndef fpc }Overload;{$endif}
  75. Procedure Debug (const Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  76. Procedure Info (const Msg : String); {$ifndef fpc }Overload;{$endif}
  77. Procedure Info (const Fmt : String; Args : Array of const); {$ifndef fpc }Overload;{$endif}
  78. Published
  79. Property AppendContent : Boolean Read fAppendContent Write fAppendContent;
  80. Property Identification : String Read FIdentification Write SetIdentification;
  81. Property LogType : TLogType Read Flogtype Write SetlogType;
  82. Property Active : Boolean Read FActive write SetActive;
  83. Property RaiseExceptionOnError : Boolean Read FRaiseExceptionOnError Write FRaiseExceptionOnError;
  84. Property DefaultEventType : TEventType Read FDEfaultEventType Write FDefaultEventType;
  85. Property FileName : String Read FFileName Write SetFileName;
  86. Property TimeStampFormat : String Read FTimeStampFormat Write FTimeStampFormat;
  87. Property CustomLogType : Word Read FCustomLogType Write FCustomLogType;
  88. Property EventIDOffset : DWord Read FEventIDOffset Write FEventIDOffset;
  89. Property OnGetCustomCategory : TLogCategoryEvent Read FOnGetCustomCategory Write FOnGetCustomCategory;
  90. Property OnGetCustomEventID : TLogCodeEvent Read FOnGetCustomEventID Write FOnGetCustomEventID;
  91. Property OnGetCustomEvent : TLogCodeEvent Read FOnGetCustomEvent Write FOnGetCustomEvent;
  92. End;
  93. ELogError = Class(Exception);
  94. Resourcestring
  95. SLogInfo = 'Info';
  96. SLogWarning = 'Warning';
  97. SLogError = 'Error';
  98. SLogDebug = 'Debug';
  99. SLogCustom = 'Custom (%d)';
  100. SErrLogFailedMsg = 'Failed to log entry (Error: %s)';
  101. implementation
  102. {$i eventlog.inc}
  103. (* File based dummy implementation is used for all platforms not providing
  104. specific implementation of eventlog.inc for the particular platform. *)
  105. { TEventLog }
  106. Resourcestring
  107. SErrOperationNotAllowed = 'Operation not allowed when eventlog is active.';
  108. procedure TEventLog.CheckInactive;
  109. begin
  110. If Active then
  111. Raise ELogError.Create(SErrOperationNotAllowed);
  112. end;
  113. procedure TEventLog.Debug(const Fmt: String; Args: array of const);
  114. begin
  115. Debug(Format(Fmt,Args));
  116. end;
  117. procedure TEventLog.Debug(const Msg: String);
  118. begin
  119. Log(etDebug,Msg);
  120. end;
  121. procedure TEventLog.EnsureActive;
  122. begin
  123. If Not Active then
  124. Active:=True;
  125. end;
  126. procedure TEventLog.Error(const Fmt: String; Args: array of const);
  127. begin
  128. Error(Format(Fmt,Args));
  129. end;
  130. procedure TEventLog.Error(const Msg: String);
  131. begin
  132. Log(etError,Msg);
  133. end;
  134. procedure TEventLog.Info(const Fmt: String; Args: array of const);
  135. begin
  136. Info(Format(Fmt,Args));
  137. end;
  138. procedure TEventLog.Info(const Msg: String);
  139. begin
  140. Log(etInfo,Msg);
  141. end;
  142. procedure TEventLog.Log(const Msg: String);
  143. begin
  144. Log(DefaultEventType,msg);
  145. end;
  146. procedure TEventLog.Log(EventType: TEventType; const Fmt: String;
  147. Args: array of const);
  148. begin
  149. Log(EventType,Format(Fmt,Args));
  150. end;
  151. procedure TEventLog.Log(EventType: TEventType; const Msg: String);
  152. begin
  153. EnsureActive;
  154. Case FlogType of
  155. ltFile : WriteFileLog(EventType,Msg);
  156. ltSystem : WriteSystemLog(EventType,Msg);
  157. end;
  158. end;
  159. procedure TEventLog.WriteFileLog(EventType : TEventType; const Msg : String);
  160. Var
  161. S,TS,T : String;
  162. begin
  163. If FTimeStampFormat='' then
  164. FTimeStampFormat:='yyyy-mm-dd hh:nn:ss.zzz';
  165. TS:=FormatDateTime(FTimeStampFormat,Now);
  166. T:=EventTypeToString(EventType);
  167. S:=Format('%s [%s %s] %s%s',[Identification,TS,T,Msg,LineEnding]);
  168. try
  169. FStream.WriteBuffer(S[1],Length(S));
  170. S:='';
  171. except
  172. On E : Exception do
  173. S:=E.Message;
  174. end;
  175. If (S<>'') and RaiseExceptionOnError then
  176. Raise ELogError.CreateFmt(SErrLogFailedMsg,[S]);
  177. end;
  178. procedure TEventLog.Log(const Fmt: String; Args: array of const);
  179. begin
  180. Log(Format(Fmt,Args));
  181. end;
  182. procedure TEventLog.SetActive(const Value: Boolean);
  183. begin
  184. If Value<>FActive then
  185. begin
  186. If Value then
  187. ActivateLog
  188. else
  189. DeActivateLog;
  190. FActive:=Value;
  191. end;
  192. end;
  193. Procedure TEventLog.ActivateLog;
  194. begin
  195. Case FLogType of
  196. ltFile : ActivateFileLog;
  197. ltSystem : ActivateSystemLog;
  198. end;
  199. end;
  200. Procedure TEventLog.DeActivateLog;
  201. begin
  202. Case FLogType of
  203. ltFile : DeActivateFileLog;
  204. ltSystem : DeActivateSystemLog;
  205. end;
  206. end;
  207. Procedure TEventLog.ActivateFileLog;
  208. var
  209. fFileFlags : Word;
  210. begin
  211. If (FFileName='') then
  212. FFileName:=DefaultFileName;
  213. // This will raise an exception if the file cannot be opened for writing !
  214. if fAppendContent and FileExists(FFileName) then
  215. fFileFlags := fmOpenWrite
  216. else
  217. fFileFlags := fmCreate;
  218. fFileFlags := fFileFlags or fmShareDenyWrite;
  219. FStream:=TFileStream.Create(FFileName,fFileFlags);
  220. end;
  221. Procedure TEventLog.DeActivateFileLog;
  222. begin
  223. FStream.Free;
  224. FStream:=Nil;
  225. end;
  226. procedure TEventLog.SetIdentification(const Value: String);
  227. begin
  228. FIdentification := Value;
  229. end;
  230. procedure TEventLog.SetlogType(const Value: TLogType);
  231. begin
  232. CheckInactive;
  233. Flogtype := Value;
  234. end;
  235. procedure TEventLog.Warning(const Fmt: String; Args: array of const);
  236. begin
  237. Warning(Format(Fmt,Args));
  238. end;
  239. procedure TEventLog.Warning(const Msg: String);
  240. begin
  241. Log(etWarning,Msg);
  242. end;
  243. procedure TEventLog.SetFileName(const Value: String);
  244. begin
  245. CheckInactive;
  246. FFileName := Value;
  247. end;
  248. Procedure TEventLog.CheckIdentification;
  249. begin
  250. If (Identification='') then
  251. Identification:=ChangeFileExt(ExtractFileName(Paramstr(0)),'');
  252. end;
  253. Function TEventLog.EventTypeToString(E : TEventType) : String;
  254. begin
  255. Case E of
  256. etInfo : Result:=SLogInfo;
  257. etWarning : Result:=SLogWarning;
  258. etError : Result:=SLogError;
  259. etDebug : Result:=SLogDebug;
  260. etCustom : Result:=Format(SLogCustom,[CustomLogType]);
  261. end;
  262. end;
  263. Procedure TEventLog.DoGetCustomEventID(Var Code : DWord);
  264. begin
  265. If Assigned(FOnGetCustomEventID) then
  266. FOnGetCustomEventID(Self,Code);
  267. end;
  268. Procedure TEventLog.DoGetCustomEventCategory(Var Code : Word);
  269. begin
  270. If Assigned(FOnGetCustomCategory) then
  271. FOnGetCustomCategory(Self,Code);
  272. end;
  273. Procedure TEventLog.DoGetCustomEvent(Var Code : DWord);
  274. begin
  275. If Assigned(FOnGetCustomEvent) then
  276. FOnGetCustomEvent(Self,Code);
  277. end;
  278. destructor TEventLog.Destroy;
  279. begin
  280. Active:=False;
  281. inherited;
  282. end;
  283. end.