delphi 日志单元

unit uText;

interface

uses   Windows,SysUtils;

procedure WriteLog(strlog:string;tag:string='CommonMsg');
procedure WriteToConsole(strlog:string;tag:string='CommonMsg');

{$APPTYPE CONSOLE}
implementation
var
LogText: Text;
FileName: string = 'D:\log.txt';

procedure WriteLog(strlog:string;tag:string='CommonMsg');
var
  logMsg:string;
  begin
    try
        AssignFile(LogText,FileName);
        if not FileExists(FileName) then
        begin
           ReWrite(LogText)
        end
        else
            Append(LogText);
        logMsg:=FormatdateTime('yyyy-mm-dd hh:nn:ss', Now)+' '+tag+':'+strlog;
        WriteLn(LogText,logMsg);
    finally
          CloseFile(LogText);
    end;
  end;


procedure WriteToConsole(strlog:string;tag:string='CommonMsg');
var
  logMsg,instr:string;

begin
  logMsg:=FormatdateTime('yyyy-mm-dd hh:nn:ss', Now)+' '+tag+':'+strlog;
  try
    Writeln(logMsg);
  except
    WriteLog('控制台异常','ConError');
  end;
end;

initialization  {可选的初始化部分}
  AllocConsole;
  SetConsoleTitle('控制台调试窗口');
finalization    {结束部分}
  FreeConsole;
end.


你可能感兴趣的:(delphi)