msghandler.pas 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. {
  2. FPCRes - Free Pascal Resource Converter
  3. Part of the Free Pascal distribution
  4. Copyright (C) 2008 by Giulio Bernardi
  5. Output messages handler
  6. See the file COPYING, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. unit msghandler;
  13. {$MODE OBJFPC}
  14. interface
  15. uses
  16. Classes, SysUtils;
  17. type
  18. { TMessages }
  19. TMessages = class
  20. private
  21. fVerboseSet : boolean;
  22. fVerbose : boolean;
  23. fVerbCache : TStringList;
  24. fStdOut : text;
  25. fStdErr : text;
  26. procedure SetVerbose(const aValue : boolean);
  27. protected
  28. public
  29. constructor Create;
  30. destructor Destroy; override;
  31. procedure DoError(const aMsg : string);
  32. procedure DoVerbose(const aMsg : string);
  33. procedure Flush;
  34. property Verbose : boolean read fVerbose write SetVerbose;
  35. end;
  36. var Messages : TMessages;
  37. procedure Halt(errnum:Longint);
  38. implementation
  39. procedure Halt(errnum:Longint);
  40. begin
  41. Messages.Flush;
  42. System.Halt(errnum);
  43. end;
  44. { TMessages }
  45. procedure TMessages.SetVerbose(const aValue: boolean);
  46. var i : integer;
  47. begin
  48. fVerbose:=aValue;
  49. if fVerboseSet then exit;
  50. fVerboseSet:=true;
  51. if fVerbose then //output all verbose messages we didn't output before
  52. for i:=0 to fVerbCache.Count-1 do
  53. writeln(fStdOut,'Debug: '+fVerbCache[i]);
  54. FreeAndNil(fVerbCache);
  55. end;
  56. constructor TMessages.Create;
  57. begin
  58. fVerbose:=false;
  59. fVerboseSet:=false;
  60. fVerbCache:=TStringList.Create;
  61. fStdOut:=stdout;
  62. fStdErr:=stderr;
  63. end;
  64. destructor TMessages.Destroy;
  65. begin
  66. if fVerbCache<>nil then
  67. fVerbCache.Free;
  68. end;
  69. procedure TMessages.DoError(const aMsg: string);
  70. begin
  71. writeln(fStdErr,'Error: '+aMsg);
  72. end;
  73. procedure TMessages.DoVerbose(const aMsg: string);
  74. begin
  75. if not fVerboseSet then
  76. begin
  77. fVerbCache.Add(aMsg);
  78. exit;
  79. end;
  80. if fVerbose then
  81. writeln(fStdOut,'Debug: '+aMsg);
  82. end;
  83. procedure TMessages.Flush;
  84. begin
  85. System.Flush(fStdOut);
  86. System.Flush(fStdErr);
  87. end;
  88. initialization
  89. Messages:=TMessages.Create;
  90. finalization
  91. Messages.Free;
  92. end.