]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
578bfd0a | 3 | /* ###################################################################### |
98ee7cd3 DK |
4 | |
5 | Global Error Class - Global error mechanism | |
578bfd0a AL |
6 | |
7 | We use a simple STL vector to store each error record. A PendingFlag | |
8 | is kept which indicates when the vector contains a Sever error. | |
98ee7cd3 | 9 | |
578bfd0a AL |
10 | This source is placed in the Public Domain, do with it what you will |
11 | It was originally written by Jason Gunthorpe. | |
98ee7cd3 | 12 | |
578bfd0a AL |
13 | ##################################################################### */ |
14 | /*}}}*/ | |
15 | // Include Files /*{{{*/ | |
6f27a7fc AL |
16 | #include <apt-pkg/error.h> |
17 | ||
90f057fd | 18 | #include <iostream> |
578bfd0a AL |
19 | #include <errno.h> |
20 | #include <stdio.h> | |
8f3853ba | 21 | #include <stdlib.h> |
6f27a7fc | 22 | #include <unistd.h> |
578bfd0a | 23 | |
4f333a8b MV |
24 | #include <string> |
25 | #include <cstring> | |
26 | ||
afd9aaf3 | 27 | #include "config.h" |
578bfd0a AL |
28 | /*}}}*/ |
29 | ||
6f27a7fc AL |
30 | // Global Error Object /*{{{*/ |
31 | /* If the implementation supports posix threads then the accessor function | |
32 | is compiled to be thread safe otherwise a non-safe version is used. A | |
33 | Per-Thread error object is maintained in much the same manner as libc | |
34 | manages errno */ | |
b2e465d6 | 35 | #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD) |
98ee7cd3 DK |
36 | #include <pthread.h> |
37 | ||
38 | static pthread_key_t ErrorKey; | |
39 | static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;}; | |
40 | static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);}; | |
41 | ||
42 | GlobalError *_GetErrorObj() { | |
43 | static pthread_once_t Once = PTHREAD_ONCE_INIT; | |
44 | pthread_once(&Once,KeyAlloc); | |
45 | ||
46 | void *Res = pthread_getspecific(ErrorKey); | |
47 | if (Res == 0) | |
48 | pthread_setspecific(ErrorKey,Res = new GlobalError); | |
49 | return (GlobalError *)Res; | |
50 | } | |
6f27a7fc | 51 | #else |
98ee7cd3 DK |
52 | GlobalError *_GetErrorObj() { |
53 | static GlobalError *Obj = new GlobalError; | |
54 | return Obj; | |
55 | } | |
6f27a7fc AL |
56 | #endif |
57 | /*}}}*/ | |
578bfd0a | 58 | // GlobalError::GlobalError - Constructor /*{{{*/ |
98ee7cd3 DK |
59 | GlobalError::GlobalError() : PendingFlag(false) {} |
60 | /*}}}*/ | |
38f29703 DK |
61 | // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/ |
62 | #define GEMessage(NAME, TYPE) \ | |
63 | bool GlobalError::NAME (const char *Function, const char *Description,...) { \ | |
64 | va_list args; \ | |
65 | size_t msgSize = 400; \ | |
66 | int const errsv = errno; \ | |
67 | while (true) { \ | |
68 | va_start(args,Description); \ | |
69 | if (InsertErrno(TYPE, Function, Description, args, errsv, msgSize) == false) \ | |
70 | break; \ | |
71 | va_end(args); \ | |
72 | } \ | |
73 | return false; \ | |
98ee7cd3 | 74 | } |
38f29703 DK |
75 | GEMessage(FatalE, FATAL) |
76 | GEMessage(Errno, ERROR) | |
77 | GEMessage(WarningE, WARNING) | |
78 | GEMessage(NoticeE, NOTICE) | |
79 | GEMessage(DebugE, DEBUG) | |
80 | #undef GEMessage | |
98ee7cd3 | 81 | /*}}}*/ |
cd7bbc47 DK |
82 | // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/ |
83 | bool GlobalError::InsertErrno(MsgType const &type, const char *Function, | |
84 | const char *Description,...) { | |
85 | va_list args; | |
38f29703 DK |
86 | size_t msgSize = 400; |
87 | int const errsv = errno; | |
88 | while (true) { | |
89 | va_start(args,Description); | |
90 | if (InsertErrno(type, Function, Description, args, errsv, msgSize) == false) | |
91 | break; | |
92 | va_end(args); | |
93 | } | |
94 | return false; | |
cd7bbc47 DK |
95 | } |
96 | /*}}}*/ | |
98ee7cd3 DK |
97 | // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/ |
98 | bool GlobalError::InsertErrno(MsgType type, const char* Function, | |
38f29703 DK |
99 | const char* Description, va_list &args, |
100 | int const errsv, size_t &msgSize) { | |
101 | char* S = (char*) malloc(msgSize); | |
102 | int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description, | |
103 | Function, errsv, strerror(errsv)); | |
104 | if (n > -1 && ((unsigned int) n) < msgSize); | |
105 | else { | |
106 | if (n > -1) | |
107 | msgSize = n + 1; | |
108 | else | |
109 | msgSize *= 2; | |
110 | return true; | |
8f3853ba DK |
111 | } |
112 | ||
38f29703 | 113 | bool const geins = Insert(type, S, args, msgSize); |
8f3853ba DK |
114 | free(S); |
115 | return geins; | |
98ee7cd3 DK |
116 | } |
117 | /*}}}*/ | |
38f29703 DK |
118 | // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/ |
119 | #define GEMessage(NAME, TYPE) \ | |
120 | bool GlobalError::NAME (const char *Description,...) { \ | |
121 | va_list args; \ | |
122 | size_t msgSize = 400; \ | |
123 | while (true) { \ | |
124 | va_start(args,Description); \ | |
125 | if (Insert(TYPE, Description, args, msgSize) == false) \ | |
126 | break; \ | |
127 | va_end(args); \ | |
128 | } \ | |
129 | return false; \ | |
98ee7cd3 | 130 | } |
38f29703 DK |
131 | GEMessage(Fatal, FATAL) |
132 | GEMessage(Error, ERROR) | |
133 | GEMessage(Warning, WARNING) | |
134 | GEMessage(Notice, NOTICE) | |
135 | GEMessage(Debug, DEBUG) | |
136 | #undef GEMessage | |
98ee7cd3 | 137 | /*}}}*/ |
cd7bbc47 DK |
138 | // GlobalError::Insert - Add a errotype message to the list /*{{{*/ |
139 | bool GlobalError::Insert(MsgType const &type, const char *Description,...) | |
140 | { | |
141 | va_list args; | |
38f29703 DK |
142 | size_t msgSize = 400; |
143 | while (true) { | |
144 | va_start(args,Description); | |
145 | if (Insert(type, Description, args, msgSize) == false) | |
146 | break; | |
147 | va_end(args); | |
148 | } | |
149 | return false; | |
cd7bbc47 DK |
150 | } |
151 | /*}}}*/ | |
98ee7cd3 DK |
152 | // GlobalError::Insert - Insert a new item at the end /*{{{*/ |
153 | bool GlobalError::Insert(MsgType type, const char* Description, | |
38f29703 DK |
154 | va_list &args, size_t &msgSize) { |
155 | char* S = (char*) malloc(msgSize); | |
156 | int const n = vsnprintf(S, msgSize, Description, args); | |
157 | if (n > -1 && ((unsigned int) n) < msgSize); | |
158 | else { | |
159 | if (n > -1) | |
160 | msgSize = n + 1; | |
161 | else | |
162 | msgSize *= 2; | |
163 | return true; | |
8f3853ba | 164 | } |
98ee7cd3 DK |
165 | |
166 | Item const m(S, type); | |
167 | Messages.push_back(m); | |
168 | ||
169 | if (type == ERROR || type == FATAL) | |
170 | PendingFlag = true; | |
171 | ||
172 | if (type == FATAL || type == DEBUG) | |
173 | std::clog << m << std::endl; | |
174 | ||
8f3853ba | 175 | free(S); |
98ee7cd3 DK |
176 | return false; |
177 | } | |
178 | /*}}}*/ | |
179 | // GlobalError::PopMessage - Pulls a single message out /*{{{*/ | |
180 | bool GlobalError::PopMessage(std::string &Text) { | |
181 | if (Messages.empty() == true) | |
182 | return false; | |
183 | ||
184 | Item const msg = Messages.front(); | |
185 | Messages.pop_front(); | |
186 | ||
187 | bool const Ret = (msg.Type == ERROR || msg.Type == FATAL); | |
188 | Text = msg.Text; | |
189 | if (PendingFlag == false || Ret == false) | |
190 | return Ret; | |
191 | ||
192 | // check if another error message is pending | |
193 | for (std::list<Item>::const_iterator m = Messages.begin(); | |
194 | m != Messages.end(); m++) | |
195 | if (m->Type == ERROR || m->Type == FATAL) | |
196 | return Ret; | |
197 | ||
198 | PendingFlag = false; | |
199 | return Ret; | |
578bfd0a AL |
200 | } |
201 | /*}}}*/ | |
202 | // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/ | |
12be8a62 | 203 | void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold, |
c4ba7c44 DK |
204 | bool const &mergeStack) { |
205 | if (mergeStack == true) | |
206 | for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin(); | |
207 | s != Stacks.rend(); ++s) | |
208 | Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end()); | |
209 | ||
98ee7cd3 DK |
210 | for (std::list<Item>::const_iterator m = Messages.begin(); |
211 | m != Messages.end(); m++) | |
12be8a62 | 212 | if (m->Type >= threshold) |
98ee7cd3 DK |
213 | out << (*m) << std::endl; |
214 | Discard(); | |
578bfd0a AL |
215 | } |
216 | /*}}}*/ | |
98ee7cd3 DK |
217 | // GlobalError::Discard - Discard /*{{{*/ |
218 | void GlobalError::Discard() { | |
219 | Messages.clear(); | |
220 | PendingFlag = false; | |
6c139d6e AL |
221 | }; |
222 | /*}}}*/ | |
98ee7cd3 DK |
223 | // GlobalError::empty - does our error list include anything? /*{{{*/ |
224 | bool GlobalError::empty(MsgType const &trashhold) const { | |
225 | if (PendingFlag == true) | |
226 | return false; | |
227 | ||
228 | if (Messages.empty() == true) | |
229 | return true; | |
230 | ||
231 | for (std::list<Item>::const_iterator m = Messages.begin(); | |
232 | m != Messages.end(); m++) | |
233 | if (m->Type >= trashhold) | |
234 | return false; | |
235 | ||
236 | return true; | |
6c139d6e AL |
237 | } |
238 | /*}}}*/ | |
c4ba7c44 DK |
239 | // GlobalError::PushToStack /*{{{*/ |
240 | void GlobalError::PushToStack() { | |
241 | MsgStack pack(Messages, PendingFlag); | |
242 | Stacks.push_back(pack); | |
243 | Discard(); | |
244 | } | |
245 | /*}}}*/ | |
246 | // GlobalError::RevertToStack /*{{{*/ | |
247 | void GlobalError::RevertToStack() { | |
248 | Discard(); | |
249 | MsgStack pack = Stacks.back(); | |
250 | Messages = pack.Messages; | |
251 | PendingFlag = pack.PendingFlag; | |
252 | Stacks.pop_back(); | |
253 | } | |
254 | /*}}}*/ | |
255 | // GlobalError::MergeWithStack /*{{{*/ | |
256 | void GlobalError::MergeWithStack() { | |
257 | MsgStack pack = Stacks.back(); | |
258 | Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end()); | |
259 | PendingFlag = PendingFlag || pack.PendingFlag; | |
260 | Stacks.pop_back(); | |
261 | } | |
262 | /*}}}*/ |