program EFXEmailServer;

uses
  System.SyncObjs,
  System.SysUtils,
  Vcl.StdCtrls,
  IniFiles,
  Vcl.Forms,
  Server.Container in 'Server.Container.pas' {FServerContainer: TDataModule},
  Main in 'Main.pas' {FMain},
  Email.Service in 'Email.Service.pas',
  Email.ServiceImpl in 'Email.ServiceImpl.pas',
  Common.Logging in 'Common.Logging.pas',
  Data in 'Data.pas' {FData},
  Database in 'Database.pas' {FDatabaseModule: TDataModule};

type
  TMemoLogAppender = class( TInterfacedObject, ILogAppender )
  private
    FLogLevel: Integer;
    FLogMemo: TMemo;
    FCriticalSection: TCriticalSection;
  public
    constructor Create(ALogLevel: Integer; ALogMemo: TMemo);
    destructor Destroy; override;
    procedure Send(logLevel: Integer; Log: ILog);
  end;

  TFileLogAppender = class( TInterfacedObject, ILogAppender )
  private
    FLogLevel: Integer;
    FFilename: string;
    FCriticalSection: TCriticalSection;
  public
    constructor Create(ALogLevel: Integer; AFilename: string);
    destructor Destroy; override;
    procedure Send(logLevel: Integer; Log: ILog);
  end;

{ TMemoLogAppender }
constructor TMemoLogAppender.Create(ALogLevel: Integer; ALogMemo: TMemo);
begin
  FLogLevel := ALogLevel;
  FLogMemo := ALogMemo;
  FCriticalSection := TCriticalSection.Create;
end;

destructor TMemoLogAppender.Destroy;
begin
  FCriticalSection.Free;
  inherited;
end;

procedure TMemoLogAppender.Send(logLevel: Integer; Log: ILog);
var
  FormattedMessage: string;
  LogTime: TDateTime;
  LogMsg: string;

begin
  FCriticalSection.Acquire;
  try
    LogTime := Now;

    FormattedMessage := FormatDateTime('[yyyy-mm-dd HH:nn:ss.zzz]', LogTime);
    LogMsg := Log.GetMessage;
    if LogMsg.IsEmpty then
      FormattedMessage := ''
    else
      FormattedMessage := FormattedMessage + '[' + IntToStr(logLevel) +'] ' + LogMsg;

    if logLevel <= FLogLevel then
      FLogMemo.Lines.Add( FormattedMessage );
  finally
    FCriticalSection.Release;
  end;
end;

{ TFileLogAppender }
constructor TFileLogAppender.Create(ALogLevel: integer; AFilename: string);
var
  iniFile: TIniFile;
  fileNum: integer;
begin
  FLogLevel := ALogLevel;
  FCriticalSection := TCriticalSection.Create;
  IniFile := TIniFile.Create( ChangeFileExt( Application.ExeName, '.ini' ) );
  try
    fileNum := IniFile.ReadInteger( 'Options', 'LogFileNum', 0 );
//    FFilename := AFilename + Format('%.*d',[4, fileNum]);
    FFilename := AFilename + Format('%.4d',[fileNum]);
    iniFile.WriteInteger( 'Options', 'LogFileNum', fileNum + 1 );
  finally
    iniFile.Free;
  end;
end;

destructor TFileLogAppender.Destroy;
begin
  FCriticalSection.Free;
  inherited;
end;

procedure TFileLogAppender.Send(logLevel: integer; Log: ILog);
var
  FormattedMessage: string;
  LogFile: string;
  LogTime: TDateTime;
  LogMsg: string;
  FLogFile: TextFile;

begin
  FCriticalSection.Acquire;
  try
    LogTime := Now;
    LogFile := ExtractFilePath(Application.ExeName) + FFilename + '.log';

    FormattedMessage := FormatDateTime('[yyyy-mm-dd HH:nn:ss.zzz]', LogTime);
    LogMsg := Log.GetMessage;
    if LogMsg.IsEmpty then
      FormattedMessage := ''
    else
      FormattedMessage := FormattedMessage + '[' + IntToStr(logLevel) +'] ' + LogMsg;

    try
      AssignFile( FLogFile, LogFile );
      if logLevel <= FLogLevel then
      begin
        if FileExists(LogFile) then
          Append( FLogFile )
        else
          ReWrite( FLogFile );
        WriteLn( FLogFile, FormattedMessage );
      end;
    finally
      CloseFile(FLogFile);
    end;
  finally
    FCriticalSection.Release;
  end;
end;

{$R *.res}

begin
  ReportMemoryLeaksOnShutdown := True;
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TFServerContainer, FServerContainer);
  Application.CreateForm(TFMain, FMain);
  Application.CreateForm(TFData, FData);
  Application.CreateForm(TFDatabaseModule, FDatabaseModule);
  Logger.AddAppender(TMemoLogAppender.Create( 5, FMain.memoinfo ));
  Application.Run;
end.
