]>
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
);
113 bool const geins
= Insert(type
, S
, args
, msgSize
);
118 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
119 #define GEMessage(NAME, TYPE) \
120 bool GlobalError::NAME (const char *Description,...) { \
122 size_t msgSize = 400; \
124 va_start(args,Description); \
125 if (Insert(TYPE, Description, args, msgSize) == false) \
131 GEMessage(Fatal
, FATAL
)
132 GEMessage(Error
, ERROR
)
133 GEMessage(Warning
, WARNING
)
134 GEMessage(Notice
, NOTICE
)
135 GEMessage(Debug
, DEBUG
)
138 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
139 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
142 size_t msgSize
= 400;
144 va_start(args
,Description
);
145 if (Insert(type
, Description
, args
, msgSize
) == false)
152 // GlobalError::Insert - Insert a new item at the end /*{{{*/
153 bool GlobalError::Insert(MsgType type
, const char* Description
,
154 va_list &args
, size_t &msgSize
) {
155 char* S
= (char*) malloc(msgSize
);
156 int const n
= vsnprintf(S
, msgSize
, Description
, args
);
157 if (n
> -1 && ((unsigned int) n
) < msgSize
);
166 Item
const m(S
, type
);
167 Messages
.push_back(m
);
169 if (type
== ERROR
|| type
== FATAL
)
172 if (type
== FATAL
|| type
== DEBUG
)
173 std::clog
<< m
<< std::endl
;
179 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
180 bool GlobalError::PopMessage(std::string
&Text
) {
181 if (Messages
.empty() == true)
184 Item
const msg
= Messages
.front();
185 Messages
.pop_front();
187 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
189 if (PendingFlag
== false || Ret
== false)
192 // check if another error message is pending
193 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
194 m
!= Messages
.end(); m
++)
195 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
202 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
203 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
204 bool const &mergeStack
) {
205 if (mergeStack
== true)
206 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
207 s
!= Stacks
.rend(); ++s
)
208 Messages
.insert(Messages
.begin(), s
->Messages
.begin(), s
->Messages
.end());
210 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
211 m
!= Messages
.end(); m
++)
212 if (m
->Type
>= threshold
)
213 out
<< (*m
) << std::endl
;
217 // GlobalError::Discard - Discard /*{{{*/
218 void GlobalError::Discard() {
223 // GlobalError::empty - does our error list include anything? /*{{{*/
224 bool GlobalError::empty(MsgType
const &trashhold
) const {
225 if (PendingFlag
== true)
228 if (Messages
.empty() == true)
231 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
232 m
!= Messages
.end(); m
++)
233 if (m
->Type
>= trashhold
)
239 // GlobalError::PushToStack /*{{{*/
240 void GlobalError::PushToStack() {
241 MsgStack
pack(Messages
, PendingFlag
);
242 Stacks
.push_back(pack
);
246 // GlobalError::RevertToStack /*{{{*/
247 void GlobalError::RevertToStack() {
249 MsgStack pack
= Stacks
.back();
250 Messages
= pack
.Messages
;
251 PendingFlag
= pack
.PendingFlag
;
255 // GlobalError::MergeWithStack /*{{{*/
256 void GlobalError::MergeWithStack() {
257 MsgStack pack
= Stacks
.back();
258 Messages
.insert(Messages
.begin(), pack
.Messages
.begin(), pack
.Messages
.end());
259 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;