error.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include <apt-pkg/macros.h>
  35. #include <string>
  36. class GlobalError
  37. {
  38. struct Item
  39. {
  40. std::string Text;
  41. bool Error;
  42. Item *Next;
  43. };
  44. Item *List;
  45. bool PendingFlag;
  46. void Insert(Item *I);
  47. public:
  48. // Call to generate an error from a library call.
  49. bool Errno(const char *Function,const char *Description,...) __like_printf(3) __cold;
  50. bool WarningE(const char *Function,const char *Description,...) __like_printf(3) __cold;
  51. /* A warning should be considered less severe than an error, and may be
  52. ignored by the client. */
  53. bool Error(const char *Description,...) __like_printf(2) __cold;
  54. bool Warning(const char *Description,...) __like_printf(2) __cold;
  55. // Simple accessors
  56. inline bool PendingError() {return PendingFlag;};
  57. inline bool empty() {return List == 0;};
  58. bool PopMessage(std::string &Text);
  59. void Discard();
  60. // Usefull routine to dump to cerr
  61. void DumpErrors();
  62. GlobalError();
  63. };
  64. // The 'extra-ansi' syntax is used to help with collisions.
  65. GlobalError *_GetErrorObj();
  66. #define _error _GetErrorObj()
  67. #endif