![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Файл лога
Dot © (2004-09-23 22:37:00)Люди как на API создать текстовый файл и написать что нибудь туда + как туда же добавить дату и время. то есть весть лог файл
Cobalt (2004-09-23 23:10:00) [1]Йоханыый... Забылся :(
Вот, исправляюсь:
unit FileLog;
interface
uses Dialogs, SysUtils, Windows;
type
TFileLog = Class (TObject)
Private
FFileName: String;
FHandle: THandle;
FLogging: Boolean;
Public
Constructor Create;
Destructor Destroy; reintroduce;
Function BeginLogging(AFileName: String; Append: boolean): Boolean;
Procedure StopLogging;
Function WriteString(Str: String):Boolean;
Property FileName: String read FFileName;
Property Handle: THandle read FHandle;
Property IsLogging: Boolean read FLogging;
end;
implementation
Constructor TFileLog.Create;
begin
FFileName:='';
FHandle:=INVALID_HANDLE_VALUE;
FLogging:=False;
end;
Destructor TFileLog.Destroy;
begin
if FLogging
then StopLogging;
inherited Destroy;
end;
Function TFileLog.BeginLogging(AFileName: String; Append: boolean): Boolean;
var
attr:word;
begin
if FLogging then StopLogging;
FFileName:=AFileName;
if Append
then attr:=OPEN_ALWAYS
else attr:=CREATE_ALWAYS;
FHandle:=CreateFile(PChar(FFileName),GENERIC_WRITE,FILE_SHARE_READ,
nil,attr,FILE_FLAG_WRITE_THROUGH,0);
if FHandle=INVALID_HANDLE_VALUE
then raise Exception.Create('Ошибка открытия файла '+FFileName+','#13#10+SysErrorMessage(GetLastError));
SetFilePointer(FHandle,0,nil,FILE_END);
FLogging:=true;
Result:=FLogging;
end;
Procedure TFileLog.StopLogging;
begin
if not FLogging then exit;
if not CloseHandle(FHandle)
then if GetLastError<>0
then raise Exception.Create('Ошибка закрытия файла '+FFileName+','#13#10+ SysErrorMessage(GetLastError));
FHandle:=INVALID_HANDLE_VALUE;
FLogging:=False;
end;
Function TFileLog.WriteString(Str: String):boolean;
var
Written, need : Cardinal;
begin
Result:=False;
if not FLogging then Exit;
Str:=Str+#13#10
need:=Length(Str);
if not WriteFile(FHandle,Str[1],need,Written,nil)
then raise Exception.Create('Ошибка записи в файл '+FFileName+','#13#10
+Format('Требуется записать %d байт, записано %d байт',[need,Written])
+','#13#10+ysErrorMessage(GetLastError));
Result:=(Written=need);
end;
end.
Cobalt (2004-09-23 23:09:00) [2]Не фонтан, конечно, но - что есть:
unit FileLog;
interface
uses Dialogs, SysUtils, Windows;
type
TFileLog = Class (TObject)
Private
FFileName: String;
FHandle: THandle;
FLogging: Boolean;
Public
Constructor Create;
Destructor Destroy; reintroduce;
Function BeginLogging(AFileName: String; Append: boolean): Boolean;
Procedure StopLogging;
Function WriteString(Str: String):Boolean;
Property FileName: String read FFileName;
Property Handle: THandle read FHandle;
Property IsLogging: Boolean read FLogging;
end;
implementation
Constructor TFileLog.Create;
begin
FFileName:='';
FHandle:=INVALID_HANDLE_VALUE;
FLogging:=False;
end;
Destructor TFileLog.Destroy;
begin
if FLogging
then StopLogging;
inherited Destroy;
end;
Function TFileLog.BeginLogging(AFileName: String; Append: boolean): Boolean;
var
attr:word;
begin
if FLogging then StopLogging;
FFileName:=AFileName;
if Append
then attr:=OPEN_ALWAYS
else attr:=CREATE_ALWAYS;
FHandle:=CreateFile(PChar(FFileName),GENERIC_WRITE,FILE_SHARE_READ,
nil,attr,FILE_FLAG_WRITE_THROUGH,0);
if FHandle=INVALID_HANDLE_VALUE
then raise Exception.Create('Îøèáêà îòêðûòèÿ ôàéëà '+FFileName+','#13#10+SysErrorMessage(GetLastError));
SetFilePointer(FHandle,0,nil,FILE_END);
FLogging:=true;
Result:=FLogging;
end;
Procedure TFileLog.StopLogging;
begin
if not FLogging then exit;
if not CloseHandle(FHandle)
then if GetLastError0
then raise Exception.Create('Îøèáêà çàêðûòèÿ ôàéëà '+FFileName+','#13#10+ SysErrorMessage(GetLastError));
FHandle:=INVALID_HANDLE_VALUE;
FLogging:=False;
end;
Function TFileLog.WriteString(Str: String):boolean;
var
Written, need : Cardinal;
begin
Result:=False;
if not FLogging then Exit;
Str:=Str+#13#10
need:=Length(Str);
if not WriteFile(FHandle,Str[1],need,Written,nil)
then raise Exception.Create('Îøèáêà çàïèñè â ôàéë '+FFileName+','#13#10
+Format('Òðåáóåòñÿ çàïèñàòü %d áàéò, çàïèñàíî %d áàéò',[need,Written])
+','#13#10+ysErrorMessage(GetLastError));
Result:=(Written=need);
end;
end.