]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Global Error Class - Global error mechanism
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.
10 This source is placed in the Public Domain, do with it what you will
11 It was originally written by Jason Gunthorpe.
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/error.h>
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
35 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
38 static pthread_key_t ErrorKey
;
39 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
40 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
42 GlobalError
*_GetErrorObj() {
43 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
44 pthread_once(&Once
,KeyAlloc
);
46 void *Res
= pthread_getspecific(ErrorKey
);
48 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
49 return (GlobalError
*)Res
;
52 GlobalError
*_GetErrorObj() {
53 static GlobalError
*Obj
= new GlobalError
;
58 // GlobalError::GlobalError - Constructor /*{{{*/
59 GlobalError::GlobalError() : PendingFlag(false) {}
61 // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
62 #define GEMessage(NAME, TYPE) \
63 bool GlobalError::NAME (const char *Function, const char *Description,...) { \
65 size_t msgSize = 400; \
66 int const errsv = errno; \
68 va_start(args,Description); \
69 if (InsertErrno(TYPE, Function, Description, args, errsv, msgSize) == false) \
75 GEMessage(FatalE
, FATAL
)
76 GEMessage(Errno
, ERROR
)
77 GEMessage(WarningE
, WARNING
)
78 GEMessage(NoticeE
, NOTICE
)
79 GEMessage(DebugE
, DEBUG
)
82 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
83 bool GlobalError::InsertErrno(MsgType
const &type
, const char *Function
,
84 const char *Description
,...) {
87 int const errsv
= errno
;
89 va_start(args
,Description
);
90 if (InsertErrno(type
, Function
, Description
, args
, errsv
, msgSize
) == false)
97 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
98 bool 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
);
114 bool const geins
= Insert(type
, S
, args
, msgSize
);
119 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
120 #define GEMessage(NAME, TYPE) \
121 bool GlobalError::NAME (const char *Description,...) { \
123 size_t msgSize = 400; \
125 va_start(args,Description); \
126 if (Insert(TYPE, Description, args, msgSize) == false) \
132 GEMessage(Fatal
, FATAL
)
133 GEMessage(Error
, ERROR
)
134 GEMessage(Warning
, WARNING
)
135 GEMessage(Notice
, NOTICE
)
136 GEMessage(Debug
, DEBUG
)
139 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
140 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
143 size_t msgSize
= 400;
145 va_start(args
,Description
);
146 if (Insert(type
, Description
, args
, msgSize
) == false)
153 // GlobalError::Insert - Insert a new item at the end /*{{{*/
154 bool 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
);
168 Item
const m(S
, type
);
169 Messages
.push_back(m
);
171 if (type
== ERROR
|| type
== FATAL
)
174 if (type
== FATAL
|| type
== DEBUG
)
175 std::clog
<< m
<< std::endl
;
181 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
182 bool GlobalError::PopMessage(std::string
&Text
) {
183 if (Messages
.empty() == true)
186 Item
const msg
= Messages
.front();
187 Messages
.pop_front();
189 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
191 if (PendingFlag
== false || Ret
== false)
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
)
204 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
205 void 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());
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
;
219 // GlobalError::Discard - Discard /*{{{*/
220 void GlobalError::Discard() {
225 // GlobalError::empty - does our error list include anything? /*{{{*/
226 bool GlobalError::empty(MsgType
const &trashhold
) const {
227 if (PendingFlag
== true)
230 if (Messages
.empty() == true)
233 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
234 m
!= Messages
.end(); m
++)
235 if (m
->Type
>= trashhold
)
241 // GlobalError::PushToStack /*{{{*/
242 void GlobalError::PushToStack() {
243 MsgStack
pack(Messages
, PendingFlag
);
244 Stacks
.push_back(pack
);
248 // GlobalError::RevertToStack /*{{{*/
249 void GlobalError::RevertToStack() {
251 MsgStack pack
= Stacks
.back();
252 Messages
= pack
.Messages
;
253 PendingFlag
= pack
.PendingFlag
;
257 // GlobalError::MergeWithStack /*{{{*/
258 void GlobalError::MergeWithStack() {
259 MsgStack pack
= Stacks
.back();
260 Messages
.insert(Messages
.begin(), pack
.Messages
.begin(), pack
.Messages
.end());
261 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;