Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2004.10.31;
Скачать: CL | DM;

Вниз

Файл лога   Найти похожие ветки 

 
Dot   (2004-09-23 22:37) [0]

Люди как на API создать текстовый файл и написать что нибудь туда + как туда же добавить дату и время. то есть весть лог файл


 
Cobalt ©   (2004-09-23 23:09) [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("&#206;&#248;&#232;&#225;&#234;&#224; &#231;&#224;&#234;&#240;&#251;&#242;&#232;&#255; &#244;&#224;&#233;&#235;&#224; "+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("&#206;&#248;&#232;&#225;&#234;&#224; &#231;&#224;&#239;&#232;&#241;&#232; &#226; &#244;&#224;&#233;&#235; "+FFileName+","#13#10
            +Format("&#210;&#240;&#229;&#225;&#243;&#229;&#242;&#241;&#255; &#231;&#224;&#239;&#232;&#241;&#224;&#242;&#252; %d &#225;&#224;&#233;&#242;, &#231;&#224;&#239;&#232;&#241;&#224;&#237;&#238; %d &#225;&#224;&#233;&#242;",[need,Written])
            +","#13#10+ysErrorMessage(GetLastError));
 Result:=(Written=need);
end;

end.


 
Cobalt ©   (2004-09-23 23:10) [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 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.



Страницы: 1 вся ветка

Текущий архив: 2004.10.31;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.043 c
8-1091407249
widoms
2004-08-02 04:40
2004.10.31
Компоненты для работы с изображениями.


1-1097746852
jenbond
2004-10-14 13:40
2004.10.31
Нужен совет в работе прогораммы


1-1097120934
newver
2004-10-07 07:48
2004.10.31
работа с DLL и тем что внутри.


1-1098226254
Кклуб
2004-10-20 02:50
2004.10.31
Динамическое создание OnClick


14-1097569401
pasha_golub
2004-10-12 12:23
2004.10.31
Чтиво интересное