]>
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 | /*}}}*/ | |
61 | // GlobalError::FatalE - Get part of the error string from errno /*{{{*/ | |
62 | bool GlobalError::FatalE(const char *Function,const char *Description,...) { | |
63 | va_list args; | |
64 | va_start(args,Description); | |
65 | return InsertErrno(FATAL, Function, Description, args); | |
578bfd0a AL |
66 | } |
67 | /*}}}*/ | |
68 | // GlobalError::Errno - Get part of the error string from errno /*{{{*/ | |
98ee7cd3 DK |
69 | bool GlobalError::Errno(const char *Function,const char *Description,...) { |
70 | va_list args; | |
71 | va_start(args,Description); | |
72 | return InsertErrno(ERROR, Function, Description, args); | |
c5162d56 AL |
73 | } |
74 | /*}}}*/ | |
98ee7cd3 DK |
75 | // GlobalError::WarningE - Get part of the warning string from errno /*{{{*/ |
76 | bool GlobalError::WarningE(const char *Function,const char *Description,...) { | |
77 | va_list args; | |
78 | va_start(args,Description); | |
79 | return InsertErrno(WARNING, Function, Description, args); | |
80 | } | |
81 | /*}}}*/ | |
82 | // GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/ | |
83 | bool GlobalError::NoticeE(const char *Function,const char *Description,...) { | |
84 | va_list args; | |
85 | va_start(args,Description); | |
86 | return InsertErrno(NOTICE, Function, Description, args); | |
87 | } | |
88 | /*}}}*/ | |
89 | // GlobalError::DebugE - Get part of the debug string from errno /*{{{*/ | |
90 | bool GlobalError::DebugE(const char *Function,const char *Description,...) { | |
91 | va_list args; | |
92 | va_start(args,Description); | |
93 | return InsertErrno(DEBUG, Function, Description, args); | |
94 | } | |
95 | /*}}}*/ | |
cd7bbc47 DK |
96 | // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/ |
97 | bool GlobalError::InsertErrno(MsgType const &type, const char *Function, | |
98 | const char *Description,...) { | |
99 | va_list args; | |
100 | va_start(args,Description); | |
101 | return InsertErrno(type, Function, Description, args); | |
102 | } | |
103 | /*}}}*/ | |
98ee7cd3 DK |
104 | // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/ |
105 | bool GlobalError::InsertErrno(MsgType type, const char* Function, | |
3c0929ec | 106 | const char* Description, va_list &args) { |
8f3853ba DK |
107 | int const errsv = errno; |
108 | char* S = (char*) malloc(400); | |
109 | size_t const Ssize = snprintf(S, 400, "%s - %s (%i: %s)", Description, | |
110 | Function, errsv, strerror(errsv)) + 1; | |
111 | ||
112 | if (Ssize > 400) { | |
113 | free(S); | |
114 | S = (char*) malloc(Ssize); | |
115 | snprintf(S, Ssize, "%s - %s (%i: %s)", Description, | |
116 | Function, errsv, strerror(errsv)); | |
117 | } | |
118 | ||
119 | bool const geins = Insert(type, S, args); | |
120 | free(S); | |
121 | return geins; | |
98ee7cd3 DK |
122 | } |
123 | /*}}}*/ | |
124 | // GlobalError::Fatal - Add a fatal error to the list /*{{{*/ | |
125 | bool GlobalError::Fatal(const char *Description,...) { | |
126 | va_list args; | |
127 | va_start(args,Description); | |
128 | return Insert(FATAL, Description, args); | |
578bfd0a AL |
129 | } |
130 | /*}}}*/ | |
131 | // GlobalError::Error - Add an error to the list /*{{{*/ | |
98ee7cd3 DK |
132 | bool GlobalError::Error(const char *Description,...) { |
133 | va_list args; | |
134 | va_start(args,Description); | |
135 | return Insert(ERROR, Description, args); | |
578bfd0a AL |
136 | } |
137 | /*}}}*/ | |
138 | // GlobalError::Warning - Add a warning to the list /*{{{*/ | |
98ee7cd3 DK |
139 | bool GlobalError::Warning(const char *Description,...) { |
140 | va_list args; | |
141 | va_start(args,Description); | |
142 | return Insert(WARNING, Description, args); | |
143 | } | |
144 | /*}}}*/ | |
145 | // GlobalError::Notice - Add a notice to the list /*{{{*/ | |
146 | bool GlobalError::Notice(const char *Description,...) | |
578bfd0a | 147 | { |
98ee7cd3 DK |
148 | va_list args; |
149 | va_start(args,Description); | |
150 | return Insert(NOTICE, Description, args); | |
578bfd0a AL |
151 | } |
152 | /*}}}*/ | |
98ee7cd3 DK |
153 | // GlobalError::Debug - Add a debug to the list /*{{{*/ |
154 | bool GlobalError::Debug(const char *Description,...) | |
578bfd0a | 155 | { |
98ee7cd3 DK |
156 | va_list args; |
157 | va_start(args,Description); | |
158 | return Insert(DEBUG, Description, args); | |
159 | } | |
160 | /*}}}*/ | |
cd7bbc47 DK |
161 | // GlobalError::Insert - Add a errotype message to the list /*{{{*/ |
162 | bool GlobalError::Insert(MsgType const &type, const char *Description,...) | |
163 | { | |
164 | va_list args; | |
165 | va_start(args,Description); | |
166 | return Insert(type, Description, args); | |
167 | } | |
168 | /*}}}*/ | |
98ee7cd3 DK |
169 | // GlobalError::Insert - Insert a new item at the end /*{{{*/ |
170 | bool GlobalError::Insert(MsgType type, const char* Description, | |
3c0929ec | 171 | va_list &args) { |
8f3853ba DK |
172 | char* S = (char*) malloc(400); |
173 | size_t const Ssize = vsnprintf(S, 400, Description, args) + 1; | |
174 | ||
175 | if (Ssize > 400) { | |
176 | free(S); | |
177 | S = (char*) malloc(Ssize); | |
178 | vsnprintf(S, Ssize, Description, args); | |
179 | } | |
98ee7cd3 DK |
180 | |
181 | Item const m(S, type); | |
182 | Messages.push_back(m); | |
183 | ||
184 | if (type == ERROR || type == FATAL) | |
185 | PendingFlag = true; | |
186 | ||
187 | if (type == FATAL || type == DEBUG) | |
188 | std::clog << m << std::endl; | |
189 | ||
8f3853ba | 190 | free(S); |
98ee7cd3 DK |
191 | return false; |
192 | } | |
193 | /*}}}*/ | |
194 | // GlobalError::PopMessage - Pulls a single message out /*{{{*/ | |
195 | bool GlobalError::PopMessage(std::string &Text) { | |
196 | if (Messages.empty() == true) | |
197 | return false; | |
198 | ||
199 | Item const msg = Messages.front(); | |
200 | Messages.pop_front(); | |
201 | ||
202 | bool const Ret = (msg.Type == ERROR || msg.Type == FATAL); | |
203 | Text = msg.Text; | |
204 | if (PendingFlag == false || Ret == false) | |
205 | return Ret; | |
206 | ||
207 | // check if another error message is pending | |
208 | for (std::list<Item>::const_iterator m = Messages.begin(); | |
209 | m != Messages.end(); m++) | |
210 | if (m->Type == ERROR || m->Type == FATAL) | |
211 | return Ret; | |
212 | ||
213 | PendingFlag = false; | |
214 | return Ret; | |
578bfd0a AL |
215 | } |
216 | /*}}}*/ | |
217 | // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/ | |
12be8a62 | 218 | void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold, |
c4ba7c44 DK |
219 | bool const &mergeStack) { |
220 | if (mergeStack == true) | |
221 | for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin(); | |
222 | s != Stacks.rend(); ++s) | |
223 | Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end()); | |
224 | ||
98ee7cd3 DK |
225 | for (std::list<Item>::const_iterator m = Messages.begin(); |
226 | m != Messages.end(); m++) | |
12be8a62 | 227 | if (m->Type >= threshold) |
98ee7cd3 DK |
228 | out << (*m) << std::endl; |
229 | Discard(); | |
578bfd0a AL |
230 | } |
231 | /*}}}*/ | |
98ee7cd3 DK |
232 | // GlobalError::Discard - Discard /*{{{*/ |
233 | void GlobalError::Discard() { | |
234 | Messages.clear(); | |
235 | PendingFlag = false; | |
6c139d6e AL |
236 | }; |
237 | /*}}}*/ | |
98ee7cd3 DK |
238 | // GlobalError::empty - does our error list include anything? /*{{{*/ |
239 | bool GlobalError::empty(MsgType const &trashhold) const { | |
240 | if (PendingFlag == true) | |
241 | return false; | |
242 | ||
243 | if (Messages.empty() == true) | |
244 | return true; | |
245 | ||
246 | for (std::list<Item>::const_iterator m = Messages.begin(); | |
247 | m != Messages.end(); m++) | |
248 | if (m->Type >= trashhold) | |
249 | return false; | |
250 | ||
251 | return true; | |
6c139d6e AL |
252 | } |
253 | /*}}}*/ | |
c4ba7c44 DK |
254 | // GlobalError::PushToStack /*{{{*/ |
255 | void GlobalError::PushToStack() { | |
256 | MsgStack pack(Messages, PendingFlag); | |
257 | Stacks.push_back(pack); | |
258 | Discard(); | |
259 | } | |
260 | /*}}}*/ | |
261 | // GlobalError::RevertToStack /*{{{*/ | |
262 | void GlobalError::RevertToStack() { | |
263 | Discard(); | |
264 | MsgStack pack = Stacks.back(); | |
265 | Messages = pack.Messages; | |
266 | PendingFlag = pack.PendingFlag; | |
267 | Stacks.pop_back(); | |
268 | } | |
269 | /*}}}*/ | |
270 | // GlobalError::MergeWithStack /*{{{*/ | |
271 | void GlobalError::MergeWithStack() { | |
272 | MsgStack pack = Stacks.back(); | |
273 | Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end()); | |
274 | PendingFlag = PendingFlag || pack.PendingFlag; | |
275 | Stacks.pop_back(); | |
276 | } | |
277 | /*}}}*/ |