error.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: error.h,v 1.8 2001/05/07 05:06:52 jgg Exp $
  4. /* ######################################################################
  5. Global Erorr Class - Global error mechanism
  6. This class has a single global instance. When a function needs to
  7. generate an error condition, such as a read error, it calls a member
  8. in this class to add the error to a stack of errors.
  9. By using a stack the problem with a scheme like errno is removed and
  10. it allows a very detailed account of what went wrong to be transmitted
  11. to the UI for display. (Errno has problems because each function sets
  12. errno to 0 if it didn't have an error thus eraseing erno in the process
  13. of cleanup)
  14. Several predefined error generators are provided to handle common
  15. things like errno. The general idea is that all methods return a bool.
  16. If the bool is true then things are OK, if it is false then things
  17. should start being undone and the stack should unwind under program
  18. control.
  19. A Warning should not force the return of false. Things did not fail, but
  20. they might have had unexpected problems. Errors are stored in a FIFO
  21. so Pop will return the first item..
  22. I have some thoughts about extending this into a more general UI<->
  23. Engine interface, ie allowing the Engine to say 'The disk is full' in
  24. a dialog that says 'Panic' and 'Retry'.. The error generator functions
  25. like errno, Warning and Error return false always so this is normal:
  26. if (open(..))
  27. return _error->Errno(..);
  28. This source is placed in the Public Domain, do with it what you will
  29. It was originally written by Jason Gunthorpe.
  30. ##################################################################### */
  31. /*}}}*/
  32. #ifndef PKGLIB_ERROR_H
  33. #define PKGLIB_ERROR_H
  34. #ifdef __GNUG__
  35. // Methods have a hidden this parameter that is visible to this attribute
  36. #define APT_MFORMAT1 __attribute__ ((format (printf, 2, 3)))
  37. #define APT_MFORMAT2 __attribute__ ((format (printf, 3, 4)))
  38. #else
  39. #define APT_MFORMAT1
  40. #define APT_MFORMAT2
  41. #endif
  42. #include <string>
  43. #include <system.h>
  44. using std::string;
  45. class GlobalError
  46. {
  47. struct Item
  48. {
  49. string Text;
  50. bool Error;
  51. Item *Next;
  52. };
  53. Item *List;
  54. bool PendingFlag;
  55. void Insert(Item *I);
  56. public:
  57. // Call to generate an error from a library call.
  58. bool Errno(const char *Function,const char *Description,...) APT_MFORMAT2 __cold;
  59. bool WarningE(const char *Function,const char *Description,...) APT_MFORMAT2 __cold;
  60. /* A warning should be considered less severe than an error, and may be
  61. ignored by the client. */
  62. bool Error(const char *Description,...) APT_MFORMAT1 __cold;
  63. bool Warning(const char *Description,...) APT_MFORMAT1 __cold;
  64. // Simple accessors
  65. inline bool PendingError() {return PendingFlag;};
  66. inline bool empty() {return List == 0;};
  67. bool PopMessage(string &Text);
  68. void Discard();
  69. // Usefull routine to dump to cerr
  70. void DumpErrors();
  71. GlobalError();
  72. };
  73. // The 'extra-ansi' syntax is used to help with collisions.
  74. GlobalError *_GetErrorObj();
  75. #define _error _GetErrorObj()
  76. #undef APT_MFORMAT1
  77. #undef APT_MFORMAT2
  78. #endif