error.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <iostream>
  36. #include <list>
  37. #include <string>
  38. #include <stdarg.h>
  39. class GlobalError /*{{{*/
  40. {
  41. public: /*{{{*/
  42. /** \brief a message can have one of following severity */
  43. enum MsgType {
  44. /** \brief Message will be printed instantly as it is likely that
  45. this error will lead to a complete crash */
  46. FATAL = 40,
  47. /** \brief An error does hinder the correct execution and should be corrected */
  48. ERROR = 30,
  49. /** \brief indicates problem that can lead to errors later on */
  50. WARNING = 20,
  51. /** \brief deprecation warnings, old fallback behavior, … */
  52. NOTICE = 10,
  53. /** \brief for developers only in areas it is hard to print something directly */
  54. DEBUG = 0
  55. };
  56. /** \brief add a fatal error message with errno to the list
  57. *
  58. * \param Function name of the function generating the error
  59. * \param Description format string for the error message
  60. *
  61. * \return \b false
  62. */
  63. bool FatalE(const char *Function,const char *Description,...) __like_printf(3) __cold;
  64. /** \brief add an Error message with errno to the list
  65. *
  66. * \param Function name of the function generating the error
  67. * \param Description format string for the error message
  68. *
  69. * \return \b false
  70. */
  71. bool Errno(const char *Function,const char *Description,...) __like_printf(3) __cold;
  72. /** \brief add a warning message with errno to the list
  73. *
  74. * A warning should be considered less severe than an error and
  75. * may be ignored by the client.
  76. *
  77. * \param Function Name of the function generates the warning.
  78. * \param Description Format string for the warning message.
  79. *
  80. * \return \b false
  81. */
  82. bool WarningE(const char *Function,const char *Description,...) __like_printf(3) __cold;
  83. /** \brief add a notice message with errno to the list
  84. *
  85. * \param Function name of the function generating the error
  86. * \param Description format string for the error message
  87. *
  88. * \return \b false
  89. */
  90. bool NoticeE(const char *Function,const char *Description,...) __like_printf(3) __cold;
  91. /** \brief add a debug message with errno to the list
  92. *
  93. * \param Function name of the function generating the error
  94. * \param Description format string for the error message
  95. *
  96. * \return \b false
  97. */
  98. bool DebugE(const char *Function,const char *Description,...) __like_printf(3) __cold;
  99. /** \brief adds an errno message with the given type
  100. *
  101. * \param type of the error message
  102. * \param Function which failed
  103. * \param Description of the error
  104. */
  105. bool InsertErrno(MsgType const &type, const char* Function,
  106. const char* Description,...) __like_printf(4) __cold;
  107. /** \brief adds an errno message with the given type
  108. *
  109. * args needs to be initialized with va_start and terminated
  110. * with va_end by the caller. msgSize is also an out-parameter
  111. * in case the msgSize was not enough to store the complete message.
  112. *
  113. * \param type of the error message
  114. * \param Function which failed
  115. * \param Description is the format string for args
  116. * \param args list from a printf-like function
  117. * \param errsv is the errno the error is for
  118. * \param msgSize is the size of the char[] used to store message
  119. * \return true if the message was added, false if not - the caller
  120. * should call this method again in that case
  121. */
  122. bool InsertErrno(MsgType type, const char* Function,
  123. const char* Description, va_list &args,
  124. int const errsv, size_t &msgSize);
  125. /** \brief add an fatal error message to the list
  126. *
  127. * Most of the stuff we consider as "error" is also "fatal" for
  128. * the user as the application will not have the expected result,
  129. * but a fatal message here means that it gets printed directly
  130. * to stderr in addiction to adding it to the list as the error
  131. * leads sometimes to crashes and a maybe duplicated message
  132. * is better than "Segfault" as the only displayed text
  133. *
  134. * \param Description Format string for the fatal error message.
  135. *
  136. * \return \b false
  137. */
  138. bool Fatal(const char *Description,...) __like_printf(2) __cold;
  139. /** \brief add an Error message to the list
  140. *
  141. * \param Description Format string for the error message.
  142. *
  143. * \return \b false
  144. */
  145. bool Error(const char *Description,...) __like_printf(2) __cold;
  146. /** \brief add a warning message to the list
  147. *
  148. * A warning should be considered less severe than an error and
  149. * may be ignored by the client.
  150. *
  151. * \param Description Format string for the message
  152. *
  153. * \return \b false
  154. */
  155. bool Warning(const char *Description,...) __like_printf(2) __cold;
  156. /** \brief add a notice message to the list
  157. *
  158. * A notice should be considered less severe than an error or a
  159. * warning and can be ignored by the client without further problems
  160. * for some times, but he should consider fixing the problem.
  161. * This error type can be used for e.g. deprecation warnings of options.
  162. *
  163. * \param Description Format string for the message
  164. *
  165. * \return \b false
  166. */
  167. bool Notice(const char *Description,...) __like_printf(2) __cold;
  168. /** \brief add a debug message to the list
  169. *
  170. * \param Description Format string for the message
  171. *
  172. * \return \b false
  173. */
  174. bool Debug(const char *Description,...) __like_printf(2) __cold;
  175. /** \brief adds an error message with the given type
  176. *
  177. * \param type of the error message
  178. * \param Description of the error
  179. */
  180. bool Insert(MsgType const &type, const char* Description,...) __like_printf(3) __cold;
  181. /** \brief adds an error message with the given type
  182. *
  183. * args needs to be initialized with va_start and terminated
  184. * with va_end by the caller. msgSize is also an out-parameter
  185. * in case the msgSize was not enough to store the complete message.
  186. *
  187. * \param type of the error message
  188. * \param Description is the format string for args
  189. * \param args list from a printf-like function
  190. * \param msgSize is the size of the char[] used to store message
  191. * \return true if the message was added, false if not - the caller
  192. * should call this method again in that case
  193. */
  194. bool Insert(MsgType type, const char* Description,
  195. va_list &args, size_t &msgSize) __cold;
  196. /** \brief is an error in the list?
  197. *
  198. * \return \b true if an error is included in the list, \b false otherwise
  199. */
  200. inline bool PendingError() const {return PendingFlag;};
  201. /** \brief is the list empty?
  202. *
  203. * The default checks if the list is empty or contains only notices,
  204. * if you want to check if also no notices happened set the parameter
  205. * flag to \b false.
  206. *
  207. * \param WithoutNotice does notices count, default is \b true, so no
  208. *
  209. * \return \b true if an the list is empty, \b false otherwise
  210. */
  211. bool empty(MsgType const &trashhold = WARNING) const;
  212. /** \brief returns and removes the first (or last) message in the list
  213. *
  214. * \param[out] Text message of the first/last item
  215. *
  216. * \return \b true if the message was an error, \b false otherwise
  217. */
  218. bool PopMessage(std::string &Text);
  219. /** \brief clears the list of messages */
  220. void Discard();
  221. /** \brief outputs the list of messages to the given stream
  222. *
  223. * Note that all messages are discarded, also the notices
  224. * displayed or not.
  225. *
  226. * \param[out] out output stream to write the messages in
  227. * \param threshold minimim level considered
  228. * \param mergeStack
  229. */
  230. void DumpErrors(std::ostream &out, MsgType const &threshold = WARNING,
  231. bool const &mergeStack = true);
  232. /** \brief dumps the list of messages to std::cerr
  233. *
  234. * Note that all messages are discarded, also the notices
  235. * displayed or not.
  236. *
  237. * \param threshold minimum level printed
  238. */
  239. void inline DumpErrors(MsgType const &threshold) {
  240. DumpErrors(std::cerr, threshold);
  241. }
  242. // mvo: we do this instead of using a default parameter in the
  243. // previous declaration to avoid a (subtle) API break for
  244. // e.g. sigc++ and mem_fun0
  245. /** \brief dumps the messages of type WARNING or higher to std::cerr
  246. *
  247. * Note that all messages are discarded, displayed or not.
  248. *
  249. */
  250. void inline DumpErrors() {
  251. DumpErrors(WARNING);
  252. }
  253. /** \brief put the current Messages into the stack
  254. *
  255. * All "old" messages will be pushed into a stack to
  256. * them later back, but for now the Message query will be
  257. * empty and performs as no messages were present before.
  258. *
  259. * The stack can be as deep as you want - all stack operations
  260. * will only operate on the last element in the stack.
  261. */
  262. void PushToStack();
  263. /** \brief throw away all current messages */
  264. void RevertToStack();
  265. /** \brief merge current and stack together */
  266. void MergeWithStack();
  267. /** \brief return the deep of the stack */
  268. size_t StackCount() const {
  269. return Stacks.size();
  270. }
  271. GlobalError();
  272. /*}}}*/
  273. private: /*{{{*/
  274. struct Item {
  275. std::string Text;
  276. MsgType Type;
  277. Item(char const *Text, MsgType const &Type) :
  278. Text(Text), Type(Type) {};
  279. friend std::ostream& operator<< (std::ostream &out, Item i) {
  280. switch(i.Type) {
  281. case FATAL:
  282. case ERROR: out << "E"; break;
  283. case WARNING: out << "W"; break;
  284. case NOTICE: out << "N"; break;
  285. case DEBUG: out << "D"; break;
  286. }
  287. return out << ": " << i.Text;
  288. }
  289. };
  290. std::list<Item> Messages;
  291. bool PendingFlag;
  292. struct MsgStack {
  293. std::list<Item> const Messages;
  294. bool const PendingFlag;
  295. MsgStack(std::list<Item> const &Messages, bool const &Pending) :
  296. Messages(Messages), PendingFlag(Pending) {};
  297. };
  298. std::list<MsgStack> Stacks;
  299. /*}}}*/
  300. };
  301. /*}}}*/
  302. // The 'extra-ansi' syntax is used to help with collisions.
  303. GlobalError *_GetErrorObj();
  304. #define _error _GetErrorObj()
  305. #endif