本文主要是介绍delphi日志模块代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
unit LOG_logging;interface
uses
System.Win.ScktComp, System.SysUtils, Winsock, Windows, System.Classes,
UGlobalvariable_type;
type
TLOG_logging = class
txt: TextFile; {文件}
txtfile: AnsiString; {文件名}
procedure ONLOG(VVGFlogtype: integer; VVGFlogstr: AnsiString);
strict private
VGFlogstr: AnsiString;
VGFlogtype: integer;
procedure Setlogstr(const Value: AnsiString);
procedure Setlogtype(const Value: integer);
procedure log_save; {日志函数}
published
public
constructor Create;
destructor destroy; override;
procedure Execute;
property logtype:integer read VGFlogtype write Setlogtype;
property logstr:AnsiString read VGFlogstr write Setlogstr;
end;
implementation
constructor TLOG_logging.Create;
begin
inherited Create;
if not DirectoryExists(ExtractFilePath(ParamStr(0))+'log\') then
ForceDirectories(ExtractFilePath(ParamStr(0))+'log\');
txtfile := ExtractFilePath(ParamStr(0))+'log\'+ FormatDateTime('YYYYMMDD',Now ) +'.log' ;
AssignFile( txt,txtfile);
//Resume ; //唤醒线程,开始执行Execute里的内容
end;
destructor TLOG_logging.destroy;
begin
inherited;
end;
procedure TLOG_logging.Execute; {建立TCP服务线程}
begin
TRY
txtfile := ExtractFilePath(ParamStr(0))+'log\'+ FormatDateTime('YYYYMMDD',Now ) +'.log' ;
if fileexists(txtfile) then
begin
end
else
begin
AssignFile( txt,txtfile);
Rewrite(txt); //日志文件新建或者重写原有的文件
CloseFile(txt);
end;
if VGFlogtype > 0 then
begin
log_save; {日志函数}
VGFlogtype := 0;
end;
FINALLY
END;
end;
procedure TLOG_logging.log_save; {日志函数}
begin
try
try
Append(txt); //写打开文件,指针到尾
except
Exit;
end;
case VGFlogtype of
1://异常记录
begin
Writeln(txt,'systemerr:'+FormatDateTime('yy-mm-dd hh:nn:ss',now) +'|'+string(VGFlogstr) ); //写入记录文件
Writeln(txt,'' );
end;
2://TCP记录
begin
Writeln(txt,'TCP_LOG:'+FormatDateTime('yy-mm-dd hh:nn:ss',now) +'|'+string(VGFlogstr)); //写入记录文件
Writeln(txt,'' );
end;
3://记录日志
begin
Writeln(txt,'LOG:'+FormatDateTime('yy-mm-dd hh:nn:ss',now) +'|'+string(VGFlogstr) ); //写入记录文件
Writeln(txt,'' );
end;
end;
finally
CloseFile(txt);
end;
end;
procedure TLOG_logging.Setlogstr(const Value: AnsiString);
begin
VGFlogstr := Value;
end;
procedure TLOG_logging.Setlogtype(const Value: integer);
begin
VGFlogtype := Value;
end;
procedure TLOG_logging.ONLOG(VVGFlogtype: integer; VVGFlogstr: AnsiString);
begin
VGFlogstr := VVGFlogstr;
VGFlogtype := VVGFlogtype;
Execute;
end;
end.
这篇关于delphi日志模块代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!