嘰哩咕嚕

          君子如玉,上善若水

          FileStream 類實現日志

          FileStream類幾乎可以處理所有的文件操作.

          以下為一個日志類,除了配置不太靈活外,挺好用的.
          type
            TBuffer = array [0..2000] of char;
           
            TGameLogFile = class
            private
              FFullPath:string;//完整路徑,用這個路徑來判斷當前的打開的日志的大小.
              FileDate:TDateTime;
              FFileParth: string; //路徑
              FText: Text;
              FLogFileStream:TFileStream;
              FIsCreateToNew: boolean; //是否是每次啟動程序都創建新的記錄文件 否則就是當天只會有1個文件
              FIsControlFileSize:Boolean;//是否控制文件大小,true,超出文件大小時,重新創建一個log文件
            public
              {帶入日志文件存放的目錄位置}
              constructor Create(Iparth: string);
              destructor Destroy; override;
              {寫入內容即可自動記錄}
              procedure init(Iparth: string);
              procedure AddLog(Icon: string; const LogLevel: Integer = 0);
              property IsCreateToNew: boolean read FIsCreateToNew write FIsCreateToNew;
            end;
          implementation
          uses StdCtrls;
          const
            {分割符號}
            CSplitStr = '===============================================================';
            ClogFileName = '.log';
          { TGameLogFile }


          procedure TGameLogFile.AddLog(Icon: string; const LogLevel: integer = 0);
          var
            txt:string;
            buffer:TBuffer; //開一個2K的緩存
          begin
            try
              if FIsCreateToNew then
                if Date - FileDate >= 1 then    //超過一天.強制換掉日志文件
                begin
                  CloseFile(FText);
                  init(FFileParth);
                end;

              if FIsControlFileSize then
              begin
                if FLogFileStream.Size > 3 * 1000 * 1000 then    //這里的單位是M,有時間改成可配置
                  init(FFileParth); //重新切換一個日志
              end;
              
              StrCopy(buffer,PChar(Icon));
              FLogFileStream.Write(buffer,Length(Icon));//如果直接write(Icon,Length(Icon)),會產生亂碼.
            except
              IOResult;
            end;
          end;

          constructor TGameLogFile.Create(Iparth: string);
          begin
            FIsCreateToNew := false;
            FIsControlFileSize := not (FIsCreateToNew xor False);  //當FIsCreateToNew為true時,此變量為假
            FFileParth := Iparth;
            init(FFileParth);
          end;

          //在這里創建一個日志文件
          procedure TGameLogFile.init(Iparth: string);
          var
            Ltep: string;
          begin
           if not DirectoryExists(FFileParth) then
              if not CreateDir(FFileParth) then begin
                raise Exception.Create('錯誤的路徑,日志類對象不能被創建');
                exit;
              end;
            if FIsCreateToNew then begin
              Ltep := FormatDateTime('yyyymmddhhnnss', Now);
              FileClose(FileCreate(FFileParth + ltep + ClogFileName));
            end
            else
              Ltep := FormatDateTime('yyyymmddhhnnss', Now);
            if not FileExists(FFileParth + ltep + ClogFileName) then
              FileClose(FileCreate(FFileParth + ltep + ClogFileName));
            FileDate := Date;
            FFullPath := FFileParth + ltep + ClogFileName;
            //此處改用TFileStream用來控制Log日志文件的大小  2011年8月24日9:28:25 ddz
            //AssignFile(FText, FFileParth + ltep + ClogFileName);
            if Assigned(FLogFileStream) then
               FLogFileStream.Free;
            //新建日志文件.
            FLogFileStream := TFileStream.Create(FFullPath,fmCreate    or fmShareDenyNone);
            FLogFileStream.free;
            //讀寫日志文件
            FLogFileStream := TFileStream.Create(FFullPath,fmOpenReadWrite    or fmShareDenyNone);
          end;

          destructor TGameLogFile.Destroy;
          begin
            try
              if Assigned(FLogFileStream) then
                FreeAndNil(FLogFileStream);
            except
            end;
            inherited;
          end;
          end.

          posted on 2011-08-24 06:52 嘰哩咕嚕 閱讀(459) 評論(0)  編輯  收藏 所屬分類: delphi


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告

          瘋狂

          導航

          <2011年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統計

          常用鏈接

          留言簿

          隨筆分類(17)

          隨筆檔案(22)

          文章分類(1)

          文章檔案(1)

          工程教程

          牛人BLOG

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 义乌市| 彩票| 句容市| 黄大仙区| 莎车县| 南阳市| 乐都县| 临沭县| 万荣县| 绥化市| 会同县| 海阳市| 伊川县| 湛江市| 固镇县| 宜兴市| 海兴县| 富平县| 修文县| 张家界市| 公安县| 大连市| 河东区| 平远县| 尚义县| 张家界市| 友谊县| 醴陵市| 香港| 仁布县| 犍为县| 高青县| 永登县| 武宣县| 武陟县| 岚皋县| 河间市| 宁安市| 含山县| 兴城市| 灵丘县|