88interface
99
1010uses
11- ZConnection, ZDataSet , DB,
12- SysUtils, fpJSON ;
11+ SysUtils, Classes, fpJSON , DB,
12+ ZConnection, ZDataSet, ZSqlProcessor ;
1313
1414type
1515
16+ TLog = class ;
17+
18+ { TThreadedLog }
19+
20+ TThreadedLog = class (TThread)
21+ private
22+ FFileName: string;
23+ FLogObject: TJSONObject;
24+ FLog: TLog;
25+ procedure SetFileName (AValue: string);
26+ procedure SetLogObject (AValue: TJSONObject);
27+ protected
28+ procedure Execute ; override;
29+ property FileName: string read FFileName write SetFileName;
30+ property LogObject: TJSONObject read FLogObject write SetLogObject;
31+ public
32+ constructor Create(AFileName: string; ALogObject: TJSONObject = nil ); overload;
33+ constructor Create(AFileName: string; ASection, AValue: string); overload;
34+ destructor Destroy; override;
35+ end ;
36+
1637 { TLog }
1738
1839 TLog = class
1940 private
2041 FQuery: TZQuery;
2142 FConn: TZConnection;
43+ FThread: TThreadedLog;
2244 function ValidaBanco : boolean;
2345 function GetDefaultDir (aFileName: string): string;
2446 public
25- constructor Create(aFileName: string = ' ' );
47+ constructor Create(aFileName: string = ' log.db ' );
2648 destructor Destroy; override;
2749 procedure Log (aJSON: TJSONObject); overload;
2850 procedure Log (aEvent, aValue: string); overload;
@@ -36,18 +58,71 @@ TDataSetJSONHelper = class helper for TDataSet
3658
3759implementation
3860
61+ { TThreadedLog }
62+
63+ procedure TThreadedLog.SetFileName (AValue: string);
64+ begin
65+ if FFileName = AValue then Exit;
66+ FFileName := AValue;
67+ end ;
68+
69+ procedure TThreadedLog.SetLogObject (AValue: TJSONObject);
70+ begin
71+ if FLogObject = AValue then Exit;
72+ FLogObject := AValue;
73+ end ;
74+
75+ constructor TThreadedLog.Create(AFileName: string; ALogObject: TJSONObject);
76+ begin
77+ inherited Create(False);
78+ FreeOnTerminate := True;
79+ FileName := AFileName;
80+ if not Assigned(FLog) then
81+ FLog := TLog.Create(AFileName);
82+
83+ if ALogObject <> nil then
84+ LogObject := ALogObject;
85+ end ;
86+
87+ constructor TThreadedLog.Create(AFileName: string; ASection, AValue: string);
88+ begin
89+ inherited Create(False);
90+ FreeOnTerminate := True;
91+ FileName := AFileName;
92+ if not Assigned(FLog) then
93+ FLog := TLog.Create(AFileName);
94+
95+ if not Assigned(LogObject) then
96+ LogObject := TJSONObject.Create;
97+ LogObject.Add(' LOG_Data' , DateTimeToStr(now));
98+ LogObject.Add(' LOG_Evento' , ASection);
99+ LogObject.Add(' LOG_Conteudo' , AValue);
100+ end ;
101+
102+ destructor TThreadedLog.Destroy;
103+ begin
104+ if Assigned(FLog) then FLog.Free;
105+ inherited Destroy;
106+ end ;
107+
108+ procedure TThreadedLog.Execute ;
109+ begin
110+ FLog.Log(LogObject);
111+ end ;
112+
39113{ TLog }
40114
41115constructor TLog.Create(aFileName: string);
116+ var
117+ zsqlproc: TZSQLProcessor;
42118begin
43119 FConn := TZConnection.Create(nil );
44120 FConn.Protocol := ' sqlite-3' ;
45- if aFileName.IsEmpty then
46- FConn.Database := ExtractFilePath(ParamStr(0 )) + ' log.db'
47- else
48- FConn.Database := ExtractFilePath(ParamStr(0 )) + aFileName;
49- FConn.Properties.Add(' LockingMode=normal' );
121+ FConn.Database := ExtractFilePath(ParamStr(0 )) + aFileName;
50122 FConn.LibraryLocation := GetDefaultDir(' sqlite3.dll' );
123+ FConn.Connect;
124+ FConn.ExecuteDirect(' PRAGMA locking_mode=NORMAL' );
125+ FConn.ExecuteDirect(' PRAGMA journal_mode=OFF' );
51126
52127 FQuery := TZQuery.Create(nil );
53128 FQuery.Connection := FConn;
@@ -76,21 +151,6 @@ function TLog.GetDefaultDir(aFileName: string): string;
76151 Result := DefaultDir + ' \' + aFileName;
77152end ;
78153
79- function TLog.getLog : string;
80- begin
81- with FQuery do
82- begin
83- Close;
84- SQL.Clear;
85- SQL.Add(' SELECT *' );
86- SQL.Add(' FROM Log' );
87- Open;
88- Result := FQuery.ToJSON;
89-
90- Close;
91- end ;
92- end ;
93-
94154procedure TLog.Log (aEvent, aValue: string);
95155var
96156 aJSON: TJSONObject;
@@ -106,6 +166,26 @@ procedure TLog.Log(aEvent, aValue: string);
106166 end ;
107167end ;
108168
169+ function TLog.getLog : string;
170+ begin
171+ with FQuery do
172+ begin
173+ ReadOnly := True;
174+ Close;
175+ SQL.Clear;
176+ SQL.Add(' SELECT ' );
177+ SQL.Add(' LOG_ID ' );
178+ SQL.Add(' , CAST(LOG_Data AS VARCHAR) LOG_Data' );
179+ SQL.Add(' , LOG_Evento' );
180+ SQL.Add(' , LOG_Conteudo' );
181+ SQL.Add(' FROM Log' );
182+ Open;
183+ Result := FQuery.ToJSON;
184+ Close;
185+ ReadOnly := False;
186+ end ;
187+ end ;
188+
109189procedure TLog.Log (aJSON: TJSONObject);
110190var
111191 I: integer;
@@ -147,7 +227,7 @@ function TLog.ValidaBanco: boolean;
147227 SQL.Clear;
148228 SQL.Add(' CREATE TABLE Log(' );
149229 SQL.Add(' LOG_ID integer primary key' );
150- SQL.Add(' , LOG_Data timestamp ' );
230+ SQL.Add(' , LOG_Data varchar ' );
151231 SQL.Add(' , LOG_Evento varchar' );
152232 SQL.Add(' , LOG_Conteudo varchar' );
153233 SQL.Add(' );' );
@@ -174,8 +254,11 @@ function TDataSetJSONHelper.ToJSON: string;
174254 begin
175255 JSONObj := TJSONObject.Create;
176256 for I := 0 to pred(FieldCount) do
257+ begin
177258 if not Fields.Fields[I].AsString.IsEmpty then
178259 JSONObj.Add(Fields.Fields[I].FieldName, Fields.Fields[I].AsString);
260+
261+ end ;
179262 JSONArr.Add(JSONObj);
180263 Next;
181264 end ;
0 commit comments