]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
233b185f | 3 | // $Id: error.h,v 1.8 2001/05/07 05:06:52 jgg Exp $ |
578bfd0a AL |
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 | /*}}}*/ | |
578bfd0a AL |
40 | #ifndef PKGLIB_ERROR_H |
41 | #define PKGLIB_ERROR_H | |
42 | ||
13500573 | 43 | |
6c139d6e | 44 | |
b2e465d6 AL |
45 | #ifdef __GNUG__ |
46 | // Methods have a hidden this parameter that is visible to this attribute | |
47 | #define APT_MFORMAT1 __attribute__ ((format (printf, 2, 3))) | |
48 | #define APT_MFORMAT2 __attribute__ ((format (printf, 3, 4))) | |
49 | #else | |
50 | #define APT_MFORMAT1 | |
51 | #define APT_MFORMAT2 | |
52 | #endif | |
53 | ||
578bfd0a | 54 | #include <string> |
578bfd0a | 55 | |
233b185f AL |
56 | using std::string; |
57 | ||
578bfd0a AL |
58 | class GlobalError |
59 | { | |
60 | struct Item | |
61 | { | |
62 | string Text; | |
63 | bool Error; | |
6c139d6e | 64 | Item *Next; |
578bfd0a AL |
65 | }; |
66 | ||
6c139d6e | 67 | Item *List; |
578bfd0a | 68 | bool PendingFlag; |
6c139d6e | 69 | void Insert(Item *I); |
578bfd0a AL |
70 | |
71 | public: | |
72 | ||
73 | // Call to generate an error from a library call. | |
b2e465d6 AL |
74 | bool Errno(const char *Function,const char *Description,...) APT_MFORMAT2; |
75 | bool WarningE(const char *Function,const char *Description,...) APT_MFORMAT2; | |
578bfd0a AL |
76 | |
77 | /* A warning should be considered less severe than an error, and may be | |
78 | ignored by the client. */ | |
b2e465d6 AL |
79 | bool Error(const char *Description,...) APT_MFORMAT1; |
80 | bool Warning(const char *Description,...) APT_MFORMAT1; | |
578bfd0a AL |
81 | |
82 | // Simple accessors | |
83 | inline bool PendingError() {return PendingFlag;}; | |
6c139d6e | 84 | inline bool empty() {return List == 0;}; |
578bfd0a | 85 | bool PopMessage(string &Text); |
6c139d6e | 86 | void Discard(); |
578bfd0a AL |
87 | |
88 | // Usefull routine to dump to cerr | |
89 | void DumpErrors(); | |
90 | ||
91 | GlobalError(); | |
92 | }; | |
93 | ||
6f27a7fc AL |
94 | // The 'extra-ansi' syntax is used to help with collisions. |
95 | GlobalError *_GetErrorObj(); | |
96 | #define _error _GetErrorObj() | |
578bfd0a | 97 | |
b2e465d6 AL |
98 | #undef APT_MFORMAT1 |
99 | #undef APT_MFORMAT2 | |
100 | ||
578bfd0a | 101 | #endif |