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