1 {*********************************************************}
3 { Zeos Database Objects }
4 { SQL Monitor component }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
59 SysUtils, Classes, Contnrs, {$IFDEF MSEgui}mclasses,{$ENDIF}
60 ZClasses, ZCompatibility, ZDbcIntfs, ZDbcLogging;
64 {** Repeat declaration of TZLoggingEvent. }
65 TZLoggingEvent = ZDbcLogging.TZLoggingEvent;
67 {** Repeat declaration of TZLoggingFormatter. }
68 IZLoggingFormatter = ZDbcLogging.IZLoggingFormatter;
69 TZLoggingFormatter = ZDbcLogging.TZLoggingFormatter;
71 {** Declares event before logging. }
72 TZTraceEvent = procedure(Sender: TObject; Event: TZLoggingEvent;
73 var LogTrace: Boolean) of object;
75 {** Declares event after logging. }
76 TZTraceLogEvent = procedure(Sender: TObject; Event: TZLoggingEvent) of object;
79 Implements an object to log events from SQL client.
81 TZSQLMonitor = class(TComponent, IZLoggingListener, IZInterface)
86 FMaxTraceCount: Integer;
87 FTraceList: TObjectList;
88 FOnTrace: TZTraceEvent;
89 FOnLogTrace: TZTraceLogEvent;
90 FLoggingFormatter : IZLoggingFormatter;
92 function GetTraceCount: Integer;
93 function GetTraceItem(Index: Integer): TZLoggingEvent;
94 procedure SetActive(const Value: Boolean);
95 procedure SetMaxTraceCount(const Value: Integer);
97 procedure TruncateTraceList(Count: Integer);
98 procedure DoTrace(Event: TZLoggingEvent; var LogTrace: Boolean);
99 procedure DoLogTrace(Event: TZLoggingEvent);
101 constructor Create(AOwner: TComponent); override;
102 destructor Destroy; override;
104 procedure LogEvent(Event: TZLoggingEvent);
106 procedure SaveToFile(const FileName: string);
108 property TraceCount: Integer read GetTraceCount;
109 property TraceList[Index: Integer]: TZLoggingEvent read GetTraceItem;
110 property LoggingFormatter: IZLoggingFormatter read FLoggingFormatter write FLoggingFormatter;
112 property Active: Boolean read FActive write SetActive default False;
113 property AutoSave: Boolean read FAutoSave write FAutoSave default False;
114 property FileName: string read FFileName write FFileName;
115 property MaxTraceCount: Integer read FMaxTraceCount write SetMaxTraceCount;
117 property OnTrace: TZTraceEvent read FOnTrace write FOnTrace;
118 property OnLogTrace: TZTraceLogEvent read FOnLogTrace write FOnLogTrace;
123 {$IFDEF WITH_UNITANSISTRINGS}
130 Constructs this object and assignes main properties.
131 @param AOwner a component owner.
133 constructor TZSQLMonitor.Create(AOwner: TComponent);
135 inherited Create(AOwner);
136 FTraceList := TObjectList.Create;
137 FMaxTraceCount := 100;
141 Destroys this object and cleanups the memory.
143 destructor TZSQLMonitor.Destroy;
151 Gets a number of stored logging events.
152 @returns a number of stored logging events.
154 function TZSQLMonitor.GetTraceCount: Integer;
156 Result := FTraceList.Count;
160 Gets a logging event by it's index.
161 @param Index an event index.
162 @retuns a requested event object.
164 function TZSQLMonitor.GetTraceItem(Index: Integer): TZLoggingEvent;
166 Result := TZLoggingEvent(FTraceList[Index]);
170 Sets an active state for this monitor.
171 @param Value <code>True</code> to activate this monitor
172 and <code>False</code> to deactivate it.
174 procedure TZSQLMonitor.SetActive(const Value: Boolean);
176 if FActive <> Value then
179 DriverManager.AddLoggingListener(Self)
181 if Assigned(DriverManager) then
182 DriverManager.RemoveLoggingListener(Self);
188 Sets a new number of logging events in the storage.
189 @param Value a new number of logging events.
191 procedure TZSQLMonitor.SetMaxTraceCount(const Value: Integer);
193 if Value <> FMaxTraceCount then
195 FMaxTraceCount := Value;
196 TruncateTraceList(Value);
201 Truncates a storage of logging events to the specified limit.
202 @param Count a number of events in the storage.
204 procedure TZSQLMonitor.TruncateTraceList(Count: Integer);
206 while FTraceList.Count > Count do
207 FTraceList.Delete(0);
211 Invokes an event listener after logging event.
212 @param Event a logging event object.
214 procedure TZSQLMonitor.DoLogTrace(Event: TZLoggingEvent);
216 if Assigned(FOnLogTrace) then
217 FOnLogTrace(Self, Event);
221 Invokes an event listener before logging event.
222 @param Event a logging event object.
223 @param LogTrace a flag which switches storing the event.
225 procedure TZSQLMonitor.DoTrace(Event: TZLoggingEvent;
226 var LogTrace: Boolean);
228 if Assigned(FOnTrace) then
229 FOnTrace(Self, Event, LogTrace);
233 Saves the logging events into predefined file
234 set in FileName property.
236 procedure TZSQLMonitor.Save;
238 SaveToFile(FFileName);
242 Saves the logging events to the specified file.
243 @param FileName a name of the file to write the events.
245 procedure TZSQLMonitor.SaveToFile(const FileName: string);
252 if not FileExists(FileName) then
253 Stream := TFileStream.Create(FileName, fmCreate)
255 Stream := TFileStream.Create(FileName, fmOpenWrite or fmShareDenyWrite);
257 for I := 0 to FTraceList.Count - 1 do
259 Temp := AnsiString(TZLoggingEvent(FTraceList[I]).AsString + LineEnding);
260 Buffer := PAnsiChar(Temp);
261 Stream.Write(Buffer^, {$IFDEF WITH_STRLEN_DEPRECATED}AnsiStrings.{$ENDIF}StrLen(Buffer) * sizeof(Ansichar));
269 Handles a new incoming logging event.
270 @param Event an incoming logging event.
272 procedure TZSQLMonitor.LogEvent(Event: TZLoggingEvent);
280 DoTrace(Event, LogTrace);
281 if not LogTrace then Exit;
284 if FMaxTraceCount <> 0 then
286 if FMaxTraceCount > 0 then
287 TruncateTraceList(FMaxTraceCount - 1);
288 FTraceList.Add(TZLoggingEvent.Create(Event.Category, Event.Protocol,
289 Event.Message, Event.ErrorCode, Event.Error));
293 if FAutoSave and (FFileName <> '') then
295 if not FileExists(FFileName) then
296 Stream := TFileStream.Create(FFileName, fmCreate)
298 Stream := TFileStream.Create(FFileName, fmOpenReadWrite or fmShareDenyWrite);
300 Stream.Seek(0, soFromEnd);
301 Temp := AnsiString(Event.AsString(FLoggingFormatter) + LineEnding);
302 Buffer := PAnsiChar(Temp);
303 Stream.Write(Buffer^, {$IFDEF WITH_STRLEN_DEPRECATED}AnsiStrings.{$ENDIF}StrLen(Buffer)*sizeof(Ansichar));