]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
Merge branch 'portability/freebsd'
[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 #include <algorithm>
31
32 /*}}}*/
33
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 */
39 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
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 }
55 #else
56 GlobalError *_GetErrorObj() {
57 static GlobalError *Obj = new GlobalError;
58 return Obj;
59 }
60 #endif
61 /*}}}*/
62 // GlobalError::GlobalError - Constructor /*{{{*/
63 GlobalError::GlobalError() : PendingFlag(false) {}
64 /*}}}*/
65 // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
66 #define GEMessage(NAME, TYPE) \
67 bool GlobalError::NAME (const char *Function, const char *Description,...) { \
68 va_list args; \
69 size_t msgSize = 400; \
70 int const errsv = errno; \
71 bool retry; \
72 do { \
73 va_start(args,Description); \
74 retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
75 va_end(args); \
76 } while (retry); \
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 bool retry;
93 do {
94 va_start(args,Description);
95 retry = InsertErrno(type, Function, Description, args, errsv, msgSize);
96 va_end(args);
97 } while (retry);
98 return false;
99 }
100 /*}}}*/
101 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
102 bool GlobalError::InsertErrno(MsgType type, const char* Function,
103 const char* Description, va_list &args,
104 int const errsv, size_t &msgSize) {
105 char* S = (char*) malloc(msgSize);
106 int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description,
107 Function, errsv, strerror(errsv));
108 if (n > -1 && ((unsigned int) n) < msgSize);
109 else {
110 if (n > -1)
111 msgSize = n + 1;
112 else
113 msgSize *= 2;
114 free(S);
115 return true;
116 }
117
118 bool const geins = Insert(type, S, args, msgSize);
119 free(S);
120 return geins;
121 }
122 /*}}}*/
123 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
124 #define GEMessage(NAME, TYPE) \
125 bool GlobalError::NAME (const char *Description,...) { \
126 va_list args; \
127 size_t msgSize = 400; \
128 bool retry; \
129 do { \
130 va_start(args,Description); \
131 retry = Insert(TYPE, Description, args, msgSize); \
132 va_end(args); \
133 } while (retry); \
134 return false; \
135 }
136 GEMessage(Fatal, FATAL)
137 GEMessage(Error, ERROR)
138 GEMessage(Warning, WARNING)
139 GEMessage(Notice, NOTICE)
140 GEMessage(Debug, DEBUG)
141 #undef GEMessage
142 /*}}}*/
143 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
144 bool GlobalError::Insert(MsgType const &type, const char *Description,...)
145 {
146 va_list args;
147 size_t msgSize = 400;
148 bool retry;
149 do {
150 va_start(args,Description);
151 retry = Insert(type, Description, args, msgSize);
152 va_end(args);
153 } while (retry);
154 return false;
155 }
156 /*}}}*/
157 // GlobalError::Insert - Insert a new item at the end /*{{{*/
158 bool GlobalError::Insert(MsgType type, const char* Description,
159 va_list &args, size_t &msgSize) {
160 char* S = (char*) malloc(msgSize);
161 int const n = vsnprintf(S, msgSize, Description, args);
162 if (n > -1 && ((unsigned int) n) < msgSize);
163 else {
164 if (n > -1)
165 msgSize = n + 1;
166 else
167 msgSize *= 2;
168 free(S);
169 return true;
170 }
171
172 Item const m(S, type);
173 Messages.push_back(m);
174
175 if (type == ERROR || type == FATAL)
176 PendingFlag = true;
177
178 if (type == FATAL || type == DEBUG)
179 std::clog << m << std::endl;
180
181 free(S);
182 return false;
183 }
184 /*}}}*/
185 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
186 bool GlobalError::PopMessage(std::string &Text) {
187 if (Messages.empty() == true)
188 return false;
189
190 Item const msg = Messages.front();
191 Messages.pop_front();
192
193 bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
194 Text = msg.Text;
195 if (PendingFlag == false || Ret == false)
196 return Ret;
197
198 // check if another error message is pending
199 for (std::list<Item>::const_iterator m = Messages.begin();
200 m != Messages.end(); ++m)
201 if (m->Type == ERROR || m->Type == FATAL)
202 return Ret;
203
204 PendingFlag = false;
205 return Ret;
206 }
207 /*}}}*/
208 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
209 void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
210 bool const &mergeStack) {
211 if (mergeStack == true)
212 for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
213 s != Stacks.rend(); ++s)
214 std::copy(s->Messages.begin(), s->Messages.end(), std::front_inserter(Messages));
215
216 std::for_each(Messages.begin(), Messages.end(), [&threshold, &out](Item const &m) {
217 if (m.Type >= threshold)
218 out << m << std::endl;
219 });
220
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 &threshold) const {
232 if (PendingFlag == true)
233 return false;
234
235 if (Messages.empty() == true)
236 return true;
237
238 return std::find_if(Messages.begin(), Messages.end(), [&threshold](Item const &m) {
239 return m.Type >= threshold;
240 }) == Messages.end();
241 }
242 /*}}}*/
243 // GlobalError::PushToStack /*{{{*/
244 void GlobalError::PushToStack() {
245 Stacks.emplace_back(Messages, PendingFlag);
246 Discard();
247 }
248 /*}}}*/
249 // GlobalError::RevertToStack /*{{{*/
250 void GlobalError::RevertToStack() {
251 Discard();
252 MsgStack pack = Stacks.back();
253 Messages = pack.Messages;
254 PendingFlag = pack.PendingFlag;
255 Stacks.pop_back();
256 }
257 /*}}}*/
258 // GlobalError::MergeWithStack /*{{{*/
259 void GlobalError::MergeWithStack() {
260 MsgStack pack = Stacks.back();
261 Messages.splice(Messages.begin(), pack.Messages);
262 PendingFlag = PendingFlag || pack.PendingFlag;
263 Stacks.pop_back();
264 }
265 /*}}}*/