Menu

[r7]: / tracefiles.pas  Maximize  Restore  History

Download this file

202 lines (176 with data), 6.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{---------------------------------------------------------------------}
{ }
{ Firebird database server profiler tool (FBProfiler) }
{ }
{ Copyright (c) 2013-2018 Bel Air Informatique (www.belair-info.fr) }
{ Copyright (c) 2013-2018 Serguei Tarassov (www.arbinada.com) }
{ }
{ FBProfiler uses IBX For Lazarus components (Firebird Express) }
{ FBProfiler was firstly released for public domain }
{ on October 31 of 2015 (Halloween) so any donations are welcome. }
{ }
{ The contents of this file are subject to the InterBase }
{ Public License Version 1.0 (the "License"); you may not }
{ use this file except in compliance with the License. You }
{ may obtain a copy of the License at }
{ http://www.firebirdsql.org/en/interbase-public-license/ }
{ Software distributed under the License is distributed on }
{ an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either }
{ express or implied. See the License for the specific language }
{ governing rights and limitations under the License. }
{ }
{---------------------------------------------------------------------}
unit TraceFiles;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, TraceControl;
const
TraceConfExt: string = '.trc';
TracePackExt: string = '.trz';
type
{ TTraceConfFile }
TTraceConfFile = class
public
class function GetDlgFilter: string;
class procedure Load(const FileName: string; Config: TTraceConfig);
class procedure Save(const FileName: string; const Config: TTraceConfig);
end;
{ TTraceFile }
TTraceFile = class
public
class function GetDlgFilter: string;
class function MakePackName(const SourceFile: string): string;
class function Restore(const PackFileName: string): string;
class procedure Save(const SourcePath, SourceFile, TargetDir, PackageName: string);
end;
implementation
uses
XMlPropStorage,
Zipper;
const
TraceConf_RootNodePath = 'FBProfiler/TraceConf';
{ TTraceConfFile }
class function TTraceConfFile.GetDlgFilter: string;
begin
Result := Format('Trace configuration file (*%0:s)|*%0:s', [TraceConfExt]);
end;
class procedure TTraceConfFile.Load(const FileName: string; Config: TTraceConfig);
var
i: integer;
Storage: TXMLPropStorage;
begin
Storage := TXMLPropStorage.Create(nil);
try
Storage.FileName := FileName;
Storage.RootNodePath := TraceConf_RootNodePath;
Config.HostName := Storage.ReadString('HostName', Config.HostName);
Config.Port := Storage.ReadInteger('Port', Config.Port);
Config.UserName := Storage.ReadString('UserName', Config.UserName);
Config.DatabaseFilter := Storage.ReadString('DatabaseFilter', Config.DatabaseFilter);
for i := 0 to Config.Params.Count - 1 do
Config.Params[i].Value :=
Storage.ReadString(Config.Params[i].Name, Config.Params[i].Value);
finally
Storage.FreeStorage;
end;
end;
class procedure TTraceConfFile.Save(const FileName: string;
const Config: TTraceConfig);
var
i: integer;
Storage: TXMLPropStorage;
begin
Storage := TXMLPropStorage.Create(nil);
try
Storage.FileName := FileName;
Storage.RootNodePath := TraceConf_RootNodePath;
Storage.WriteString('HostName', Config.HostName);
Storage.WriteInteger('Port', Config.Port);
Storage.WriteString('UserName', Config.UserName);
Storage.WriteString('DatabaseFilter', Config.DatabaseFilter);
for i := 0 to Config.Params.Count - 1 do
Storage.WriteString(Config.Params[i].Name, Config.Params[i].Value);
Storage.Save;
finally
Storage.FreeStorage;
end;
end;
{ TTraceFile }
class function TTraceFile.GetDlgFilter: string;
begin
Result := Format('Trace files package (*%0:s)|*%0:s', [TracePackExt]);
end;
class function TTraceFile.MakePackName(const SourceFile: string): string;
begin
Result := Format('%s_%s%s',
[ChangeFileExt(SourceFile, ''),
FormatDateTime('yyyy-mm-dd_hh-nn-ss', Now),
TracePackExt]);
end;
class function TTraceFile.Restore(const PackFileName: string): string;
var
UnZip: TUnZipper;
i: integer;
begin
Assert(SysUtils.FileExists(PackFileName),
Format('Trace pack file not found: %s', [PackFileName]));
Result := '';
UnZip := TUnZipper.Create;
try
UnZip.FileName := PackFileName;
UnZip.OutputPath := ExtractFileDir(PackFileName);
UnZip.Examine;
for i := 0 to UnZip.Entries.Count - 1 do
begin
if ExtractFileExt(UnZip.Entries[i].ArchiveFileName) = '.dbf' then
begin
Result := IncludeTrailingPathDelimiter(UnZip.OutputPath) +
UnZip.Entries[i].ArchiveFileName;
break;
end;
end;
Assert(Trim(Result) <> '', Format('No data file was found in package: %s', [PackFileName]));
UnZip.UnZipAllFiles;
finally
UnZip.Free;
end;
end;
class procedure TTraceFile.Save(const SourcePath, SourceFile, TargetDir,
PackageName: string);
procedure AddFileToZip(Z: TZipper; const APath, AFileName: string);
var
FileName: string;
begin
FileName := IncludeTrailingPathDelimiter(APath) + AFileName;
if SysUtils.FileExists(FileName) then
Z.Entries.AddFileEntry(FileName, AFileName)
else
raise Exception.CreateFmt('Trace file not exists: %s', [FileName]);
end;
var
PackageFileName: string;
Zip: TZipper;
begin
if not DirectoryExists(TargetDir) then
raise Exception.CreateFmt('Directory not exist: %s', [TargetDir]);
if Pos(PathDelim, PackageName) <> 0 then
raise Exception.Create('Package name contains invalid characters');
PackageFileName := IncludeTrailingPathDelimiter(TargetDir) + PackageName;
if ExtractFileExt(PackageFileName) <> TracePackExt then
PackageFileName := PackageFileName + TracePackExt;
if SysUtils.FileExists(PackageFileName) then
SysUtils.DeleteFile(PackageFileName);
if SysUtils.FileExists(PackageFileName) then
raise Exception.CreateFmt('Cannot delete existing file %s', [PackageFileName]);
Zip := TZipper.Create;
try
Zip.FileName := PackageFileName;
AddFileToZip(Zip, SourcePath, SourceFile);
AddFileToZip(Zip, SourcePath, ChangeFileExt(SourceFile, '.fpt'));
Zip.ZipAllFiles;
finally
Zip.Free;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.