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