]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
merged from 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 <apt-pkg/error.h>
17
18 #include <iostream>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include <string>
25 #include <cstring>
26
27 #include "config.h"
28 /*}}}*/
29
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 */
35 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
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 }
51 #else
52 GlobalError *_GetErrorObj() {
53 static GlobalError *Obj = new GlobalError;
54 return Obj;
55 }
56 #endif
57 /*}}}*/
58 // GlobalError::GlobalError - Constructor /*{{{*/
59 GlobalError::GlobalError() : PendingFlag(false) {}
60 /*}}}*/
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; \
74 }
75 GEMessage(FatalE, FATAL)
76 GEMessage(Errno, ERROR)
77 GEMessage(WarningE, WARNING)
78 GEMessage(NoticeE, NOTICE)
79 GEMessage(DebugE, DEBUG)
80 #undef GEMessage
81 /*}}}*/
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;
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;
95 }
96 /*}}}*/
97 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
98 bool GlobalError::InsertErrno(MsgType type, const char* Function,
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;
111 }
112
113 bool const geins = Insert(type, S, args, msgSize);
114 free(S);
115 return geins;
116 }
117 /*}}}*/
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; \
130 }
131 GEMessage(Fatal, FATAL)
132 GEMessage(Error, ERROR)
133 GEMessage(Warning, WARNING)
134 GEMessage(Notice, NOTICE)
135 GEMessage(Debug, DEBUG)
136 #undef GEMessage
137 /*}}}*/
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;
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;
150 }
151 /*}}}*/
152 // GlobalError::Insert - Insert a new item at the end /*{{{*/
153 bool GlobalError::Insert(MsgType type, const char* Description,
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;
164 }
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
175 free(S);
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;
200 }
201 /*}}}*/
202 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
203 void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
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
210 for (std::list<Item>::const_iterator m = Messages.begin();
211 m != Messages.end(); m++)
212 if (m->Type >= threshold)
213 out << (*m) << std::endl;
214 Discard();
215 }
216 /*}}}*/
217 // GlobalError::Discard - Discard /*{{{*/
218 void GlobalError::Discard() {
219 Messages.clear();
220 PendingFlag = false;
221 };
222 /*}}}*/
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;
237 }
238 /*}}}*/
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 /*}}}*/