-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLog.pas
378 lines (347 loc) · 12.6 KB
/
Log.pas
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{ ############################################################################ }
{ # # }
{ # MSpeech v1.5.10 # }
{ # # }
{ # Copyright (c) 2012-2020, Mikhail Grigorev. All rights reserved. # }
{ # # }
{ # License: http://opensource.org/licenses/GPL-3.0 # }
{ # # }
{ # Contact: Mikhail Grigorev (email: [email protected]) # }
{ # # }
{ ############################################################################ }
unit Log;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Global, ExtCtrls, ComCtrls, CommCtrl, MGTextReader, MGThread,
MGVCLUtils, Menus, ClipBrd, MGPlacement;
type
TLogForm = class(TForm)
LFileNameDesc: TLabel;
CBFileName: TComboBox;
DeleteLogButton: TButton;
ReloadLogButton: TButton;
FileReadThread: TMGThread;
StatusBar: TStatusBar;
TextListView: TListView;
LogPopupMenu: TPopupMenu;
LogCopy: TMenuItem;
SelectAllRow: TMenuItem;
MGFormPlacement: TMGFormPlacement;
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure CBFileNameChange(Sender: TObject);
procedure DeleteLogButtonClick(Sender: TObject);
procedure ReloadLogButtonClick(Sender: TObject);
procedure FileReadThreadExecute(Sender: TObject; Params: Pointer);
procedure FileReadThreadFinish(Sender: TObject);
procedure CBFileNameDropDown(Sender: TObject);
procedure CheckLogFile;
procedure FileReadThreadStop;
procedure FindLogFile(Dir, Ext: String);
procedure TextListViewData(Sender: TObject; Item: TListItem);
procedure TextListViewCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
procedure TextListViewContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
procedure LogCopyClick(Sender: TObject);
procedure SelectAllRowClick(Sender: TObject);
private
{ Private declarations }
FTextReader: TMGTextReaderA;
IsUnicodeFile: Boolean;
procedure OnLogUpdated(var Msg: TMessage); message WM_UPDATELOG;
// Для мультиязыковой поддержки
procedure OnLanguageChanged(var Msg: TMessage); message WM_LANGUAGECHANGED;
procedure LoadLanguageStrings;
public
{ Public declarations }
end;
var
LogForm: TLogForm;
implementation
{$R *.dfm}
uses
Main;
procedure TLogForm.FormCreate(Sender: TObject);
begin
// Для мультиязыковой поддержки
LogFormHandle := Handle;
SetWindowLong(Handle, GWL_HWNDPARENT, 0);
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_APPWINDOW);
// Загружаем язык интерфейса
LoadLanguageStrings;
// Настройки TextListView
// Инфо тут http://zarezky.spb.ru/lectures/mfc/list-control.html
ListView_SetExtendedListViewStyle(TextListView.Handle, LVS_EX_DOUBLEBUFFER or LVS_EX_FULLROWSELECT); // or LVS_EX_INFOTIP
// Сохранение позиций окна
MGFormPlacement.IniFileName := WorkPath + INIFormStorage;
end;
procedure TLogForm.FormDestroy(Sender: TObject);
begin
if Assigned(FTextReader) then
FreeAndNil(FTextReader);
end;
procedure TLogForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
Close;
end;
procedure TLogForm.FormShow(Sender: TObject);
begin
// Прозрачность окна
AlphaBlend := AlphaBlendEnable;
AlphaBlendValue := AlphaBlendEnableValue;
// Другие настройки
TextListView.Clear;
CBFileName.ItemIndex := -1;
ReloadLogButton.Enabled := False;
DeleteLogButton.Enabled := False;
IsUnicodeFile := False;
end;
procedure TLogForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// Завершаем поток
FileReadThreadStop;
end;
{ Раскрытие списка CBFileName }
procedure TLogForm.CBFileNameDropDown(Sender: TObject);
begin
// Завершаем поток
//FileReadThreadStop;
// Построение списка файлов
CheckLogFile;
end;
{ Изменение текста в CBFileName }
procedure TLogForm.CBFileNameChange(Sender: TObject);
begin
// Завершаем поток
FileReadThreadStop;
// Закрываем логи
CloseLogFile;
// Ожидание...
StatusBar.Panels[0].Text := GetLangStr('PleaseWait');
StatusBar.Panels[1].Text := '';
StatusBar.Panels[2].Text := '';
StartWait;
if EnableLogs then
EnableLogs := False;
FileReadThread.Execute(Self);
end;
{ Поток чтения лог-файла }
procedure TLogForm.FileReadThreadExecute(Sender: TObject; Params: Pointer);
var
FileName: String;
TC: Cardinal;
LineCount, TempLineCount: Integer;
LineCountTime: Extended;
I: Integer;
begin
FileName := CBFileName.Items[CBFileName.ItemIndex];
if FileExists(FileName) then
begin
TextListView.Clear;
if GetMyFileSize(FileName) > 0 then
begin
DeleteLogButton.Enabled := False;
ReloadLogButton.Enabled := False;
if Assigned(FTextReader) then
FreeAndNil(FTextReader);
try
TC := GetTickCount;
FTextReader := TMGTextReaderA.Create(FileName);
LineCount := FTextReader.LineCount;
LineCountTime := GetTickCount - TC;
TextListView.Items.Count := LineCount;
StatusBar.Panels[0].Text := ExtractFileName(FileName);
StatusBar.Panels[1].Text := Format(GetLangStr('TotalString'), [LineCount]);
StatusBar.Panels[2].Text := Format(GetLangStr('LoadingTime'), [LineCountTime]);
except
on E: Exception do
begin
EnableLogs := ReadCustomINI(WorkPath, 'Main', 'EnableLogs', False);
MsgDie(ProgramsName + ' - ' + GetLangStr('MsgErr5'), PWideChar(Format(GetLangStr('MsgErr6'), [FileName]) + #13 +
GetLangStr('MsgErr7') + #13 + Trim(E.Message)));
end;
end;
end
else
begin
StatusBar.Panels[0].Text := ExtractFileName(FileName);
StatusBar.Panels[1].Text := Format(GetLangStr('TotalString'), [0]);
StatusBar.Panels[2].Text := Format(GetLangStr('LoadingTime'), [0.00]);
end;
ReloadLogButton.Enabled := True;
DeleteLogButton.Enabled := True;
end;
end;
{ Поток завершен }
procedure TLogForm.FileReadThreadFinish(Sender: TObject);
begin
EnableLogs := ReadCustomINI(WorkPath, 'Main', 'EnableLogs', False);
SendMessage(TextListView.Handle, WM_VSCROLL, SB_BOTTOM, 0);
// Окончание ожидания
StopWait;
end;
{ Завершаем поток }
procedure TLogForm.FileReadThreadStop;
begin
// Окончание ожидания
StopWait;
// Останавливаем поток
if not FileReadThread.Terminated then
FileReadThread.Terminate;
while not (FileReadThread.Terminated) do
begin
Sleep(1);
Application.ProcessMessages;
end;
if Assigned(FTextReader) then
FreeAndNil(FTextReader);
end;
procedure TLogForm.LogCopyClick(Sender: TObject);
var
LVStr: String;
I: Integer;
begin
LVStr := '';
if TextListView.SelCount = 1 then
LVStr := TextListView.Items.Item[TextListView.ItemIndex].Caption
else
begin
for I := TextListView.ItemIndex to TextListView.ItemIndex+TextListView.SelCount-1 do
LVStr := LVStr + TextListView.Items.Item[I].Caption + #13#10;
end;
Clipboard.SetTextBuf(PChar(LVStr));
end;
procedure TLogForm.SelectAllRowClick(Sender: TObject);
begin
TextListView.SelectAll;
end;
procedure TLogForm.TextListViewContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
if TextListView.Items.Count > 0 then
TextListView.PopupMenu := LogPopupMenu
else
TextListView.PopupMenu := nil;
end;
procedure TLogForm.TextListViewCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
const
cStripe = $CCFFCC;
begin
if Odd(Item.Index) then
Sender.Canvas.Brush.Color := cStripe
else
Sender.Canvas.Brush.Color := clWindow;
end;
procedure TLogForm.TextListViewData(Sender: TObject; Item: TListItem);
begin
if Assigned(FTextReader) then
begin
if IsUnicodeFile then
Item.Caption := UTF8ToWideString((FTextReader.Lines[Item.Index]))
else
Item.Caption := String((FTextReader.Lines[Item.Index]));
end;
end;
procedure TLogForm.DeleteLogButtonClick(Sender: TObject);
var
Deleted: Boolean;
UserAnswer: Integer;
begin
if FileExists(CBFileName.Items[CBFileName.ItemIndex]) then
begin
// Завершаем поток
FileReadThreadStop;
// Пытаемся удалить файл
Deleted := False;
UserAnswer := 1;
repeat
Deleted := SysUtils.DeleteFile(CBFileName.Items[CBFileName.ItemIndex]);
if not Deleted then
begin
case MessageBox(Handle,PWideChar(Format(GetLangStr('MsgErr9'), [CBFileName.Items[CBFileName.ItemIndex]]) + #13 +
GetLangStr('MsgErr10')),PWideChar(MainForm.Caption + ' - ' + GetLangStr('MsgErr8')),36) of
6: UserAnswer := 1; // Да
7: UserAnswer := 0; // Нет
end;
end;
until (Deleted) or (UserAnswer = 0);
if Deleted then
begin
if EnableLogs then WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': Procedure DeleteLogButtonClick: File ' + CBFileName.Items[CBFileName.ItemIndex] + ' deleted.');
if Assigned(FTextReader) then
FreeAndNil(FTextReader);
TextListView.Clear;
StatusBar.Panels[0].Text := '';
StatusBar.Panels[1].Text := '';
StatusBar.Panels[2].Text := '';
end;
CheckLogFile;
end;
end;
procedure TLogForm.CheckLogFile;
begin
CBFileName.Clear;
ReloadLogButton.Enabled := False;
DeleteLogButton.Enabled := False;
FindLogFile(WorkPath, '*.log');
end;
{ Процедура поиска файлов по маске и формирования их списка }
procedure TLogForm.FindLogFile(Dir, Ext: String);
var
SR: TSearchRec;
begin
if FindFirst(Dir + '*.*', faAnyFile or faDirectory, SR) = 0 then
begin
repeat
if (SR.Attr = faDirectory) and ((SR.Name = '.') or (SR.Name = '..')) then // Чтобы не было файлов . и ..
begin
Continue; // Продолжаем цикл
end;
if MatchStrings(SR.Name, Ext) then
begin
// Заполняем лист
CBFileName.Items.Add(Dir+SR.Name);
end;
if (SR.Attr = faDirectory) then // Если нашли директорию, то ищем файлы в ней
begin
FindLogFile(Dir + '\' + SR.Name, Ext); // Pекурсивно вызываем нашу процедуру
Continue; // Продолжаем цикл
end;
until FindNext(SR) <> 0;
FindClose(SR);
end;
end;
procedure TLogForm.ReloadLogButtonClick(Sender: TObject);
begin
CBFileNameChange(ReloadLogButton);
end;
{ Подгрузка лога по событию WM_UPDATELOG }
procedure TLogForm.OnLogUpdated(var Msg: TMessage);
begin
if CBFileName.ItemIndex <> -1 then
ReloadLogButton.Click;
end;
{ Смена языка интерфейса по событию WM_LANGUAGECHANGED }
procedure TLogForm.OnLanguageChanged(var Msg: TMessage);
begin
LoadLanguageStrings;
end;
{ Для мультиязыковой поддержки }
procedure TLogForm.LoadLanguageStrings;
begin
Caption := ProgramsName + ' - ' + GetLangStr('LogFormCaption');
LogPopupMenu.Items[0].Caption := GetLangStr('Copy');
LogPopupMenu.Items[1].Caption := GetLangStr('SelectAll');
LFileNameDesc.Caption := GetLangStr('LFileNameDesc');
DeleteLogButton.Caption := GetLangStr('DeleteLogButton');
ReloadLogButton.Caption := GetLangStr('ReloadLogButton');
// Позиционируем лейблы
CBFileName.Left := LFileNameDesc.Left + LFileNameDesc.Width + 5;
CBFileName.Width := ReloadLogButton.Left - CBFileName.Left - 5;
end;
end.