Delphi 로그 출력 레코드 클래스

6531 단어
{ ******************************************************* }
{    :  UntTIO.pas }
{     :2006-1-14 23:20:08 }
{            QQ 22900104 }
{   :    I/O   }
{ }
{ ******************************************************* }

unit UntTIO;

interface

uses
  Classes,
  SysUtils,
  ComCtrls,
  Graphics;

type
  T       = (
        _  ,
        _  ,
        _  ,
        _  ,
        _  );

type
  TGameLogFile = class
    private
      F_      : string;
      F_      : TextFile;

    public
      {               }
      constructor Create(
        sz       : string = '\Log\';
        b        : boolean = false);
      destructor Destroy; override;
      {            }

      procedure     (
        sz             : string;
        const Args     : array of const;
        const          : T       =     _  ); overload;
      procedure     (
        sz         : string;
        const      : T       =     _  ); overload;
  end;

  TEventShowed = procedure(ILogCon : string) of object;

  TIOer = class(TObject)
    private
      F_         : boolean;      //            
      F_         : TEventShowed; //                 
      F_         : boolean;      //         
      F_         : string;       //      
      F_         : TGameLogFile;
    protected
      F_     : TComponent; //   
      F_     : Word;       //           

      function     (
        sz         : string;
        const      : T       =     _  ) : Integer; virtual;

    public
      Enabled : boolean;

      function     (
        sz           : string;
        const Args   : array of const;
        const         : T       =     _  ) : Integer; overload;
      function     (
        sz         : string;
        const      : T       =     _  ) : Integer;
        overload;
      {                        }
      constructor Create(
        p       : TComponent;
        sz      : string = '');
      destructor Destroy; override;
      property      : Word
        read   F_    
        write  F_    ;
      property        : boolean
        read   F_      
        write  F_      ;
      property          : boolean
        read   F_        
        write  F_        ;
      property       : string
        read   F_     
        write  F_     ;
      property       : TEventShowed
        read   F_     
        write  F_     ;
  end;

implementation

uses
  StdCtrls,
  Forms;

const
  {      }
  CSplitStr = '===============================================================';
  ClogFileName = '.txt';
  { TGameLogFile }

procedure TGameLogFile.    (
  sz             : string;
  const Args     : array of const;
  const          : T       =     _  );
begin
      (Format(sz    , Args    ),     );
end;

procedure TGameLogFile.    (
  sz         : string;
  const      : T       =     _  );
begin
  Writeln(F_    , sz  );
  CloseFile(F_    );
  Append(F_    );
end;

constructor TGameLogFile.Create(
  sz       : string;
  b        : boolean);
begin
  F_      := sz       + FormatDateTime('yyyymmdd-hhnnss', Now()) + '.log';

  //            
  if not DirectoryExists(sz      ) then
  begin
    if not ForceDirectories(sz      ) then
    begin
      raise
        Exception.Create('     ,          ');
    end;
  end;

  //         ,      
  Assignfile(F_    , F_     );
  if ((FileExists(F_     )) and ( not b    )) then
    Append(F_    ) //   
  else
    ReWrite(F_    ); //   
end;

destructor TGameLogFile.Destroy;
begin
  try
    CloseFile(F_    );
  except
  end;
  inherited;
end;

{ TGameIO }

function TIOer.    (
  sz           : string;
  const Args   : array of const;
  const        : T       =     _  ) : Integer;
begin
  Result := 0;
  try
        (Format(sz    , Args  ),     );
  except
  end;
end;

function TIOer.    (
  sz        : string;
  const         : T       =     _  ) : Integer;
begin
  if F_       then
    sz   := DateTimeToStr(Now) + ' ' + sz  ;
  if F_         then
    sz   := sz   + #13#10 + F_     ;
  Result :=     (sz  ,     );
  if assigned(F_  ) then
    F_  .    (sz  );
  if assigned(F_     ) then
    F_     (sz  );
end;

constructor TIOer.Create(
  p       : TComponent;
  sz      : string);
begin
  F_         := 1000;
  F_         := True;
  F_         := false;
  F_         := CSplitStr;
  F_         := p  ;
  Enabled    := True;
  if sz      <> '' then
    F_   := TGameLogFile.Create(sz     );
end;

destructor TIOer.Destroy;
begin
  if assigned(F_  ) then
    F_  .Free;
  inherited;
end;

function TIOer.    (
  sz        : string;
  const         : T       =     _  ) : Integer;
var
  ListItem : TListItem;
begin
  Result := - 1;
  if Application.Terminated then
    exit;
  if ( not Enabled) then
    exit;
  if (F_     is TMemo) then
  begin
    Result := TMemo(F_    ).Lines.Add(sz  );
    if Result >= F_     then
      TMemo(F_    ).Clear
  end
  else if (F_     is TRichEdit) then
  begin
    case      of
          _  : TRichEdit(F_    ).SelAttributes.Color := clBlue;
          _  : TRichEdit(F_    ).SelAttributes.Color := clblack;
          _  : TRichEdit(F_    ).SelAttributes.Color := clblack;
          _  : TRichEdit(F_    ).SelAttributes.Color := clred;
          _  : TRichEdit(F_    ).SelAttributes.Color := clblack;
    end;

    Result := TRichEdit(F_    ).Lines.Add(sz  );
    if Result >= F_     then
      TRichEdit(F_    ).Clear
  end
  else if (F_     is TListBox) then
  begin
    Result := TListBox(F_    ).Items.Add(sz  );
    if Result >= F_     then
      TListBox(F_    ).Clear
  end
  else if (F_     is TListView) then
  begin
    ListItem            := TListView(F_    ).Items.Add;
    ListItem.Caption    := FormatDateTime('yyyy-mm-dd hh:nn:ss', Now);
    ListItem.ImageIndex := ord(    );
    ListItem.SubItems.Add(sz  );
    if TListView(F_    ).Items.Count >= F_     then
      TListView(F_    ).Items.Clear;
  end
  else
    raise Exception.Create('      :' + F_    .ClassName);
end;

end.

//    ,     Rich
var
  AppPath : string;
  Gio     : TIOer;
procedure TForm1.FormCreate(Sender : TObject);
begin
  AppPath := ExtractFilePath(Application.ExeName);
  Gio     := TIOer.Create(RichEdit1, AppPath + 'Log\');
  Gio.    ('         ...',     _  );
  Gio.    ('         2...',     _  );


좋은 웹페이지 즐겨찾기