zeoslib  UNKNOWN
 All Files
ZDbcLogging.pas
Go to the documentation of this file.
1 {*********************************************************}
2 { }
3 { Zeos Database Objects }
4 { Database Logging Classes and Interfaces }
5 { }
6 { Originally written by Sergey Seroukhov }
7 { }
8 {*********************************************************}
9 
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
12 { }
13 { License Agreement: }
14 { }
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. }
20 { }
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. }
39 { }
40 { }
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) }
45 { }
46 { http://www.sourceforge.net/projects/zeoslib. }
47 { }
48 { }
49 { Zeos Development Group. }
50 {********************************************************@}
51 
52 unit ZDbcLogging;
53 
54 interface
55 
56 {$I ZDbc.inc}
57 
58 uses SysUtils, ZClasses;
59 
60 type
61 
62  {** Defines a time or the message. }
63  TZLoggingCategory = (lcConnect, lcDisconnect, lcTransaction, lcExecute, lcOther,
64  lcPrepStmt, lcBindPrepStmt, lcExecPrepStmt, lcUnprepStmt);
65 
66  {** Defines a object for logging event. }
67  TZLoggingEvent = class;
68 
69  {** Defines an interface to format logging events. }
70  IZLoggingFormatter = interface (IZInterface)
71 // ['{53559F5F-AC22-4DDC-B2EA-45D21ADDD2D5}']
72  function Format(LoggingEvent: TZLoggingEvent) : string;
73  end;
74 
75  { TZLoggingFormatter }
76  {** Defines a object for logging event. }
77  TZLoggingFormatter = class (TInterfacedObject, IZLoggingFormatter)
78  private
79  public
80  function Format(LoggingEvent: TZLoggingEvent) : string; virtual;
81  end;
82 
83  {** Defines a object for logging event. }
84  TZLoggingEvent = class (TObject)
85  private
86  FCategory: TZLoggingCategory;
87  FProtocol: string;
88  FMessage: string;
89  FErrorCode: Integer;
90  FError: string;
91  FTimestamp: TDateTime;
92  public
93  constructor Create(Category: TZLoggingCategory; Protocol: string;
94  Msg: string; ErrorCode: Integer; Error: string);
95 
96  function AsString(LoggingFormatter:IZLoggingFormatter = nil): string;
97 
98  property Category: TZLoggingCategory read FCategory;
99  property Protocol: string read FProtocol;
100  property Message: string read FMessage;
101  property ErrorCode: Integer read FErrorCode;
102  property Error: string read FError;
103  property Timestamp: TDateTime read FTimestamp;
104  end;
105 
106  {** Defines an interface to accept logging events. }
107  IZLoggingListener = interface (IZInterface)
108  ['{53559F5F-AC22-4DDC-B2EA-45D21ADDD2D4}']
109 
110  procedure LogEvent(Event: TZLoggingEvent);
111  end;
112 
113 implementation
114 var DefaultLoggingFormatter: TZLoggingFormatter;
115 
116 { TZLoggingFormatter }
117 
118 function TZLoggingFormatter.Format(LoggingEvent: TZLoggingEvent): string;
119 begin
120  Result := FormatDateTime('yyyy-mm-dd hh:mm:ss', LoggingEvent.Timestamp) + ' cat: ';
121  case LoggingEvent.Category of
122  lcConnect: Result := Result + 'Connect';
123  lcDisconnect: Result := Result + 'Disconnect';
124  lcTransaction: Result := Result + 'Transaction';
125  lcExecute: Result := Result + 'Execute';
126  lcPrepStmt: Result := Result + 'Prepare';
127  lcBindPrepStmt: Result := Result + 'Bind prepared';
128  lcExecPrepStmt: Result := Result + 'Execute prepared';
129  lcUnprepStmt: Result := Result + 'Unprepare prepared';
130  else
131  Result := Result + 'Other';
132  end;
133  if LoggingEvent.Protocol <> '' then
134  Result := Result + ', proto: ' + LoggingEvent.Protocol;
135  Result := Result + ', msg: ' + LoggingEvent.Message;
136  if (LoggingEvent.ErrorCode <> 0) or (LoggingEvent.Error <> '') then
137  begin
138  Result := Result + ', errcode: ' + IntToStr(LoggingEvent.ErrorCode)
139  + ', error: ' + LoggingEvent.Error;
140  end;
141 end;
142 
143 { TZLoggingEvent }
144 
145 {**
146  Constructs this logging event.
147  @param Protocol a DBC protocol.
148  @param Msg a description message.
149  @param ErrorCode an error code.
150  @param Error an error message.
151 }
152 constructor TZLoggingEvent.Create(Category: TZLoggingCategory;
153  Protocol: string; Msg: string; ErrorCode: Integer; Error: string);
154 begin
155  FCategory := Category;
156  FProtocol := Protocol;
157  FMessage := Msg;
158  FErrorCode := ErrorCode;
159  FError := Error;
160  FTimestamp := Now;
161 end;
162 
163 {**
164  Gets a string representation for this event.
165  @returns a string representation.
166 }
167 function TZLoggingEvent.AsString(LoggingFormatter:IZLoggingFormatter = nil): string;
168 begin
169  If Assigned(LoggingFormatter) then
170  Result := LoggingFormatter.Format(Self)
171  else
172  Result := DefaultLoggingFormatter.Format(Self);
173 end;
174 
175 initialization
176  DefaultLoggingFormatter := TZLoggingFormatter.Create;
177 
178 finalization
179  DefaultLoggingFormatter.Free;
180 end.
181