]>
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 - Get part of the error string from errno /*{{{*/
62 bool GlobalError::FatalE(const char *Function
,const char *Description
,...) {
64 va_start(args
,Description
);
65 return InsertErrno(FATAL
, Function
, Description
, args
);
68 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
69 bool GlobalError::Errno(const char *Function
,const char *Description
,...) {
71 va_start(args
,Description
);
72 return InsertErrno(ERROR
, Function
, Description
, args
);
75 // GlobalError::WarningE - Get part of the warning string from errno /*{{{*/
76 bool GlobalError::WarningE(const char *Function
,const char *Description
,...) {
78 va_start(args
,Description
);
79 return InsertErrno(WARNING
, Function
, Description
, args
);
82 // GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/
83 bool GlobalError::NoticeE(const char *Function
,const char *Description
,...) {
85 va_start(args
,Description
);
86 return InsertErrno(NOTICE
, Function
, Description
, args
);
89 // GlobalError::DebugE - Get part of the debug string from errno /*{{{*/
90 bool GlobalError::DebugE(const char *Function
,const char *Description
,...) {
92 va_start(args
,Description
);
93 return InsertErrno(DEBUG
, Function
, Description
, args
);
96 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
97 bool GlobalError::InsertErrno(MsgType
const &type
, const char *Function
,
98 const char *Description
,...) {
100 va_start(args
,Description
);
101 return InsertErrno(type
, Function
, Description
, args
);
104 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
105 bool GlobalError::InsertErrno(MsgType type
, const char* Function
,
106 const char* Description
, va_list &args
) {
107 int const errsv
= errno
;
108 char* S
= (char*) malloc(400);
109 size_t const Ssize
= snprintf(S
, 400, "%s - %s (%i: %s)", Description
,
110 Function
, errsv
, strerror(errsv
)) + 1;
114 S
= (char*) malloc(Ssize
);
115 snprintf(S
, Ssize
, "%s - %s (%i: %s)", Description
,
116 Function
, errsv
, strerror(errsv
));
119 bool const geins
= Insert(type
, S
, args
);
124 // GlobalError::Fatal - Add a fatal error to the list /*{{{*/
125 bool GlobalError::Fatal(const char *Description
,...) {
127 va_start(args
,Description
);
128 return Insert(FATAL
, Description
, args
);
131 // GlobalError::Error - Add an error to the list /*{{{*/
132 bool GlobalError::Error(const char *Description
,...) {
134 va_start(args
,Description
);
135 return Insert(ERROR
, Description
, args
);
138 // GlobalError::Warning - Add a warning to the list /*{{{*/
139 bool GlobalError::Warning(const char *Description
,...) {
141 va_start(args
,Description
);
142 return Insert(WARNING
, Description
, args
);
145 // GlobalError::Notice - Add a notice to the list /*{{{*/
146 bool GlobalError::Notice(const char *Description
,...)
149 va_start(args
,Description
);
150 return Insert(NOTICE
, Description
, args
);
153 // GlobalError::Debug - Add a debug to the list /*{{{*/
154 bool GlobalError::Debug(const char *Description
,...)
157 va_start(args
,Description
);
158 return Insert(DEBUG
, Description
, args
);
161 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
162 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
165 va_start(args
,Description
);
166 return Insert(type
, Description
, args
);
169 // GlobalError::Insert - Insert a new item at the end /*{{{*/
170 bool GlobalError::Insert(MsgType type
, const char* Description
,
172 char* S
= (char*) malloc(400);
173 size_t const Ssize
= vsnprintf(S
, 400, Description
, args
) + 1;
177 S
= (char*) malloc(Ssize
);
178 vsnprintf(S
, Ssize
, Description
, args
);
181 Item
const m(S
, type
);
182 Messages
.push_back(m
);
184 if (type
== ERROR
|| type
== FATAL
)
187 if (type
== FATAL
|| type
== DEBUG
)
188 std::clog
<< m
<< std::endl
;
194 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
195 bool GlobalError::PopMessage(std::string
&Text
) {
196 if (Messages
.empty() == true)
199 Item
const msg
= Messages
.front();
200 Messages
.pop_front();
202 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
204 if (PendingFlag
== false || Ret
== false)
207 // check if another error message is pending
208 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
209 m
!= Messages
.end(); m
++)
210 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
217 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
218 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
219 bool const &mergeStack
) {
220 if (mergeStack
== true)
221 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
222 s
!= Stacks
.rend(); ++s
)
223 Messages
.insert(Messages
.begin(), s
->Messages
.begin(), s
->Messages
.end());
225 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
226 m
!= Messages
.end(); m
++)
227 if (m
->Type
>= threshold
)
228 out
<< (*m
) << std::endl
;
232 // GlobalError::Discard - Discard /*{{{*/
233 void GlobalError::Discard() {
238 // GlobalError::empty - does our error list include anything? /*{{{*/
239 bool GlobalError::empty(MsgType
const &trashhold
) const {
240 if (PendingFlag
== true)
243 if (Messages
.empty() == true)
246 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
247 m
!= Messages
.end(); m
++)
248 if (m
->Type
>= trashhold
)
254 // GlobalError::PushToStack /*{{{*/
255 void GlobalError::PushToStack() {
256 MsgStack
pack(Messages
, PendingFlag
);
257 Stacks
.push_back(pack
);
261 // GlobalError::RevertToStack /*{{{*/
262 void GlobalError::RevertToStack() {
264 MsgStack pack
= Stacks
.back();
265 Messages
= pack
.Messages
;
266 PendingFlag
= pack
.PendingFlag
;
270 // GlobalError::MergeWithStack /*{{{*/
271 void GlobalError::MergeWithStack() {
272 MsgStack pack
= Stacks
.back();
273 Messages
.insert(Messages
.begin(), pack
.Messages
.begin(), pack
.Messages
.end());
274 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;