Type TBaseDebug = Class Private FStartTime, FEndTime: Cardinal; FBugShowForm: TForm; FLoger: TDebugLog; FTimer:TTimer; FtrackBar: TTrackBar; FGroupBox:TGroupBox; FShower: TMemo; Procedure FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState); Procedure TimerOnTimer(Iobj:TObject);//自动设置快捷键的Timer事件 {加载热键系统 Alt+Shift+ctrl+o 是打开debug窗体 +p是打开/关闭自动记录功能-1开 0关} Procedure InitDebugSystem(ImainForm: TForm); Procedure TrackOnTrack(Iobj:TObject); Public {是否在程序结束的时候自动保存除错信息 默认是False} WantAutoSaveLog: boolean; {开始记录时间} Procedure StartLogTime; {停止记录并且返回时间差单位毫秒} Function EndLogTIme: Cardinal; {弹出变量的值} Function ShowVar(Ivar: Variant): Variant; {添加到Log容器} Function AddLogShower(Ivar: Variant): Variant; Overload; Function AddLogShower(IDesc: String; Ivar: Variant): Variant; Overload; Function AddLogShower(IStrings:TStringList): Variant; Overload; {显示Debug窗体} Procedure ShowDebugform; {将所有记录的东东保存成日志} Procedure SaveLog(IfileName: String = 'LogFile.log'); Constructor Create; Destructor Destroy; Override; End; Var Gob_Debug: TBaseDebug; Implementation
{ TMyInterfacedObject }
Function TMyInterfaceObject._AddRef: Integer; Begin Result := 0; End;
Function TMyInterfaceObject._Release: Integer; Begin Result := 0; End;
Function TMyInterfaceObject.QueryInterface(Const IID: TGUID; Out Obj): HResult; Begin Result := 0; End;
{ TDebugLog }
Function TDebugLog.AddShow(ICon: String): Integer; Begin If FIsAddTime Then ICon := DateTimeToStr(Now) + ' ' + Icon; If FIsNeedSplt Then ICon := ICon + #13#10 + FSplitChar; Result := DoAdd(ICon); If assigned(FLog) Then FLog.AddLog(ICon); If Assigned(FAfterShowed) Then FAfterShowed(ICon); End;
Destructor TDebugLog.Destroy; Begin If assigned(FLog) Then FLog.Free; Inherited; End;
Function TDebugLog.DoAdd(Icon: String): Integer; Begin If (FShower Is TMemo) Then Begin Result := TMemo(FShower).Lines.Add(Icon); If Result >= FClearTager Then TMemo(FShower).Clear End Else If (FShower Is TListBox) Then Begin Result := TListBox(FShower).Items.Add(Icon); If Result >= FClearTager Then TListBox(FShower).Clear End Else Raise Exception.Create('默认容器错误:' + FShower.ClassName);
Constructor TDebugLogFile.Create(Iparth: String); Var Ltep: String; Begin FIsCreateToNew := True; FFileParth := Iparth; If Not DirectoryExists(FFileParth) Then If Not CreateDir(FFileParth) Then Begin Raise Exception.Create('错误的路径,日值类对象不能被创建'); exit; End; Ltep := FormatDateTime('yyyymmddhhnnss', Now); FileClose(FileCreate(FFileParth + ltep + ClogFileName)); AssignFile(FText, FFileParth + ltep + ClogFileName); End;
Destructor TDebugLogFile.Destroy; Begin Try CloseFile(FText); Except End; Inherited; End;
{ TBaseDebug }
Function TBaseDebug.AddLogShower(Ivar: Variant): Variant; Begin Try Result := Ivar; FLoger.Shower.AddShow(Ivar); Except On e: Exception Do AddLogShower(e.Message); End; End;
Function TBaseDebug.AddLogShower(IDesc: String; Ivar: Variant): Variant; Var Ltep: String; Begin Try Ltep := Ivar; Result := Ivar; FLoger.Shower.AddShow('描述<' + IDesc + '> <值: ' + Ltep + '>'); Except On e: Exception Do AddLogShower(e.Message); End; End;
Destructor TBaseDebug.Destroy; Begin AddLogShower(Format('程序结束时间(%s)', [DateTimeToStr(now)])); If WantAutoSaveLog Then SaveLog(); FtrackBar.Free; FGroupBox.Free; FLoger.Free; FShower.Free; FBugShowForm.Free; Inherited; End;
Function TBaseDebug.EndLogTIme: Cardinal; Begin FEndTime := GetTickCount; Result := FEndTime - FStartTime; End;
Procedure TBaseDebug.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState); Begin If (ssAlt In Shift) Then Begin Case Key Of // ord('o'), Ord('O'): Begin FBugShowForm.Visible := Not FBugShowForm.Visible; Application.MainForm.SetFocus; End; ord('P'), ord('p'): Begin WantAutoSaveLog := Not WantAutoSaveLog; AddLogShower('当前自动保存的状态改为: '); AddLogShower(WantAutoSaveLog) End; End; // case End; End;
procedure TBaseDebug.TimerOnTimer(Iobj:TObject); begin If Application.MainForm<>nil Then Begin InitDebugSystem(Application.MainForm); TTimer(Iobj).Enabled:=False; TTimer(Iobj).Free; End; end;
Procedure TBaseDebug.ShowDebugform; Begin FBugShowForm.Show; End;
Function TBaseDebug.ShowVar(Ivar: Variant): Variant; Var S: String; Begin Try Result := Ivar; s := Ivar; MessageBox(0, Pchar(s), 'Debug', 0); Except On e: Exception Do AddLogShower(e.Message); End; End;
Procedure TBaseDebug.StartLogTime; Begin FStartTime := GetTickCount; End;
procedure TBaseDebug.TrackOnTrack(Iobj: TObject); begin FBugShowForm.AlphaBlendValue:=TTrackBar(Iobj).Position; end;
function TBaseDebug.AddLogShower(IStrings: TStringList): Variant; Var I: Integer; begin AddLogShower('>>>开始显示StringList'); For I := 0 To IStrings.Count - 1 Do AddLogShower(IStrings.Strings[i]); AddLogShower('显示StringList结束<<<'); end;