嘰哩咕嚕

          君子如玉,上善若水

          FileStream 類實(shí)現(xiàn)日志

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

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


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

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

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

          //在這里創(chuàng)建一個(gè)日志文件
          procedure TGameLogFile.init(Iparth: string);
          var
            Ltep: string;
          begin
           if not DirectoryExists(FFileParth) then
              if not CreateDir(FFileParth) then begin
                raise Exception.Create('錯(cuò)誤的路徑,日志類對(duì)象不能被創(chuàng)建');
                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用來(lái)控制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;
            //讀寫(xiě)日志文件
            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) 評(píng)論(0)  編輯  收藏 所屬分類: delphi


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          公告

          瘋狂

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆分類(17)

          隨筆檔案(22)

          文章分類(1)

          文章檔案(1)

          工程教程

          牛人BLOG

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 澎湖县| 景德镇市| 广德县| 宁夏| 荥阳市| 松溪县| 台北市| 府谷县| 东港市| 五华县| 双柏县| 巴青县| 同德县| 香港 | 祥云县| 德阳市| 尖扎县| 延安市| 沐川县| 凯里市| 张家界市| 宜城市| 怀远县| 喀喇| 钟祥市| 贵溪市| 泊头市| 黄陵县| 平邑县| 浮山县| 大渡口区| 顺平县| 孝昌县| 克拉玛依市| 陆良县| 塔河县| 岱山县| 麻城市| 社旗县| 崇左市| 宣汉县|