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