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