unit verbose;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Buttons, ToolWin, ExtCtrls;

type
  TForm4 = class(TForm)
    RichEdit: TRichEdit;
    ToolBar1: TToolBar;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    ToolButton1: TToolButton;
    SpeedButton3: TSpeedButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Panel1: TPanel;
    Button1: TButton;
    SpeedButton4: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
  private
    { Private declarations }
  public
   Rec: Boolean;
   RecBypass: Integer;
   AddColor: TColor;
   DebugLevel: Integer;
   Indent: String;
   procedure Add(Msg: String; InfoLevel: Integer; MsgType: TMsgDlgType = mtInformation);
   procedure Append(Msg: String; InfoLevel: Integer; MsgType: TMsgDlgType = mtinformation);
   procedure IndentAdd;
   procedure IndentDel;
  end;

Const
 // Debug level
 dlAll = 0;
 dlConfig = 1;
 dlCore = 5;
 dlPlugins = 10;
 dlError = 15;
 dlCrusial = 20;

var
  Form4: TForm4;

implementation

{$R *.dfm}

Uses CM2PlugInTypes;

{ TForm4 }

Function AddZero(W: Word; Digits: Byte): String;
Var
 I: Integer;
 S: String;
Begin
 S := IntToStr(W);
 For I := length(S) to Digits-1 do begin
  S := '0' + S;
 End;
 Result := S;
End;

procedure TForm4.Add(Msg: String; InfoLevel: Integer; MsgType: TMsgDlgType = mtInformation);
Var
 H, M, S, MSec: Word;
begin
 If InfoLevel < DebugLevel then exit;
 If not Rec then begin
  Inc(RecBypass);
  exit;
 End;
 DecodeTime(Now, H, M, S, MSec);
 msg := AddZero(H, 2) + ':' + AddZero(M, 2) + ':' + AddZero(S, 2) + '.' + AddZero(Msec, 4) + ' - ' + indent + msg;
 Try
   RichEdit.SelLength := 0;
   RichEdit.SelStart := length(RichEdit.lines.Text);
   Case MsgType of
    mtWarning: Richedit.SelAttributes.Color := clYellow;
    mtError: Richedit.SelAttributes.Color := clRed;
    mtInformation: Begin
                    If AddColor = clteal then AddColor := clblue
                     else AddColor := clteal;
                    Richedit.SelAttributes.Color := AddColor;;
                   End;
    mtCustom: RichEdit.SelAttributes.Color := clFuchsia;
   End;
   RichEdit.Lines.Add(msg);
 except
   RichEdit.Lines.Add(msg);
 end;
// RichEdit.Update;
 Application.ProcessMessages;
end;

procedure TForm4.Append(Msg: String; InfoLevel: Integer; MsgType: TMsgDlgType = mtinformation);
Var
 S: String;
begin
 If InfoLevel < DebugLevel then exit;
 If not Rec then begin
  Inc(RecBypass);
  exit;
 End;
 Try
   RichEdit.SelLength := 0;
   RichEdit.SelStart := length(RichEdit.lines.Text);
   Case MsgType of
    mtWarning: Richedit.SelAttributes.Color := clYellow;
    mtError: Richedit.SelAttributes.Color := clRed;
    mtInformation: Richedit.SelAttributes.Color := clwhite;
   End;
   S := Richedit.Lines[Richedit.Lines.count-1] + msg;
   Richedit.Lines[Richedit.Lines.count-1] := S;
 except
   RichEdit.Lines.Add(msg);
 end;
 RichEdit.Update;
end;

procedure TForm4.SpeedButton1Click(Sender: TObject);
begin
 Caption := 'Verbose';
 Rec := True;
 If RecByPass > 0 Then begin
  Add('Failed to record ' + IntToStr(RecByPass) + ' commands', dlcrusial, mtwarning);
 End;
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
 Rec := true;
 Indent := '';
end;

procedure TForm4.SpeedButton2Click(Sender: TObject);
begin
 If Rec = false then exit;
 Caption := 'Verbose [Paused]';
 Rec := False;
 RecByPass := 0;
end;

procedure TForm4.SpeedButton3Click(Sender: TObject);
begin
 Richedit.Lines.SaveToFile('VerboseDUMP.rtf');
end;

procedure TForm4.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 Richedit.Lines.SaveToFile('VerboseDUMP.rtf');
 application.terminate;
end;

procedure TForm4.ToolButton3Click(Sender: TObject);
Var
 S: String;
begin
 ToolButton3.Down := False;
 ToolButton4.Down := False;
 ToolButton5.Down := False;
 ToolButton6.Down := False;
 ToolButton7.Down := False;
 (sender as TToolButton).Down := True;
 Case (sender as TToolButton).Tag of
  1: Begin
      DebugLevel := dlAll;
      S := 'All';
     end;
  2: Begin
      DebugLevel := dlCore;
      S := 'Core';
     end;
  3: begin
      DebugLevel := dlPlugins;
      S := 'Plugins';
     end;
  4: begin
      DebugLevel := dlError;
      S := 'Errors Only';
     end;
  5: Begin
      DebugLevel := dlCrusial;
      S := 'Crusial';
     end;
 end;
 Self.Add('Debuglevel changed to "' + s + '"', dlcrusial, mtwarning);
end;

procedure TForm4.Button1Click(Sender: TObject);
begin
 Debug := False;
 hide;
end;

procedure TForm4.IndentAdd;
begin
 Indent := Indent + ' ';
end;

procedure TForm4.IndentDel;
begin
 Indent := Copy(Indent, 1, length(indent) - 1);
end;

procedure TForm4.SpeedButton4Click(Sender: TObject);
Var
 S: String;
begin
 S := '';
 If not inputquery('Verbose Window', 'Add Marker', S) then exit;
 Self.Add('Custom Marker: ' + S, dlCrusial, mtCustom);
end;

end.
