unit LogUtils;

interface

uses
  Classes;

type
  TLogUtils = class
  private
    FLogFolder: string;
    FLogFile: string;
    FExeVersion: string;
  public
    constructor Create;
    destructor Destroy; override;

    procedure AppendLogTo(const ALog: string);
  end;

implementation

uses SysUtils, UtilMethods, Math, ZhAPI;

{ TLog }

procedure TLogUtils.AppendLogTo(const ALog: string);
var
  f: TextFile;
begin
  try
    AssignFile(f, FLogFile);
    if FileExists(FLogFile) then
      Append(f)
    else
      Rewrite(f);
    Writeln(f, Format('[Version: %s] %s %s', [FExeVersion, DateTimeToStr(Now), ALog]));
    Flush(f);
  finally
    CloseFile(f);
  end;
end;

constructor TLogUtils.Create;
var
  vFormatSetting: TFormatSettings;
begin
  vFormatSetting.ShortDateFormat := 'yyyy/MM/dd';
  vFormatSetting.DateSeparator := '-';
  FLogFolder := GetAppFilePath + 'log';
  if not DirectoryExists(FLogFolder) then
    CreateDirectoryInDeep(FLogFolder);
  FLogFile := FLogFolder + '\' + DateToStr(Date, vFormatSetting) + '.log';

  FExeVersion := GetExeFileVersion(ParamStr(0));
end;

destructor TLogUtils.Destroy;
begin
  inherited;
end;

end.
