本文整理自网络,侵删。
varLg: TLogs;
Lg := TLogs.Create('_sql.log');Lg.Log('日志内容');Lg.Destroy();
unit TLog;
interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;
type TLogs = class
private FileName: string;
public FRTL:TRTLCriticalSection; function Log(const AText:AnsiString):Boolean;
public constructor Create (FileNM:string) ; overload; destructor Destroy; end;
implementation//constructor TLogs.Create;constructor TLogs.Create (FileNM:string) ;//var// i:Integer;begin inherited Create; FileName:= FileNM; InitializeCriticalSection(FRTL);end;
destructor TLogs.Destroy;begin DeleteCriticalSection(FRTL); inherited Destroy;end;
//===============function TLogs.Log(const AText:AnsiString):Boolean;
var LStr:AnsiString; LFile:TextFile;begin
try try EnterCriticalSection(FRTL); Result:=True;// LStr:=ExtractFilePath(Application.ExeName)+'log.txt'; LStr:=FileName; AssignFile(LFile,LStr); if FileExists(LStr) then Append(LFile) else Rewrite(LFile); LStr:=Format('%s - %s',[DateTimeToStr(Now),AText]); Writeln(LFile,LStr);
finally CloseFile(LFile); LeaveCriticalSection(FRTL); end;
except Result:=False; end;
end;
end.
相关阅读 >>
Delphi 2009 之 tcategorypanelgroup[2]: headeralignment、gradientdirection
Delphi 结构体的使用、tbitmap和tmemorystream的转换
Delphi android 中 toast 的实现(老外写的unit)
Delphi获取flash文件的影片时长,原始尺寸,帧数等信息
更多相关阅读请进入《Delphi》频道 >>