]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/error.cc
support very long mtab entries in mountpoint discovery
[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>
30
ea542140 31 /*}}}*/
578bfd0a 32
6f27a7fc
AL
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 */
b2e465d6 38#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
98ee7cd3
DK
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 }
6f27a7fc 54#else
98ee7cd3
DK
55 GlobalError *_GetErrorObj() {
56 static GlobalError *Obj = new GlobalError;
57 return Obj;
58 }
6f27a7fc
AL
59#endif
60 /*}}}*/
578bfd0a 61// GlobalError::GlobalError - Constructor /*{{{*/
98ee7cd3
DK
62GlobalError::GlobalError() : PendingFlag(false) {}
63 /*}}}*/
38f29703
DK
64// GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
65#define GEMessage(NAME, TYPE) \
66bool 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); \
9ca0d581 72 bool const retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
38f29703 73 va_end(args); \
9ca0d581
DK
74 if (retry == false) \
75 break; \
38f29703
DK
76 } \
77 return false; \
98ee7cd3 78}
38f29703
DK
79GEMessage(FatalE, FATAL)
80GEMessage(Errno, ERROR)
81GEMessage(WarningE, WARNING)
82GEMessage(NoticeE, NOTICE)
83GEMessage(DebugE, DEBUG)
84#undef GEMessage
98ee7cd3 85 /*}}}*/
cd7bbc47
DK
86// GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
87bool GlobalError::InsertErrno(MsgType const &type, const char *Function,
88 const char *Description,...) {
89 va_list args;
38f29703
DK
90 size_t msgSize = 400;
91 int const errsv = errno;
92 while (true) {
93 va_start(args,Description);
9ca0d581 94 bool const retry = InsertErrno(type, Function, Description, args, errsv, msgSize);
38f29703 95 va_end(args);
9ca0d581
DK
96 if (retry == false)
97 break;
38f29703
DK
98 }
99 return false;
cd7bbc47
DK
100}
101 /*}}}*/
98ee7cd3
DK
102// GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
103bool GlobalError::InsertErrno(MsgType type, const char* Function,
38f29703
DK
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;
6070a346 115 free(S);
38f29703 116 return true;
8f3853ba
DK
117 }
118
38f29703 119 bool const geins = Insert(type, S, args, msgSize);
8f3853ba
DK
120 free(S);
121 return geins;
98ee7cd3
DK
122}
123 /*}}}*/
38f29703
DK
124// GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
125#define GEMessage(NAME, TYPE) \
126bool 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; \
98ee7cd3 136}
38f29703
DK
137GEMessage(Fatal, FATAL)
138GEMessage(Error, ERROR)
139GEMessage(Warning, WARNING)
140GEMessage(Notice, NOTICE)
141GEMessage(Debug, DEBUG)
142#undef GEMessage
98ee7cd3 143 /*}}}*/
cd7bbc47
DK
144// GlobalError::Insert - Add a errotype message to the list /*{{{*/
145bool GlobalError::Insert(MsgType const &type, const char *Description,...)
146{
147 va_list args;
38f29703
DK
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;
cd7bbc47
DK
156}
157 /*}}}*/
98ee7cd3
DK
158// GlobalError::Insert - Insert a new item at the end /*{{{*/
159bool GlobalError::Insert(MsgType type, const char* Description,
38f29703
DK
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;
6070a346 169 free(S);
38f29703 170 return true;
8f3853ba 171 }
98ee7cd3
DK
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
8f3853ba 182 free(S);
98ee7cd3
DK
183 return false;
184}
185 /*}}}*/
186// GlobalError::PopMessage - Pulls a single message out /*{{{*/
187bool 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();
f7f0d6c7 201 m != Messages.end(); ++m)
98ee7cd3
DK
202 if (m->Type == ERROR || m->Type == FATAL)
203 return Ret;
204
205 PendingFlag = false;
206 return Ret;
578bfd0a
AL
207}
208 /*}}}*/
209// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
12be8a62 210void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
c4ba7c44
DK
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
98ee7cd3 217 for (std::list<Item>::const_iterator m = Messages.begin();
f7f0d6c7 218 m != Messages.end(); ++m)
12be8a62 219 if (m->Type >= threshold)
98ee7cd3
DK
220 out << (*m) << std::endl;
221 Discard();
578bfd0a
AL
222}
223 /*}}}*/
98ee7cd3
DK
224// GlobalError::Discard - Discard /*{{{*/
225void GlobalError::Discard() {
226 Messages.clear();
227 PendingFlag = false;
d3e8fbb3 228}
6c139d6e 229 /*}}}*/
98ee7cd3
DK
230// GlobalError::empty - does our error list include anything? /*{{{*/
231bool 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();
f7f0d6c7 239 m != Messages.end(); ++m)
98ee7cd3
DK
240 if (m->Type >= trashhold)
241 return false;
242
243 return true;
6c139d6e
AL
244}
245 /*}}}*/
c4ba7c44
DK
246// GlobalError::PushToStack /*{{{*/
247void GlobalError::PushToStack() {
248 MsgStack pack(Messages, PendingFlag);
249 Stacks.push_back(pack);
250 Discard();
251}
252 /*}}}*/
253// GlobalError::RevertToStack /*{{{*/
254void 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 /*{{{*/
263void 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 /*}}}*/