打印本文 打印本文 关闭窗口 关闭窗口
一个简单Tracer类,用来为应用写入跟踪
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1629  更新时间:2009/4/23 18:34:55  文章录入:mintao  责任编辑:mintao
 aInfo: ITraceInfo);
begin
    if FOutput = nil then raise Exception.CreateFmt(''''没有创建输出目标%s!!!'''', []);
    FOutput.Write(aInfo);
end;

{ TStringTI }

constructor TStringTI.Create(data: string);
begin
    FData := Data;
end;

function TStringTI.ToString: string;
begin
    Result := FData;
end;

{ TStringLog }

constructor TFileLog.Create(const FileName: string);
begin
    FLogFile := FileName;
end;

procedure TFileLog.Write(const aInfo: ITraceInfo);
begin
    if not FileExists(FLogFile) then FileClose(FileCreate(FLogFile));
    with TStringList.Create do
    begin
        try
            LoadFromFile(FLogFile);
            Add(aInfo.ToString);
            SaveToFile(FLogFile);
        finally
            Free;
        end;
    end;
end;

{ TDatabaseLog }

constructor TDatabaseLog.Create(WriteProc: TProcStr);
begin
    FWriteProc := WriteProc;
    if not Assigned(FWriteProc) then raise Exception.CreateFmt(''''没有传入正确的写入跟踪方法%s!!!'''', []);
end;

procedure TDatabaseLog.Write(const aInfo: ITraceInfo);
begin
    FWriteProc(aInfo.ToString);
end;

end.

===================测试代码==========================
{******************************************************************************}
{                                                                              }
{          测试名称:                                                          }
{          作    者:                                                          }
{          版    本:                                                          }
{          说    明:                                                          }
{          备    注:                                                          }
{                                                                              }
{******************************************************************************}

unit test.com.sunset.app.tracer;

interface

uses
  Windows, SysUtils, Classes, TestFramework, TestExtensions,
  com.sunset.app.tracer;

type
  TTest = class(TTestCase)
  protected
    procedure SetUp; override;
    procedure TearDown; override;

  published
    procedure TestTracer;
  end;

implementation

procedure TTest.Setup;
begin

end;

procedure TTest.TearDown;
begin

end;

procedure TTest.TestTracer;
var
    tracer:TTracer;
    aInfo:ITraceInfo;
const
    testData =''''adfadfdasf'''';
    testFile =''''d:\2.txt'''';
begin
    aInfo := TStringTI.Create(testData);
    Tracer := TTracer.Create(TFileLog.Create(testfile));
    Tracer.Write(aInfo);
    Tracer.Free;
    aInfo := nil;
    with TStringList.Create do
    begin
        LoadFromFile(testfile);
        Check(Strings[Count -1] = testData);
        Free;
    end;
end;

initialization
  TestFramework.RegisterTest(TTest.Suite);

end.

一竿残照@金棣.net

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口