apt  0.9.7.7ubuntu4
error.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: error.h,v 1.8 2001/05/07 05:06:52 jgg Exp $
4 /* ######################################################################
5 
6  Global Erorr Class - Global error mechanism
7 
8  This class has a single global instance. When a function needs to
9  generate an error condition, such as a read error, it calls a member
10  in this class to add the error to a stack of errors.
11 
12  By using a stack the problem with a scheme like errno is removed and
13  it allows a very detailed account of what went wrong to be transmitted
14  to the UI for display. (Errno has problems because each function sets
15  errno to 0 if it didn't have an error thus eraseing erno in the process
16  of cleanup)
17 
18  Several predefined error generators are provided to handle common
19  things like errno. The general idea is that all methods return a bool.
20  If the bool is true then things are OK, if it is false then things
21  should start being undone and the stack should unwind under program
22  control.
23 
24  A Warning should not force the return of false. Things did not fail, but
25  they might have had unexpected problems. Errors are stored in a FIFO
26  so Pop will return the first item..
27 
28  I have some thoughts about extending this into a more general UI<->
29  Engine interface, ie allowing the Engine to say 'The disk is full' in
30  a dialog that says 'Panic' and 'Retry'.. The error generator functions
31  like errno, Warning and Error return false always so this is normal:
32  if (open(..))
33  return _error->Errno(..);
34 
35  This source is placed in the Public Domain, do with it what you will
36  It was originally written by Jason Gunthorpe.
37 
38  ##################################################################### */
39  /*}}}*/
40 #ifndef PKGLIB_ERROR_H
41 #define PKGLIB_ERROR_H
42 
43 #include <apt-pkg/macros.h>
44 
45 #include <iostream>
46 #include <list>
47 #include <string>
48 
49 #include <stdarg.h>
50 
51 class GlobalError /*{{{*/
52 {
53 public: /*{{{*/
55  enum MsgType {
58  FATAL = 40,
60  ERROR = 30,
62  WARNING = 20,
64  NOTICE = 10,
66  DEBUG = 0
67  };
68 
76  bool FatalE(const char *Function,const char *Description,...) __like_printf(3) __cold;
77 
85  bool Errno(const char *Function,const char *Description,...) __like_printf(3) __cold;
86 
97  bool WarningE(const char *Function,const char *Description,...) __like_printf(3) __cold;
98 
106  bool NoticeE(const char *Function,const char *Description,...) __like_printf(3) __cold;
107 
115  bool DebugE(const char *Function,const char *Description,...) __like_printf(3) __cold;
116 
123  bool InsertErrno(MsgType const &type, const char* Function,
124  const char* Description,...) __like_printf(4) __cold;
125 
139  bool Fatal(const char *Description,...) __like_printf(2) __cold;
140 
147  bool Error(const char *Description,...) __like_printf(2) __cold;
148 
158  bool Warning(const char *Description,...) __like_printf(2) __cold;
159 
171  bool Notice(const char *Description,...) __like_printf(2) __cold;
172 
179  bool Debug(const char *Description,...) __like_printf(2) __cold;
180 
186  bool Insert(MsgType const &type, const char* Description,...) __like_printf(3) __cold;
187 
192  inline bool PendingError() const {return PendingFlag;};
193 
204  bool empty(MsgType const &trashhold = WARNING) const;
205 
212  bool PopMessage(std::string &Text);
213 
215  void Discard();
216 
226  void DumpErrors(std::ostream &out, MsgType const &threshold = WARNING,
227  bool const &mergeStack = true);
228 
236  void inline DumpErrors(MsgType const &threshold) {
237  DumpErrors(std::cerr, threshold);
238  }
239 
240  // mvo: we do this instead of using a default parameter in the
241  // previous declaration to avoid a (subtle) API break for
242  // e.g. sigc++ and mem_fun0
248  void inline DumpErrors() {
250  }
251 
261  void PushToStack();
262 
264  void RevertToStack();
265 
267  void MergeWithStack();
268 
270  size_t StackCount() const {
271  return Stacks.size();
272  }
273 
274  GlobalError();
275  /*}}}*/
276 private: /*{{{*/
277  struct Item {
278  std::string Text;
279  MsgType Type;
280 
281  Item(char const *Text, MsgType const &Type) :
282  Text(Text), Type(Type) {};
283 
284  friend std::ostream& operator<< (std::ostream &out, Item i) {
285  switch(i.Type) {
286  case FATAL:
287  case ERROR: out << "E"; break;
288  case WARNING: out << "W"; break;
289  case NOTICE: out << "N"; break;
290  case DEBUG: out << "D"; break;
291  }
292  return out << ": " << i.Text;
293  }
294  };
295 
296  std::list<Item> Messages;
297  bool PendingFlag;
298 
299  struct MsgStack {
300  std::list<Item> const Messages;
301  bool const PendingFlag;
302 
303  MsgStack(std::list<Item> const &Messages, bool const &Pending) :
304  Messages(Messages), PendingFlag(Pending) {};
305  };
306 
307  std::list<MsgStack> Stacks;
308 
309  bool InsertErrno(MsgType type, const char* Function,
310  const char* Description, va_list &args,
311  int const errsv, size_t &msgSize);
312  bool Insert(MsgType type, const char* Description,
313  va_list &args, size_t &msgSize);
314  /*}}}*/
315 };
316  /*}}}*/
317 
318 // The 'extra-ansi' syntax is used to help with collisions.
319 GlobalError *_GetErrorObj();
320 #define _error _GetErrorObj()
321 
322 #endif