]>
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 /*{{{*/
18 #include <apt-pkg/error.h>
33 // Global Error Object /*{{{*/
34 /* If the implementation supports posix threads then the accessor function
35 is compiled to be thread safe otherwise a non-safe version is used. A
36 Per-Thread error object is maintained in much the same manner as libc
38 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
41 static pthread_key_t ErrorKey
;
42 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
43 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
45 GlobalError
*_GetErrorObj() {
46 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
47 pthread_once(&Once
,KeyAlloc
);
49 void *Res
= pthread_getspecific(ErrorKey
);
51 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
52 return (GlobalError
*)Res
;
55 GlobalError
*_GetErrorObj() {
56 static GlobalError
*Obj
= new GlobalError
;
61 // GlobalError::GlobalError - Constructor /*{{{*/
62 GlobalError::GlobalError() : PendingFlag(false) {}
64 // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
65 #define GEMessage(NAME, TYPE) \
66 bool GlobalError::NAME (const char *Function, const char *Description,...) { \
68 size_t msgSize = 400; \
69 int const errsv = errno; \
71 va_start(args,Description); \
72 bool const retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
79 GEMessage(FatalE
, FATAL
)
80 GEMessage(Errno
, ERROR
)
81 GEMessage(WarningE
, WARNING
)
82 GEMessage(NoticeE
, NOTICE
)
83 GEMessage(DebugE
, DEBUG
)
86 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
87 bool GlobalError::InsertErrno(MsgType
const &type
, const char *Function
,
88 const char *Description
,...) {
91 int const errsv
= errno
;
93 va_start(args
,Description
);
94 bool const retry
= InsertErrno(type
, Function
, Description
, args
, errsv
, msgSize
);
102 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
103 bool GlobalError::InsertErrno(MsgType type
, const char* Function
,
104 const char* Description
, va_list &args
,
105 int const errsv
, size_t &msgSize
) {
106 char* S
= (char*) malloc(msgSize
);
107 int const n
= snprintf(S
, msgSize
, "%s - %s (%i: %s)", Description
,
108 Function
, errsv
, strerror(errsv
));
109 if (n
> -1 && ((unsigned int) n
) < msgSize
);
119 bool const geins
= Insert(type
, S
, args
, msgSize
);
124 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
125 #define GEMessage(NAME, TYPE) \
126 bool GlobalError::NAME (const char *Description,...) { \
128 size_t msgSize = 400; \
130 va_start(args,Description); \
131 if (Insert(TYPE, Description, args, msgSize) == false) \
137 GEMessage(Fatal
, FATAL
)
138 GEMessage(Error
, ERROR
)
139 GEMessage(Warning
, WARNING
)
140 GEMessage(Notice
, NOTICE
)
141 GEMessage(Debug
, DEBUG
)
144 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
145 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
148 size_t msgSize
= 400;
150 va_start(args
,Description
);
151 if (Insert(type
, Description
, args
, msgSize
) == false)
158 // GlobalError::Insert - Insert a new item at the end /*{{{*/
159 bool GlobalError::Insert(MsgType type
, const char* Description
,
160 va_list &args
, size_t &msgSize
) {
161 char* S
= (char*) malloc(msgSize
);
162 int const n
= vsnprintf(S
, msgSize
, Description
, args
);
163 if (n
> -1 && ((unsigned int) n
) < msgSize
);
173 Item
const m(S
, type
);
174 Messages
.push_back(m
);
176 if (type
== ERROR
|| type
== FATAL
)
179 if (type
== FATAL
|| type
== DEBUG
)
180 std::clog
<< m
<< std::endl
;
186 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
187 bool GlobalError::PopMessage(std::string
&Text
) {
188 if (Messages
.empty() == true)
191 Item
const msg
= Messages
.front();
192 Messages
.pop_front();
194 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
196 if (PendingFlag
== false || Ret
== false)
199 // check if another error message is pending
200 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
201 m
!= Messages
.end(); ++m
)
202 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
209 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
210 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
211 bool const &mergeStack
) {
212 if (mergeStack
== true)
213 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
214 s
!= Stacks
.rend(); ++s
)
215 Messages
.insert(Messages
.begin(), s
->Messages
.begin(), s
->Messages
.end());
217 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
218 m
!= Messages
.end(); ++m
)
219 if (m
->Type
>= threshold
)
220 out
<< (*m
) << std::endl
;
224 // GlobalError::Discard - Discard /*{{{*/
225 void GlobalError::Discard() {
230 // GlobalError::empty - does our error list include anything? /*{{{*/
231 bool GlobalError::empty(MsgType
const &trashhold
) const {
232 if (PendingFlag
== true)
235 if (Messages
.empty() == true)
238 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
239 m
!= Messages
.end(); ++m
)
240 if (m
->Type
>= trashhold
)
246 // GlobalError::PushToStack /*{{{*/
247 void GlobalError::PushToStack() {
248 MsgStack
pack(Messages
, PendingFlag
);
249 Stacks
.push_back(pack
);
253 // GlobalError::RevertToStack /*{{{*/
254 void GlobalError::RevertToStack() {
256 MsgStack pack
= Stacks
.back();
257 Messages
= pack
.Messages
;
258 PendingFlag
= pack
.PendingFlag
;
262 // GlobalError::MergeWithStack /*{{{*/
263 void GlobalError::MergeWithStack() {
264 MsgStack pack
= Stacks
.back();
265 Messages
.insert(Messages
.begin(), pack
.Messages
.begin(), pack
.Messages
.end());
266 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;