summaryrefslogtreecommitdiff
path: root/fpcsrc/utils/fpcreslipo/msghandler.pp
blob: 17baf20e149819f9bdf63e0e45ce9f9db698a866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{

    FPCResLipo - Free Pascal External Resource Thinner
    Part of the Free Pascal distribution
    Copyright (C) 2008 by Giulio Bernardi

    Output messages handler

    See the file COPYING, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

unit msghandler;

{$MODE OBJFPC}

interface

uses
  Classes, SysUtils;

type

  { TMessages }

  TMessages = class
  private
    fVerboseSet : boolean;
    fVerbose : boolean;
    fVerbCache : TStringList;
    fStdOut : text;
    fStdErr : text;
    procedure SetVerbose(const aValue : boolean);
  protected
  public
    constructor Create;
    destructor Destroy; override;
    procedure DoError(const aMsg : string);
    procedure DoVerbose(const aMsg : string);
    property Verbose : boolean read fVerbose write SetVerbose;
  end;
  
var Messages : TMessages;

implementation

{ TMessages }

procedure TMessages.SetVerbose(const aValue: boolean);
var i : integer;
begin
  fVerbose:=aValue;
  if fVerboseSet then exit;

  fVerboseSet:=true;
  if fVerbose then //output all verbose messages we didn't output before
    for i:=0 to fVerbCache.Count-1 do
      writeln(fStdOut,'Debug: '+fVerbCache[i]);

  FreeAndNil(fVerbCache);
end;

constructor TMessages.Create;
begin
  fVerbose:=false;
  fVerboseSet:=false;
  fVerbCache:=TStringList.Create;
  fStdOut:=stdout;
  fStdErr:=stderr;
end;

destructor TMessages.Destroy;
begin
  if fVerbCache<>nil then
    fVerbCache.Free;
end;

procedure TMessages.DoError(const aMsg: string);
begin
  writeln(fStdErr,'Error: '+aMsg);
end;

procedure TMessages.DoVerbose(const aMsg: string);
begin
  if not fVerboseSet then
  begin
    fVerbCache.Add(aMsg);
    exit;
  end;
  if fVerbose then
    writeln(fStdOut,'Debug: '+aMsg);
end;

initialization
  Messages:=TMessages.Create;


finalization
  Messages.Free;

end.