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