]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/error.cc
ExecGPGV: Pass current config state to apt-key via temp file
[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>
95278287 30#include <algorithm>
4f333a8b 31
ea542140 32 /*}}}*/
578bfd0a 33
6f27a7fc
AL
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 */
b2e465d6 39#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
98ee7cd3
DK
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 }
6f27a7fc 55#else
98ee7cd3
DK
56 GlobalError *_GetErrorObj() {
57 static GlobalError *Obj = new GlobalError;
58 return Obj;
59 }
6f27a7fc
AL
60#endif
61 /*}}}*/
578bfd0a 62// GlobalError::GlobalError - Constructor /*{{{*/
98ee7cd3
DK
63GlobalError::GlobalError() : PendingFlag(false) {}
64 /*}}}*/
38f29703
DK
65// GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
66#define GEMessage(NAME, TYPE) \
67bool GlobalError::NAME (const char *Function, const char *Description,...) { \
68 va_list args; \
69 size_t msgSize = 400; \
70 int const errsv = errno; \
196d590a
DK
71 bool retry; \
72 do { \
38f29703 73 va_start(args,Description); \
196d590a 74 retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
38f29703 75 va_end(args); \
196d590a 76 } while (retry); \
38f29703 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;
196d590a
DK
92 bool retry;
93 do {
38f29703 94 va_start(args,Description);
196d590a 95 retry = InsertErrno(type, Function, Description, args, errsv, msgSize);
38f29703 96 va_end(args);
196d590a 97 } while (retry);
38f29703 98 return false;
cd7bbc47
DK
99}
100 /*}}}*/
98ee7cd3
DK
101// GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
102bool GlobalError::InsertErrno(MsgType type, const char* Function,
38f29703
DK
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;
6070a346 114 free(S);
38f29703 115 return true;
8f3853ba
DK
116 }
117
38f29703 118 bool const geins = Insert(type, S, args, msgSize);
8f3853ba
DK
119 free(S);
120 return geins;
98ee7cd3
DK
121}
122 /*}}}*/
38f29703
DK
123// GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
124#define GEMessage(NAME, TYPE) \
125bool GlobalError::NAME (const char *Description,...) { \
126 va_list args; \
127 size_t msgSize = 400; \
196d590a
DK
128 bool retry; \
129 do { \
38f29703 130 va_start(args,Description); \
196d590a 131 retry = Insert(TYPE, Description, args, msgSize); \
38f29703 132 va_end(args); \
196d590a 133 } while (retry); \
38f29703 134 return false; \
98ee7cd3 135}
38f29703
DK
136GEMessage(Fatal, FATAL)
137GEMessage(Error, ERROR)
138GEMessage(Warning, WARNING)
139GEMessage(Notice, NOTICE)
140GEMessage(Debug, DEBUG)
141#undef GEMessage
98ee7cd3 142 /*}}}*/
cd7bbc47
DK
143// GlobalError::Insert - Add a errotype message to the list /*{{{*/
144bool GlobalError::Insert(MsgType const &type, const char *Description,...)
145{
146 va_list args;
38f29703 147 size_t msgSize = 400;
196d590a
DK
148 bool retry;
149 do {
38f29703 150 va_start(args,Description);
196d590a 151 retry = Insert(type, Description, args, msgSize);
38f29703 152 va_end(args);
196d590a 153 } while (retry);
38f29703 154 return false;
cd7bbc47
DK
155}
156 /*}}}*/
98ee7cd3
DK
157// GlobalError::Insert - Insert a new item at the end /*{{{*/
158bool GlobalError::Insert(MsgType type, const char* Description,
38f29703
DK
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;
6070a346 168 free(S);
38f29703 169 return true;
8f3853ba 170 }
98ee7cd3
DK
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
8f3853ba 181 free(S);
98ee7cd3
DK
182 return false;
183}
184 /*}}}*/
185// GlobalError::PopMessage - Pulls a single message out /*{{{*/
186bool 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();
f7f0d6c7 200 m != Messages.end(); ++m)
98ee7cd3
DK
201 if (m->Type == ERROR || m->Type == FATAL)
202 return Ret;
203
204 PendingFlag = false;
205 return Ret;
578bfd0a
AL
206}
207 /*}}}*/
208// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
12be8a62 209void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
c4ba7c44
DK
210 bool const &mergeStack) {
211 if (mergeStack == true)
212 for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
213 s != Stacks.rend(); ++s)
95278287
DK
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 });
c4ba7c44 220
98ee7cd3 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 230// GlobalError::empty - does our error list include anything? /*{{{*/
95278287 231bool GlobalError::empty(MsgType const &threshold) const {
98ee7cd3
DK
232 if (PendingFlag == true)
233 return false;
234
235 if (Messages.empty() == true)
236 return true;
237
95278287
DK
238 return std::find_if(Messages.begin(), Messages.end(), [&threshold](Item const &m) {
239 return m.Type >= threshold;
240 }) == Messages.end();
6c139d6e
AL
241}
242 /*}}}*/
c4ba7c44
DK
243// GlobalError::PushToStack /*{{{*/
244void GlobalError::PushToStack() {
95278287 245 Stacks.emplace_back(Messages, PendingFlag);
c4ba7c44
DK
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();
95278287 261 Messages.splice(Messages.begin(), pack.Messages);
c4ba7c44
DK
262 PendingFlag = PendingFlag || pack.PendingFlag;
263 Stacks.pop_back();
264}
265 /*}}}*/